Cod sursa(job #929217)

Utilizator Dddarius95Darius-Florentin Neatu Dddarius95 Data 26 martie 2013 22:04:19
Problema Heapuri Scor 100
Compilator fpc Status done
Runda Arhiva educationala Marime 2.08 kb
program dasas;
type heap=array[1..200 * 1000] of longword;
var  n,i,x,r,m:longword; op:byte;
     h,a,pos:heap;
     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
inc(r);  inc(N);
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,'heapuri.out');rewrite(g);settextbuf(g,iesire);
readln(f,m);
n:=0; r:=0;
for i:=1 to m do
                 begin
                 read(f,op);
                 if op=1 then begin
                              readln(f,x);
                              insert(n,r,x);
                              end;
                 if op=2 then begin
                              readln(f,x);
                              delete(n,x);
                              end;
                 if op=3 then begin
                              readln(f);
                              writeln(g,h[1]);
                              end;
                 end;


{
for i:=1 to m do
  begin
  read(f,op);
  if (op=3) then writeln(g,h[1])
            else
                begin
                read(f,x);
                if (op=1) then insert(n,r,x)
                          else delete(n,x);
                end;
  readln(f);
  end;
}
close(f);close(g);
end.