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. Hive 启动报错,需先启动元数据

    Exception in thread "main" java.lang.RuntimeException: java.lang.RuntimeException: Unable ...

  2. Leetcode 373.查找和最小的k对数字

    查找和最小的k对数字 给定两个以升序排列的整形数组 nums1 和 nums2, 以及一个整数 k. 定义一对值 (u,v),其中第一个元素来自 nums1,第二个元素来自 nums2. 找到和最小的 ...

  3. ER模型图工具:PowerDesigner

    导读:开始是想一个阶段一个阶段的写文档,再写了可行性分析报告之后,发现这种方法整体性不强,写出来的文档也总是有考虑不周的地方.后来,就看完了全部的软工视频再写.现在该写项目开发总结报告了.在写文档的过 ...

  4. 有关git的配置

    git版本控制器总结 关于部分内容请参考:https://www.cnblogs.com/smuxiaolei/p/7484678.htmlgit是一个版本控制器,分布式管理:可以记录每次文件的改动, ...

  5. Python之静态语法检查

    Python是一门动态语言.在给python传参数的时候并没有严格的类型限制.写python程序的时候,发现错误经常只能在执行的时候发现.有一些错误由于隐藏的比较深,只有特定逻辑才会触发,往往导致需要 ...

  6. java面试题之hashcode相等两个类一定相等吗?equals呢?相反呢?

    答:hashcode相等,两个类不一定相等,equals也不一定相等: 反过来,equals相等,hashcode一定相等

  7. bzoj[HNOI2015]亚瑟王 - 递推与动规 - 概率与期望

    [bzoj4008][HNOI2015]亚瑟王 2015年4月22日3,2991 Description 小 K 不慎被 LL 邪教洗脑了,洗脑程度深到他甚至想要从亚瑟王邪教中脱坑. 他决定,在脱坑之 ...

  8. phoenixframework集成了所有自动化测试的思想的平台。mark一下。

    phoenixframework http://www.cewan.la/

  9. maven配置中国下载源【转:http://www.cnblogs.com/libingbin/p/5949483.html】

    修改 配置文件 maven 安装 路径 F:\apache-maven-3.3.9\conf 修改 settings.xml或者在.m2文件夹下新建一个settings.xml 阿里源 <mir ...

  10. 怎么用SQL语句查数据库中某一列是否有重复项

    SELECT 某一列, COUNT( 某一列 ) FROM 表 GROUP BY 某一列 HAVING COUNT( 某一列 ) 〉1 这样查询出来的结果, 就是 有重复, 而且 重复的数量.