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 ...
随机推荐
- 文字处理TX Text Control X10独家揭秘(二):图像占位符合并
在前面一篇文章<TX Text Control X10独家揭秘(一):数据源自动处理>中已经对即将发布的TX Text Control X10的数据源自动处理做了一些了解,接下来述说它的图 ...
- zabbix监控nginx
nginx status详解 active connections – 活跃的连接数量server accepts handled requests — 总共处理了11989个连接 , 成功创建11 ...
- How to create UrlSlug in Asp.Net MVC
转自:http://www.ehsanghanbari.com/Post/20/how-to-create-urlslug-in-aspnet-mvc UrlSlug Is a way of gene ...
- Hadoop集群管理之配置文件
一.配置文件列表如下: [hadoop@node1 conf]$ pwd /app/hadoop/conf [hadoop@node1 conf]$ echo $HADOOP_HOME /app/ha ...
- C#:屏幕显示区域问题
更改电脑屏幕显示的文字大小后,平面显示区域问题. /// <summary> /// 屏幕显示尺寸 /// </summary> public static Size Revi ...
- 【转】介绍设置Session失效的几种方法
转载地址:http://developer.51cto.com/art/201106/269493.htm Session对象是HttpSessionState的一个实例.该类为当前用户会话提供信息, ...
- 连接无线设备——与Wi-Fi直接连接
原文链接:http://developer.android.com/intl/zh-CN/training/connect-devices-wirelessly/wifi-direct.html 目录 ...
- Android 静默安装
有时候我们需要软件实现静默安装,但是Android并未提供相应的API,然而我们知道命令行安装android的时候是不会提示用户的,所有要实现这个功能,我们就可以从执行命令行的方式实现.android ...
- Cimg代码初探
Cimg代码初探 程序设计最为激动人心的地方,在于丰富的并且容易被查阅到资料.比如对于图像处理,固然有Opencv等较为丰富.被广泛知晓的类库:也有其他很多具有一定特色的类库.在这段时间里面, ...
- 提高 Linux 上 socket 性能
http://www.cnblogs.com/luxf/archive/2010/06/13/1757662.html 基于Linux的Socket网络编程的性能优化 1 引言 随着In ...