poj3253
一道赫夫曼树的经典题目,一直以为这题的代码会很复杂,没想到书中竟描述地如此简单
#include <stdio.h>
int n;
long long p[20010];
//一道经典的赫夫曼编码题
void swap(int &min1,int& min2)
{
int d;
d=min1;
min1=min2;
min2=d;
}
int main()
{
scanf("%d",&n);
int i;
for(i=1;i<=n;i++)
scanf("%lld",&p[i]);
if(n==2||n==1)
{
long long sum=0;
for(i=1;i<=n;i++)
sum+=p[i];
printf("%lld\n",sum);
return 0;
}
long long ans=0;
while(n>1)
{
int min1=1,min2=2;
if(p[min1]>p[min2])
swap(min1,min2);
for(i=3;i<=n;i++)
{
if(p[i]<p[min1])
{
min2=min1;
min1=i;
}
else if(p[i]<p[min2])
min2=i;
}
long long t=p[min1]+p[min2];
ans+=t;
if(min1==n)swap(min1,min2);
p[min1]=t;
p[min2]=p[n];
//p[min1]=t;
//if(min1!=n)swap(min2,n);
n--;
}
printf("%lld\n",ans);
return 0;
}
poj3253的更多相关文章
- POJ3253 Haffman
POJ3253 分析: 简单的哈弗曼树的应用. AC代码: //Memory: 316K Time: 16MS #include <iostream> #include <cstri ...
- POJ-3253 Fence Repair---Huffman贪心
题目链接: https://vjudge.net/problem/POJ-3253 题目大意: 有一个农夫要把一个木板钜成几块给定长度的小木板,每次锯都要收取一定费用,这个费用就是当前锯的这个木版的长 ...
- poj3253 Fence Repair(贪心+哈夫曼 经典)
https://vjudge.net/problem/POJ-3253 很经典的题,运用哈夫曼思想,想想很有道理!! 具体实现还是有点绕人,最后被long long卡了一下,看数据大小的时候单纯相乘了 ...
- poj3253 Fence Repair
http://poj.org/problem?id=3253 Farmer John wants to repair a small length of the fence around the pa ...
- 哈夫曼树---POJ3253
http://poj.org/problem?id=3253 这就是 最典型的哈夫曼树的题型,我们就根据这道题学习一下哈夫曼树 这是最开始我们把21据下来之后我们据下8,然后再据下5得到34,可以看出 ...
- POJ3253 Fence Repair(贪心)
分割木板的顺序是自由的,所以每次选择两块最短的板,组合在一起,增加队列,原来两个板出队,直到队列中为空或者仅仅剩下一个板时结束.这里使用优先队列较为方便. #include<iostream&g ...
- 优先队列 poj3253 Fence Repair
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 51411 Accepted: 16879 De ...
- Fence Repair(poj3253)
题目链接:http://poj.org/problem?id=3253 Description Farmer John wants to repair a small length of the fe ...
- Poj3253 Fence Repair (优先队列)
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 67319 Accepted: 22142 De ...
随机推荐
- Redis分布式部署,一致性hash
一致性哈希 由于hash算法结果一般为unsigned int型,因此对于hash函数的结果应该均匀分布在[0,2^32-1]区间,如果我们把一个圆环用2^32 个点来进行均匀切割,首先按照hash( ...
- struts2介绍
struts2简介 Struts2框架发展 Struts于2000年5月由Craig McClanahan发起,并于 2001年7月发布了1.0版本,Struts一出现便大受欢迎,更成为了以后几年内w ...
- Oracle无法启动,ORA-01034、ORA-01078
因为调整32位系统的SGA区大小时不慎,超出可用内存,造成Oracle实例无法启动,报出ORA-01034.ORA-01078等错误.如下图 sqlplus /nolog SQL> conn / ...
- oracle 自定义异常处理
--第一种方式:使用raise_application_error抛出自定义异常declare i number:=-1;begin if i=-1 then raise_application_er ...
- python数据分析之pandas库的Series应用
一.pandas的数据结构介绍 1. Series 1.1 Series是由一种类似于一维数组的对象,它由一组数据以及一组与之相关的数据索引构成.仅由一组数据可产生最简单的Series. from p ...
- window.open窗口关闭后刷新父窗口代码
window.open窗口关闭后刷新父窗口代码 window.opener.location.href=window.opener.location.href;window.close();
- [Hibernate] - many to many
Hibernate的多对多实现: hibernate.cfg.xml <?xml version="1.0" encoding="UTF-8"?> ...
- MATLAB绘 透视图
MATLAB绘图随记(1)--如何画一个透明平面 http://blog.sina.com.cn/s/blog_5cd4cccf0100q90p.html 小老板让我绘个图 找了些资料 最后发现mat ...
- php xdebug xampp eclipse
Eclipse配置 一:配置workspace 打开Eclipse for PHP Developers,需要设置workspace,这个必须设置到C:\xampp\htdocs目录,否则待会无法进行 ...
- 清理SQL Server日志释放文件空间的终极方法
清理SQL Server日志释放文件空间的终极方法 转自:http://www.cnblogs.com/dudu/archive/2013/04/10/3011416.html [问题场景]有一个数 ...