题目链接:http://poj.org/problem?id=3253
分析:每次找到最小的两根接到一起,将其放回,然后再选两个最小的接到一起,又放回....直到最后成了一根!!!
#include<iostream> #include<string> #include<cstring> #include<algorithm> #include<cstdio> #include<cmath> #include<iomanip> #include<queue> using namespace std; const int maxn=1000000; struct node { __int64 k; friend bool operator < (node x,node y) { return x.k>y.k; } } f[maxn],rt; priority_queue<node>M; int main() { int n; cin>>n; while(!M.empty())M.pop(); for(int i=0; i<n; ++i) { cin>>f[i].k; M.push(f[i]); } __int64 sum=0; while(M.size()>1) { __int64 x,y; x=M.top().k; M.pop(); y=M.top().k; M.pop(); sum+=x+y; rt.k=x+y; M.push(rt); } cout<<sum<<endl; return 0; }