POJ 3253 Fence Repair(修篱笆)
POJ 3253 Fence Repair(修篱笆)
Time Limit: 2000MS Memory Limit: 65536K
|
【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. |
农夫约翰打算修理下围绕牧场的一小段篱笆。他量了量篱笆发觉需要N(1 ≤ N ≤ 20,000)块木板,每块木板分别需要Li(1 ≤ Li ≤ 50,000)整的单位长度。然后他买了块长到可以切出这N块木板的长木板(换句话说,它的长度是Li的总和)。FJ会忽略锯开木板时变成木屑的“锯痕”;所以你也应该忽略它。 FJ悲伤地发现自己没有锯子用来据木头,因此他带着长木板去农夫Don的农场借去了。 怎料农夫Don是个抠门的资本家,并不想借给FJ,但可以有偿帮他切N-1次木板。切木板的费用与木板长度相等。却一块长21的木板需要花21美分。 农夫Don让农夫约翰自己决定怎么切。请帮农夫约翰确定完成这N块板后的最小费用。FJ知道使用不同切割方式会造成不同的花费,因为中途的木板长度不同。 |
|
【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 |
第1行:一个整数N,表示木板的数量。 第2~N+1行:每行一个整数,表示需要木板的长度。 |
|
【Output】 |
【输出】 |
|
Line 1: One integer: the minimum amount of money he must spend to make N-1 cuts |
第一行:约翰进行N-1次切割的最小花费 |
|
【Sample Input - 输入样例】 |
【Sample Output - 输出样例】 |
|
3 8 5 8 |
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的长木板切成8,5,和8. 初始木板的长度为8+5+8=21。初次切割花费21,然后应被切成13与8。第二次切割花费13,然后应被切成8和5。此时的花费是21+13=34。如果21被改切成16和5,第二次切割花费16后总费用为37(这样比34来得大)。 |
【题解】
贪心plus,用哈夫曼树的合并思想。
一开始想先把最短的板分出去,不过算了算结果好像不对。
应该把一块长板分成尽可能长的两块短板,之后逆向实现就好了。

需要这里要注意的是数据会超int
【代码 C++】
#include<cstdio>
#include<queue>
#include<functional>
std::priority_queue<int, std::vector<__int64>, std::greater<int> > data;
int main(){
int n, i;
__int64 temp, opt;
scanf("%d", &n);
for (i = opt = ; i < n; ++i){
scanf("%I64d", &temp);
data.push(temp);
}
if (n == ) printf("%I64d", data.top());
else{
for (i = ; i < n; ++i){
temp = data.top(); data.pop();
temp += data.top(); data.pop();
opt += temp; data.push(temp);
}
printf("%I64d", opt);
}
return ;
}
POJ 3253 Fence Repair(修篱笆)的更多相关文章
- POj 3253 Fence Repair(修农场栅栏,锯木板)(小根堆 + 哈弗曼建树得最小权值思想 )
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 28359 Accepted: 9213 Des ...
- POJ 3253 Fence Repair (优先队列)
POJ 3253 Fence Repair (优先队列) Farmer John wants to repair a small length of the fence around the past ...
- poj 3253 Fence Repair 优先队列
poj 3253 Fence Repair 优先队列 Description Farmer John wants to repair a small length of the fence aroun ...
- POJ 3253 Fence Repair (贪心)
Fence Repair Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
- POJ 3253 Fence Repair 贪心 优先级队列
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 77001 Accepted: 25185 De ...
- poj 3253 Fence Repair
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 42979 Accepted: 13999 De ...
- poj 3253:Fence Repair(堆排序应用)
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 23913 Accepted: 7595 Des ...
- poj 3253 Fence Repair(优先队列+哈夫曼树)
题目地址:POJ 3253 哈夫曼树的结构就是一个二叉树,每个父节点都是两个子节点的和. 这个题就是能够从子节点向根节点推. 每次选择两个最小的进行合并.将合并后的值继续加进优先队列中.直至还剩下一个 ...
- POJ 3253 Fence Repair (哈夫曼树)
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 19660 Accepted: 6236 Des ...
随机推荐
- Ubuntu1404: 将VIM打造为一个实用的PythonIDE
参考: http://www.tuicool.com/articles/ZRv6Rv 说明: 内容非原创, 主要是做了整合和梳理. 在 ubuntu14.04 & debian 8 下测试通 ...
- 记录下标准网线水晶头的做法 100m/1G
注意, 网线分平行线(也叫直连线)和交叉线. 用法见下图 .也就是相同层内用交叉线, 不同层用平行线. 所以两台电脑,两台非级联并行的路由才用交叉线,一般我们都用平行线即可. 千兆网和百兆网不同: ...
- 、web前端的这么知识应该是怎样的一个知识体系架构?
.web前端的这么知识应该是怎样的一个知识体系架构?之前我以为可以以W3C为纲要,把W3C的东西学会了就够了.后来发现我错了,W3C还不全面. 真正全面的覆盖了web前端知识体系的东西是——浏览器内核 ...
- Android 常用工具类之LogUtil,可以定位到代码行,双击跳转
package cn.utils; import android.util.Log; public class LogUtils { public static boolean isDebug = t ...
- 非常好!!!【从头开始写操作系统系列】实现一个-GDT(1)【转】
转自:http://blog.csdn.net/luoyhang003/article/details/47338019 权声明:本文为博主原创文章,未经博主允许不得转载.(文章来源:http://b ...
- altera soc体验之旅 FPGA与ARM的窃窃私语
喜大普奔,公司要评估用SOC做产品,我就自然而然的被安排了学习和评估的工作,于是,每天的工作就是开始研究soc了.其实,只要能静下心来学习,一切都还是能够弄出来的. 以前像个无头苍蝇一样到处乱撞, ...
- Android如何区分app原生和webview实现
在开发Android app时,特别是强内容展现型的功能,会想在原生native实现和web实现中做选择,做这种选择的时候,难免想看看竞品或其它app类似功能是用哪种方式实现的.但是如何判断其它app ...
- [ios]app后台运行
参考:http://www.douban.com/note/375127736/ 1 使用开源代码MMPDeepSleepPreventer将文件加入工程,包括音频文件.可以在源文件中加入单例,便于使 ...
- SlickGrid example 3b: 支持撤销操作的编辑单元
不同类型的属性可以按不同的风格编辑. 每个编辑单元可以设置不同的验证方法. 历史编辑可以撤销. 代码: <!DOCTYPE html PUBLIC "-//W3C//DTD HTM ...
- Python基本概念及零碎知识点
1.python面向对象 类和对象是面向对象编程的两个主要方面.类创建一个新类型,而对象这个类的实例:这类似于你有一个int类型的变量,这存储整数的变量是int类的实例(对象)把握一点:在python ...