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