当前位置: 代码迷 >> 综合 >> 紫书 例题8-11 UVa 10954 (优先队列)
  详细解决方案

紫书 例题8-11 UVa 10954 (优先队列)

热度:34   发布时间:2023-09-20 22:14:17.0
解法和合并果子是一样的, 每次取最小的两个, 更新答案, 加入队列

#include<cstdio>
#include<queue>
#define REP(i, a, b) for(int i = (a); i < (b); i++)
using namespace std;int main()
{int n, x;while(~scanf("%d", &n) && n){priority_queue<int, vector<int>, greater<int> > q;REP(i, 0, n) scanf("%d", &x), q.push(x);int ans = 0;REP(i, 0, n - 1){int a = q.top(); q.pop();int b = q.top(); q.pop();ans += a + b;q.push(a+b);}printf("%d\n", ans);}return 0;	
}