Cod sursa(job #929182)

Utilizator Dddarius95Darius-Florentin Neatu Dddarius95 Data 26 martie 2013 21:41:10
Problema Heapuri Scor 0
Compilator fpc Status done
Runda Arhiva educationala Marime 1.44 kb
program heapuri;
type heap=array[1..200 * 1000] of longword;
var  h,a,pos:heap;
     n,m,r,i,op,x:longword;
     f,g:text;
     intrare,iesire:array[1..1 shl 17]of char;

procedure swap(var a,b:longword);
var aux:longword;
begin
aux:=a;a:=b;b:=aux;
end;

procedure insert(var n,r:longword; x:longword);
var tata,fiu:longword;
begin
r:=r+1;
n:=n+1;
h[n]:=x;
a[n]:=r;
pos[r]:=n;
fiu:=n;
tata:=fiu div 2;
while (tata>0)and(h[tata]>h[fiu]) do
  begin
  swap(h[tata],h[fiu]);
  pos[a[tata]]:=fiu;
  pos[a[fiu]]:=tata;
  swap(a[tata],a[fiu]);
  fiu:=tata;
  tata:=fiu div 2;
  end;
end;

procedure delete(var n:longword; r:longword);
var tata,fiu:longword;
begin
h[pos[r]]:=h[n];
a[pos[r]]:=a[n];
pos[a[n]]:=pos[r];
tata:=pos[r];
fiu:=2*tata;
if (fiu+1<=n)and(h[fiu+1]<h[fiu]) then inc(fiu);
while (fiu<=n)and(h[fiu]<h[tata]) do
    begin
    swap(h[tata],h[fiu]);
    pos[a[tata]]:=fiu;
    pos[a[fiu]]:=tata;
    swap(a[tata],a[fiu]);
    tata:=fiu;
    fiu:=2*tata;
    if (fiu+1<=n)and(h[fiu+1]<h[fiu])then inc(fiu);
    end;
end;


begin
assign(f,'heapuri.in');reset(f);settextbuf(f,intrare);
assign(g,'heaputi.out');rewrite(g);settextbuf(g,iesire);
readln( m );
n:=0; r:=0;
for i:=1 to m do
  begin
  read(op);
  if (op=3) then writeln(g,h[1])
    else begin
         read(x);
         if (op=1) then insert(n,r,x)
                   else delete(n,x);
         end;
  end;
close(f);close(g);
end.