Cod sursa(job #411679)

Utilizator hungntnktpHungntnktp hungntnktp Data 5 martie 2010 04:19:25
Problema Heapuri Scor 100
Compilator fpc Status done
Runda Arhiva educationala Marime 1.54 kb
const fi        =       'heapuri.in';
      fo        =       'heapuri.out';
      maxn      =       222222;
var ok          :       array[0..maxn] of boolean;
    a,heap      :       array[0..maxn] of longint;
    n,nh        :       longint;
    f1,f2       :       text;

procedure swap(var a,b:longint);
Var tg : longint;
 begin
  tg := a; a := b; b := tg;
 end;

procedure push(u:longint);
Var i,j : longint;
 begin
  inc(nh);
  heap[nh] := u;
  i := nh; j := i div 2;
  While j > 0 do
   begin
    if a[heap[j]] <= a[heap[i]] then break;
    swap(heap[i],heap[j]);
    i := j; j := i div 2;
   end;
 end;

procedure pop;
var i,j : longint;
 begin
  heap[1] := heap[nh];
  dec(nh);
  i := 1; j := 2;
  While j <= nh do
   begin
    if (j < nh) and (a[heap[j]] > a[heap[j+1]]) then inc(j);
    if a[heap[i]] < a[heap[j]] then break;
    swap(heap[i],heap[j]);
    i := j; j := i*2;
   end;
 end;

procedure process;
var i,j,k,d : longint;
 begin
  assign(f1,fi); Assign(f2,fo);
  reset(f1); Rewrite(f2);
  readln(f1,n);
  Fillchar(ok,SizeOf(ok),true);
  d := 0;
  for i := 1 to n do
   begin
    Read(f1,j);
    If j <> 3 then Read(f1,k);

    If j = 1 then
     begin
      inc(d);
      a[d] := k;
      Push(d);
     end
      else
    If j = 2 then ok[k] := false
    else
     begin
      j := heap[1];
      While not ok[j] do
       begin
        pop;
        j := heap[1];
       end;
      Writeln(f2,a[j]);
     end;
   end;
  close(f1);
  close(f2);
 end;

begin
 process;
end.