Cod sursa(job #961954)

Utilizator bongdayeahsuper qa bongdayeah Data 13 iunie 2013 12:35:36
Problema Heapuri Scor 0
Compilator fpc Status done
Runda Arhiva educationala Marime 3.51 kb
PRogram heappuri;
const   fi='heapfuri.inp';
        fo='heapfuri.out';
        max=200000;
var     f,fa                    :       text;
        heap                    :       array[1..max] of longint;
        N                       :       longint;
        u,v                     :       longint;
        Nheap                   :       longint;
        a                       :       array[1..max] of longint;
        duc                     :       longint;
(*************************)
Procedure swap(var x,y:longint);
var tg:longint;
begin
        tg:=x;
        x:=y;
        y:=tg;
end;
(*************************)
Procedure upheap(i:longint);
begin
        If (i=1) or (heap[i] >= heap[i div 2]) then exit ;
                Swap(heap[i],heap[i div 2]);
                upheap(i div 2);
end;
(*************************)
Procedure downheap(i:longint);
var j:longint;
begin
        j:=i*2;
        If j>Nheap then exit;
        If heap[j] > heap[j+1] then inc(j);
        If heap[i] > heap[j] then
                begin
                        Swap(heap[i],heap[j]);
                        downheap(j);
                end;
end;
(*************************)
Procedure push(i:longint);
begin
        inc(Nheap);
        Heap[nheap]:=i;
        Upheap(nheap);
end;
(*************************)
function pop(i:longint):longint;
begin
        pop:=heap[i];
        heap[i]:=heap[nheap];
        dec(nheap);
        downheap(i);
end;
(*************************)
Procedure Process;
var j,i:longint;

begin
        assign(f,fi);
        reset(f);
        Readln(f,n);
        Nheap:=0;
        i:=1;
        while not seekeof(f) do
                begin
                        Read(f,u);
                        {assign(F,fo);
                        rewrite(f);}
                                If u<3 then
                                   begin
                                        Readln(f,v);
                                        If u=1 then
                                               begin
                                                 push(v);
                                                 a[i]:=v;
                                                 inc(i);
                                               end
                                                else
                                                        begin
                                                                For j:=1 to nheap do
                                                                        If heap[j]=a[v] then
                                                                                begin
                                                                                        pop(j);
                                                                                        break;
                                                                                end;
                                                        end;

                                   end
                                        else
                                                begin

                                                        duc:=pop(1);
                                                        Writeln(fa,duc);

                                                end;

                       // close(f);
                end;
        close(f);
end;
(*************************)
begin
        assign(fa,fo);
        rewrite(fa);
        Process;
        close(Fa);
end.