Description

Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000) planks of wood, each having some integer length Li (1 ≤ Li ≤
50,000) units. He then purchases a single long board just long enough to saw into the N planks (i.e., whose length is the sum of the lengths Li). FJ is ignoring the "kerf", the extra length lost to sawdust when a sawcut is made;
you should ignore it, too.

FJ sadly realizes that he doesn't own a saw with which to cut the wood, so he mosies over to Farmer Don's Farm with this long board and politely asks if he may borrow a saw.

Farmer Don, a closet capitalist, doesn't lend FJ a saw but instead offers to charge Farmer John for each of the N-1 cuts in the plank. The charge to cut a piece of wood is exactly equal to its length. Cutting a plank of length 21 costs 21 cents.

Farmer Don then lets Farmer John decide the order and locations to cut the plank. Help Farmer John determine the minimum amount of money he can spend to create the N planks. FJ knows that he can cut the board in various different orders which will
result in different charges since the resulting intermediate planks are of different lengths.

Input

Line 1: One integer N, the number of planks 

Lines 2..N+1: Each line contains a single integer describing the length of a needed plank

Output

Line 1: One integer: the minimum amount of money he must spend to make N-1 cuts

Sample Input

3
8
5
8

Sample Output

34

Hint

He wants to cut a board of length 21 into pieces of lengths 8, 5, and 8.

The original board measures 8+5+8=21. The first cut will cost 21, and should be used to cut the board into pieces measuring 13 and 8. The second cut will cost 13, and should be used to cut the 13 into 8 and 5. This would cost 21+13=34. If the 21 was cut
into 16 and 5 instead, the second cut would cost 16 for a total of 37 (which is more than 34).

//这是比赛时写的代码,答案尽管对的。但有问题

//不是把一个长度为21的砍开吗?我就想着一个開始是21。先砍成8和13,须要21,然后再把13砍成8和5,须要13  13+21=34;然后我就把数组里的素

//从大到小排列,为的是21加的更少,花的金币就更少。就从大到小排列,

//然而错了n次,比赛结束后我看了看别人的代码,是哈夫曼树+优先队列,每次取两最小的数。粘成一个长的,每次都这样

//但我不知道我的代码错在哪

#include<cstdio>
#include<algorithm>
using namespace std;
int cmp(__int64 x,__int64 y)
{
return x>y;
}
__int64 a[20100];
int main()
{
int n,i;
while(~scanf("%d",&n))
{
__int64 s=0;
for(i=0;i<n;++i)
{
scanf("%I64d",a+i);
s+=a[i];
}
sort(a,a+n,cmp);
__int64 sum=0;
for(i=0;i<n;++i)
{
if(s!=a[n-1])
{
sum+=s;
s-=a[i];
}
}
printf("%I64d\n",sum);
}
}

/*
题意:将一块木块砍一刀,当前木块是多长就须要多少金币,
求花最少的金币砍成你想要的木块长度
*/
//哈夫曼树+优先队列 poj 3253 //每次实现最小的两个数相加
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std; int main()
{
int n,i;
__int64 a,b;
__int64 m;
scanf("%d",&n);
{
priority_queue<__int64,vector<__int64>,greater<__int64> >q; for(i=0;i<n;++i)
{
scanf("%I64d",&m);
q.push(m);
}
__int64 sum=0;
while(q.size()>1)
{
a=q.top();
q.pop();
b= q.top();
q.pop();
sum+=a+b;
q.push(a+b);
}
printf("%I64d\n",sum);
}
}

poj3253 Fence Repair【哈夫曼树+优先队列】的更多相关文章

  1. poj 3253 Fence Repair (哈夫曼树 优先队列)

    题目:http://poj.org/problem?id=3253 没用long long wrong 了一次 #include <iostream> #include<cstdio ...

  2. Poj 3253 Fence Repair(哈夫曼树)

    Description Farmer John wants to repair a small length of the fence around the pasture. He measures ...

  3. BZOJ 3253 Fence Repair 哈夫曼树 水题

    http://poj.org/problem?id=3253 这道题约等于合并果子,但是通过这道题能够看出来哈夫曼树是什么了. #include<cstdio> #include<c ...

  4. 【PTA 天梯赛训练】修理牧场(哈夫曼树+优先队列)

    农夫要修理牧场的一段栅栏,他测量了栅栏,发现需要N块木头,每块木头长度为整数L​i​​个长度单位,于是他购买了一条很长的.能锯成N块的木头,即该木头的长度是L​i​​的总和. 但是农夫自己没有锯子,请 ...

  5. POJ 3253 Fence Repair【哈弗曼树/贪心/优先队列】

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 53645   Accepted: 17670 De ...

  6. POJ 3253 Fence Repair(哈夫曼编码)

    题目链接:http://poj.org/problem?id=3253 题目大意: 有一个农夫要把一个木板钜成几块给定长度的小木板,每次锯都要收取一定费用,这个费用就是当前锯的这个木版的长度 给定各个 ...

  7. POJ 3253 Fence Repair(简单哈弗曼树_水过)

    题目大意:原题链接 锯木板,锯木板的长度就是花费.比如你要锯成长度为8 5 8的木板,最简单的方式是把21的木板割成13,8,花费21,再把13割成5,8,花费13,共计34,当然也可以先割成16,5 ...

  8. [tree]合并果子(哈夫曼树+优先队列)

    现在有n堆果子,第i堆有ai个果子.现在要把这些果子合并成一堆,每次合并的代价是两堆果子的总果子数.求合并所有果子的最小代价. Input 第一行包含一个整数T(T<=50),表示数据组数. 每 ...

  9. POJ 3253 Fence Repair(优先队列,哈夫曼树,模拟)

    题目 //做哈夫曼树时,可以用优先队列(误?) //这道题教我们优先队列的一个用法:取前n个数(最大的或者最小的) //哈夫曼树 //64位 //超时->优先队列,,,, //这道题的优先队列用 ...

随机推荐

  1. H5系列之地理位置(必知必会)

    H5之地理位置必知必会     [02]概念   规范地址:http://www.w3.org/TR/geolocation-API/     HTML5 Geolocation(地理定位)用于定位用 ...

  2. Wp 导航到手机定位设置页面

    WP开放了很多选择器和启动器,找了半天没发现有打开定位设置页面的,找了好久终于找到了解决办法: await Windows.System.Launcher.LaunchUriAsync(new Uri ...

  3. CentOS6配置静态IP

    CentOS6配置静态IP 1. 编辑网卡配置 vi /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 HWADDR=不需要改 TYPE=Et ...

  4. Builder(构造者)

    Builder(构造者) <?php class Product { private $name; public function setName($name) { $this->name ...

  5. JS事件兼容性

    事件代理的时候,使用事件对象中的srcElement属性,获取触发元素.IE浏览器支持window.event.srcElement , 而firefox支持window.event.target. ...

  6. Mysql 主外键与索引之间的区别和联系

    系数据库依赖于主键,它是数据库物理模式的基石.主键在物理层面上只有两个用途: 惟一地标识一行. 作为一个可以被外键有效引用的对象. 索引是一种特殊的文件(InnoDB数据表上的索引是表空间的一个组成部 ...

  7. 【Luogu】P3708Koishi的数字游戏(数论)

    题目链接 考虑f(i)=i%1+i%2+i%3+.....+i%n f(i+1)=(i+1)%1+(i+1)%2+......+(i+1)%n 其中不是i+1的因数的部分在f(i+1)的地方都加了1. ...

  8. [BZOJ3054] Rainbow的信号(考虑位运算 + DP?)

    传送门 BZOJ没数据范围... 其实数据范围是这样的.. 前20%可以直接n^3暴力枚举每个区间 前40%可以考虑每一位,因为所有数每一位都是独立的,而和的期望=期望的和,那么可以枚举每一位,再枚举 ...

  9. 刷题总结——拦截导弹(ssoj)

    题目: 题目背景 NOIP1999 提高组试题 题目描述 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都 ...

  10. C++ 中头文件(.h)和源文件(.cc)的写法简述

    用C++编写比较大型的项目时,文件的分割管理确实确实是非常必要的 .下面就非常简洁明了地谈谈头文件(.h)和源文件(.cc)应该怎么写. 头文件(.h):写类的声明(包括类里面的成员和方法的声明).函 ...