Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var heap:array[1..100000] of longint;
- i,j,n,m,z,c:longint;
- procedure build(i:longint);
- var l,r,max:longint;
- begin
- l:=i shl 1; r:=l+1; max:=i;
- if (l<=n) and (heap[l] > heap[max]) then
- max:=l;
- if (r<=n) and (heap[r] > heap[max]) then
- max:=r;
- if (max <> i) then
- begin
- swap(heap[i],heap[max]);
- build(max);
- end;
- end;
- procedure delete_max;
- begin
- swap(heap[1],heap[n]); dec(n);
- build(1);
- end;
- procedure add(a:longint);
- begin
- inc(n); heap[n]:=a; i:=n;
- while (i > 1) and (heap[i] > heap[i shr 1]) do
- begin
- swap(heap[i],heap[i shr 1]);
- i:=i shr 1;
- end;
- end;
- Begin
- end.
Advertisement
Add Comment
Please, Sign In to add comment