优先队列的力量

P1090 [NOIP2004 提高组] 合并果子 / [USACO06NOV] Fence Repair G

```cpp //pascal var a:array[0..300000]of longint; n,i,j,x,y,s,l:longint; procedure put(x:longint); var f,s,t:longint; begin inc(l); a[l]:=x; s:=l; f:=s shr 1; while f>0 do begin if a[f]>a[s] then begin t:=a[f]; a[f]:=a[s]; a[s]:=t; end else break; s:=f; f:=s shr 1; end; end; function get:longint; var f,s,t:longint; begin get:=a[1]; a[1]:=a[l]; dec(l); f:=1; if a[f shl 1]<a[f shl 1 or 1] then s:=f shl 1 else s:=f shl 1 or 1; while s<=l do begin if a[f]>a[s] then begin t:=a[f]; a[f]:=a[s]; a[s]:=t; end else break; f:=s; if a[f shl 1]<a[f shl 1 or 1] then s:=f shl 1 else s:=f shl 1 or 1; end; end; begin readln(n); l:=0; for i:=1 to n do begin read(x); put(x); end; s:=0; for i:=n downto 2 do begin x:=get; y:=get; inc(s,x+y); put(x+y); end; writeln(s); end. ```
by ghd123 @ 2016-11-14 19:08:46


```cpp //C++ #include <bits/stdc++.h> using namespace std; priority_queue< int,vector<int>,greater<int> >q; int n,x,i,s; int main() { scanf("%d",&n); for (i=1;i<=n;i++) { scanf("%d",&x); q.push(x); } for (i=1;i<n;i++) { x=q.top(); q.pop(); x+=q.top(); q.pop(); s+=x; q.push(x); } printf("%d\n",s); return 0; } ```
by ghd123 @ 2016-11-14 19:09:26


STL万岁
by 坚决杀毒2008 @ 2016-11-14 19:10:17


不要在讨论区发题解好么QWQ
by Thecats @ 2016-11-15 07:56:29


%%%
by taoxinyue2016 @ 2016-12-05 17:03:32


###**\_。。。。。。\_**
by 天橙降临 @ 2016-12-18 21:57:53


|