Pagini recente » Cod sursa (job #2377981) | Cod sursa (job #1711464) | Cod sursa (job #108625) | Cod sursa (job #1270191) | Cod sursa (job #342227)
Cod sursa(job #342227)
Program algsort;
var f,g:text; h:array[0..1000001]of longint;
i,n,nh:longint;
procedure swap (x,y:longint);
var z:longint;
begin
z:=h[x]; h[x]:=h[y]; h[y]:=z;
end;
procedure upheap (x:longint);
begin
if h[x]<h[x div 2] then begin
swap (x,x div 2);
upheap (x div 2);
end;
end;
procedure downheap (x:longint);
var y:longint;
begin
if 2*x<=nh then begin
y:=x;
if h[y]>h[2*x] then y:=2*x;
if 2*x+1<=nh then if h[y]>h[2*x+1] then y:=2*x+1;
if x<>y then begin
swap (x,y);
downheap (y);
end;
end;
end;
procedure delete;
begin
swap (1,nh);
dec (nh);
downheap (1);
end;
begin
assign (f,'algsort.in'); reset (f);
assign (g,'algsort.out'); rewrite (g);
readln (f,n);
for i:=1 to n do read (f,h[i]);
for i:=1 to n do dec (h[i],1);
h[0]:=-1;
for i:=n+1 to 1000001 do h[i]:=maxlongint;
nh:=n;
for i:=(nh div 2) downto 1 do downheap (i);
for i:=n downto 2 do delete;
for i:=n downto 2 do write (g,h[i]+1,' ');
writeln (g,h[1]);
close (f); close (g);
end.