实现的功能——输入1 x,将x加入小根堆中;输入2,输出最小值并去在堆中除掉
1 var 2 i,j,k,l,m,n,head:longint; 3 a,lef,rig,fix:array[0..100000] of longint; 4 function min(x,y:longint):longint;inline; 5 begin 6 if x<y then min:=x else min:=y; 7 end; 8 function max(x,y:longint):longint;inline; 9 begin 10 if x>y then max:=x else max:=y; 11 end; 12 procedure swap(var x,y:longint);inline; 13 var z:longint; 14 begin 15 z:=x;x:=y;y:=z; 16 end; 17 procedure merge(var x,y:longint); 18 begin 19 if x=0 then swap(x,y); 20 if y=0 then exit; 21 if a[x]>a[y] then swap(x,y); 22 merge(rig[x],y); 23 fix[x]:=min(fix[lef[x]],fix[rig[x]])+1; 24 if fix[lef[x]]<fix[rig[x]] then swap(lef[x],rig[x]); 25 end; 26 function cuthead(var x:longint):longint; 27 var a1:longint; 28 begin 29 a1:=a[x]; 30 merge(lef[x],rig[x]); 31 x:=lef[x]; 32 exit(a1); 33 end; 34 begin 35 readln(n);m:=0;head:=0; 36 for i:=1 to n do 37 begin 38 read(j); 39 case j of 40 1:begin 41 readln(k); 42 inc(m);a[m]:=k;l:=m; 43 merge(head,l); 44 end; 45 2:begin 46 readln; 47 writeln(cuthead(head)); 48 end; 49 end; 50 end; 51 end. 52
时间: 2024-11-03 20:56:02