贪心水题,用优先队列水过
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
long long ans,tll;
priority_queue<long long, vector<long long>, greater<long long> > q;
int main()
{
int n,i,j,t;
while (!q.empty())
q.pop();
scanf("%d",&n);
for (i=0; i<n; i++)
{
scanf("%d",&t);
q.push(t);
}
ans=0;
while (q.size() >= 2)
{
tll=q.top();
q.pop();
tll+=q.top();
ans+=tll;
q.pop();
q.push(tll);
}
printf("%lld\n",ans);
q.pop();
return 0;
}
本文介绍了一个基于贪心算法的问题解决方案,使用优先队列实现。通过不断合并最小的两个元素并将其重新插入队列中,直到队列中只剩下一个元素为止。此过程用于计算最优解,适用于诸如计算最小成本等问题。
192

被折叠的 条评论
为什么被折叠?



