uva 10954——Add All
生活随笔
收集整理的这篇文章主要介绍了
uva 10954——Add All
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
<p>题意:给定一个序列,然后从中选择两个数,相加后放入原来的序列,消耗的费用为两个数 的和,问最小的代价。</p><p>
</p><p>思路:贪心。用优先队列维护,每次取得时候都取最小的两个即可。</p><p>
</p><p>code:</p>
#include <bits/stdc++.h>
using namespace std;int main()
{int n,x;while (~scanf("%d",&n)&&n){priority_queue<int,vector<int>,greater<int> >q;for (int i=0;i<n;i++)scanf("%d",&x),q.push(x);int ans=0;for (int i=0;i<n-1;i++){int a=q.top();q.pop();int b=q.top();q.pop();ans+=a+b;q.push(a+b);}printf("%d\n",ans);}
}
总结
以上是生活随笔为你收集整理的uva 10954——Add All的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: CodeForces 501B——Mis
- 下一篇: uva 1152 ——4 Values