首页 > 其他 > 详细

BZOJ 3253 Fence Repair 哈夫曼树 水题

时间:2018-06-01 22:37:26      阅读:180      评论:0      收藏:0      [点我收藏+]

http://poj.org/problem?id=3253

这道题约等于合并果子,但是通过这道题能够看出来哈夫曼树是什么了。

技术分享图片
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<cmath>
 5 #include<iostream>
 6 #include<queue>
 7 using namespace std;
 8 #define LL long long
 9 const int maxn=300010;
10 int n;
11 priority_queue< LL,vector<LL>,greater< LL > >q;
12 int main(){
13     while(~scanf("%d",&n)){
14         LL x,y;
15         for(int i=1;i<=n;i++){scanf("%lld",&x);q.push(x);}
16         LL ans=0;
17         while(!q.empty()){
18             x=q.top();q.pop();
19             if(q.empty())continue;
20             y=q.top();q.pop();
21             ans+=x+y;
22             q.push(x+y);
23         }
24         printf("%lld\n",ans);
25         while(!q.empty())q.pop();
26     }
27     return 0;
28 }
View Code

 

BZOJ 3253 Fence Repair 哈夫曼树 水题

原文:https://www.cnblogs.com/137shoebills/p/9123718.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!