luogu 1090
贪心,每次取最小的两个合并即可。
优先队列,懒惰就是美德。
#include"cstdio" #include"cctype" #include"queue" using namespace std; int read() { int c,x=0; while(!isdigit(c=getchar())); while(x=x*10+c-'0',isdigit(c=getchar())); return x; } priority_queue<int,vector<int>,greater<int> > q; int main() { int n=read(),ans=0; for(int i=1; i<=n; i++) q.push(read()); for(int i=1; i<n; i++) { int w=q.top(); q.pop(); w+=q.top(); q.pop(); ans+=w; q.push(w); } printf("%d",ans); return 0; }
单调队列,开两个队列,一个放没有合并过的果子堆,另一个放合并过的果子堆。
由于合并出来的果子堆是从小到大合并出来的,所以直接放队列尾部即可。
#include"cstdio" #include"cctype" #include"algorithm" using namespace std; int read() { int c,x=0; while(!isdigit(c=getchar())); while(x=x*10+c-'0',isdigit(c=getchar())); return x; } int h,t,l=1,a[10001],q[10001]; int top(int n) { if(h==t) return a[l++]; if(l==n+1) return q[h++]; if(a[l]<q[h]) return a[l++]; else return q[h++]; } int main() { int n=read(),ans=0; for(int i=1; i<=n; i++) a[i]=read(); sort(a+1,a+n+1); for(int i=1; i<n; i++) ans+=q[t]=top(n)+top(n),t++; printf("%d",ans); return 0; }