Cod sursa(job #1414522)

Utilizator ButnaruButnaru George Butnaru Data 2 aprilie 2015 18:14:07
Problema Heapuri Scor 10
Compilator fpc Status done
Runda Arhiva educationala Marime 1.3 kb
program heapuri;
type
date=record
x,pos:longint;
end;
    tabel=array[0..200001] of date;
    tabb=array[0..200001] of longint;
var heap:tabel; pos:tabb;
    n,i,j,tip,x,nr:longint;
    f1,f2:text;
procedure swap(var a,b:date);
var aux:date; aux1:longint;
begin aux:=a; a:=b; b:=aux;
aux1:=pos[a.pos]; pos[a.pos]:=pos[b.pos]; pos[b.pos]:=aux1;
end;
procedure heapup(v:longint);
var k:date;
begin
k:=heap[v];
while (v>1) and (heap[v div 2].x>heap[v].x) do begin
swap(heap[v],heap[v div 2]);
v:=v div 2;
end;
heap[v]:=k;
end;
procedure heapdown(v:longint);
var w:longint;
begin
w:=v*2;
while w<=nr do begin
if (w+1<=nr) and (heap[w+1].x<heap[w].x) then w:=w+1;
if heap[w].x<heap[v].x then swap(heap[v],heap[w]) else break;
v:=w; w:=w*2;
end;
end;
procedure add_heap;
begin
nr:=nr+1; heap[nr].x:=x; heap[nr].pos:=nr; pos[nr]:=nr;
heapup(nr);
end;
procedure deletheap(v:longint);
begin
swap(heap[v],heap[nr]); nr:=nr-1;
heapup(v);
heapdown(v);
end;
begin
assign (f1,'heapuri.in');
assign (f2,'heapuri.out');
reset (f1);
rewrite (f2);
readln (f1,n);
nr:=0;
for i:=1 to n do begin
read (f1,tip);
if tip=1 then begin read (f1,x); add_heap; end else
if tip=2 then begin read (f1,x); deletheap(pos[x]); end else
writeln (f2,heap[1].x);
readln (f1);
end;
close (f1);
close (f2);
end.