Pagini recente » Cod sursa (job #299073) | Istoria paginii planificare/sedinta-201410xx | Profil popoiu.george | Cod sursa (job #370053) | Cod sursa (job #409128)
Cod sursa(job #409128)
var h:array [1..500000] of longint;
buf:array [1..65000] of byte;
n,k,i,x:longint;
f,g:text;
procedure inv(var x,y:longint);
var aux:longint;
begin
aux:=x;
x:=y;
y:=aux;
end;
procedure siftup(i:longint);
begin
if i<>1 then
if h[i]>h[i div 2] then
begin
inv(h[i],h[i div 2]);
siftup(i div 2);
end;
end;
procedure addheap(x,i:longint);
begin
h[i]:=x;
siftup(i);
end;
procedure siftdown(i,k:longint);
var fiu:longint;
begin
fiu:=i;
if i*2<=k then
if h[i]<h[i*2] then
fiu:=2*i;
if i*2+1<=k then
if h[fiu]<h[2*i+1] then
fiu:=2*i+1;
if fiu<>i then
begin
inv(h[i],h[fiu]);
siftdown(fiu,k);
end;
end;
begin
assign(f,'algsort.in');reset(f);
settextbuf(f,buf);
readln(f,n);
for i:=1 to n do
begin
read(f,x);
addheap(x,i);
end;
close(f);
k:=n;
for i:=1 to n do
begin
inv(h[1],h[k]);
siftdown(1,k-1);
k:=k-1;
end;
assign(g,'algsort.out');rewrite(g);
for i:=1 to n do
write(g,h[i],' ');
close(g);
end.