您的位置:首页 > 其它

哈夫曼编码(Add All,uva 10954)

2016-09-12 17:32 399 查看
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

struct cmp
{
bool operator () (ll a,ll b)
{
return a>b;
}
};

int main()
{
ll N;
while(scanf("%lld",&N)==1&&N)
{
priority_queue<ll,vector<ll>,cmp>q;
ll temp;
for(ll i=1;i<=N;i++)
{
scanf("%lld",&temp);
q.push(temp);
}
ll ans=0;
while(q.size()>=2)
{
ll a=q.top();
q.pop();
ll b=q.top();
q.pop();
ll c=a+b;
ans+=c;
q.push(c);
}
printf("%lld\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: