我说的是最大堆(即从大到小排)
初始堆即最大的元素在第一个,其他元素任意排(但是满足父节点大于子节点)
var i,n:longint;
a:array[1..1000] of longint;
procedure ex(var x,y:longint);
var k:longint;
begin
k:=x;x:=y;y:=k;
end;
procedure down(i,l:longint);
var t:longint;
begin
t:=i*2;
while ta[i] then
begin
ex(a[t],a[i]);
i:=t;t:=i*2;
end
else break;
end;
end;
begin
readln(n);
for i:=1 to n do read(a[i]);
readln;
for i:=n div 2 downto 1 do down(i,n);
for i:=n downto 2 do
begin
write(a[1],' ');
a[1]:=a[i];
down(1,i-1);
end;
writeln(a[1]);
end.