#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
scanf("%d",&n);
priority_queue<int,vector<int>,greater<int> > q;
int cou[150],lhf=0;
memset(cou,0,sizeof(cou));
while(n--)
{
int t;
scanf("%d",&t);
q.push(t);
}
while(!q.empty())
{
int m=q.top();
q.pop();
if(!q.empty())
{
int n=q.top();
q.pop();
int p=m+n;
lhf+=p;
q.push(p);
}
}
printf("%d",lhf);
}
SDUT2127树-堆结构练习——合并果子之哈夫曼树
最新推荐文章于 2024-04-05 21:13:12 发布
本文介绍了一个使用优先队列实现的高效算法,该算法通过不断合并两个最小元素来减少总的操作次数。具体实现中利用了C++标准库中的priority_queue,并结合了自定义的比较器以实现小顶堆的功能。
1334

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



