- 合并果子
- 2009-11-13 17:02:37 @
用堆排,谁帮我看一下,有两个点答案错误(本人菜鸟,对堆排不熟悉)
program fruit;
var
n,i,c,t:Longint;
h:array[1..maxint] of longint;
procedure heap(s,e:longint);
var min,nextstart:longint;
begin
while true do
begin
min:=h;
nextstart:=-1;
if (s*2
1 条评论
-
nipanqi LV 5 @ 2017-07-21 21:47:30
const maxn=30000;
var heap:array[1..maxn] of longint;
ans,n,len,a,b,i,tmp:longint;procedure put(x:longint);
var fa,son,tmp:longint;
begin
len:=len+1;
heap[len]:=x;
son:=len;
while (son<>1)and(heap[son div 2]>heap[son]) do
begin
tmp:=heap[son div 2];
heap[son div 2]:=heap[son];
heap[son]:=tmp;
son:=son div 2;
end;
end;
function get:longint;
var fa,son,tmp:longint;
stop:boolean;
begin
get:=heap[1];
heap[1]:=heap[len];
len:=len-1;
fa:=1; stop:=false;
while ((fa*2<=len)or(fa*2+1<=len))and(not stop) do
begin
if (fa*2+1>len)or(heap[fa*2]<heap[fa*2+1])
then son:=fa*2
else son:=fa*2+1;
if heap[fa]>heap[son]
then begin
tmp:=heap[fa];
heap[fa]:=heap[son];
heap[son]:=tmp;
fa:=son;
end
else stop:=true;
end;
end;
begin
len:=0;
read(n);
for i:=1 to n do
begin
read(tmp);
put(tmp);
end;
ans:=0;
for i:=1 to n-1 do
begin
a:=get;
b:=get;
ans:=ans+a+b;
put(a+b);
end;
writeln(ans);
end.
- 1