P1090 [NOIP2004 提高组] 合并果子 / [USACO06NOV] Fence Repair G

阅读 99

2022-11-07


​​传送门​​

P1090 [NOIP2004 提高组] 合并果子 / [USACO06NOV] Fence Repair G_出队

思路:

用优先队列(从小到大排序)储存数据,然后每次取出队顶的两个元素相加再放入队中,直到队中只剩下一个元素。

#include<bits/stdc++.h>
using namespace std;
# define ll long long
priority_queue<int,vector<int>,greater<int> >a;
int main()
{
int n;
scanf("%d",&n);
for(int i = 1; i <= n; i++)
{
int b;
scanf("%d",&b);
a.push(b);
}
ll ans = 0;
while(a.size() != 1)
{
int x = a.top();
a.pop();
int y = a.top();
a.pop();
x = x + y;
a.push(x);
ans += x;
}
printf("%lld\n",ans);
}


精彩评论(0)

0 0 举报