
#include<iostream>
#include<queue>
#include<algorithm>
#include<vector>
using namespace std;
int n;
int main()
{
cin>>n;
priority_queue<int,vector<int>,greater<int>>heap;
for(int i=0;i<n;i++)
{
int x;
cin>>x;
heap.push(x);
}
int res=0;
while(heap.size()>1)
{
int a=heap.top();heap.pop();
int b=heap.top();heap.pop();
res+=a+b;
heap.push(a+b);//利用贪心的思想
}
cout<<res<<endl;
return 0;
}
本文介绍了一个使用最小堆和贪心算法解决特定问题的C++程序实例。该程序读取一组整数,通过构建最小堆来求解最优解。在每次迭代中,程序从堆中取出两个最小元素进行操作,然后将结果重新插入堆中,直至堆中只剩下一个元素,该元素即为最终结果。
2359

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



