Cod sursa(job #1637734)

Utilizator robertadRoxana Rodile robertad Data 7 martie 2016 18:59:15
Problema Heapuri Scor 40
Compilator fpc Status done
Runda Arhiva educationala Marime 1.75 kb
program heapuri;
var f,g:text;
   A,H,pos:array[1..200009] of longint;
   cod,x,L,nr:longint;
procedure push(x:longint);
var aux:integer;
  begin
    aux:=0;
    while (x div 2 >0) and (A[H[x]]<A[H[x div 2]]) do
      begin
        aux:=H[x];
        H[x]:=H[x div 2];
        H[x div 2]:=aux;
        pos[H[x]]:=x;
        pos[H[x div 2]]:=x div 2;
        x:=x div 2;
      end;
  end;
procedure pop(x:longint);
var aux,y:longint;
  begin
    y:=0;
    aux:=0;
    while (x<>y) do
      begin
        y:=x;
        if (y*2<=L) and (A[H[x]]>A[H[y*2]]) then
                                            x:=y*2;
        if (y*2+1<=L) and (A[H[x]]>A[H[y*2+1]]) then
                                                x:=y*2+1;
        aux:=H[x];
        H[x]:=H[y];
        H[y]:=aux;
        pos[H[x]]:=x;
        pos[H[y]]:=y;
      end;
  end;
begin
  assign(f,'heapuri.in');
  assign(g,'heapuri.out');
  reset(f);
  rewrite(g);
  readln(f,x);
  while not seekeof(f) do
   begin
     read(f,cod);
     if cod=1 then
              begin
                readln(f,x);
                L:=L+1;
                nr:=nr+1;
                A[nr]:=x;
                H[L]:=nr;
                pos[nr]:=L;
                push(L);
              end
              else
     if cod=2 then
              begin
                readln(f,x);
                A[x]:=-1;
                push(pos[x]);
                pos[H[1]]:=0;
                H[1]:=H[L];
                L:=L-1;
                pos[H[1]]:=1;
                if (L>1) then
                         pop(1);
              end
              else
     if cod=3 then
              begin
                writeln(g,A[H[1]]);
              end;
   end;
  close(f);
  close(g);
end.