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 ...
随机推荐
- 统一使用GPT分区表,安装MAC 10.10 和 Win8.1 pro双系统
步骤一: 为Mac OS 分区,为其它分区留白1,使用OSX Mavericks制作的Mac安装U盘按住Option键启动:2,选择安装Mavericks盘符:3,进入OSX安装启动界面,选择磁盘工具 ...
- xxxx
http://www.cnblogs.com/zhuojun/p/5747999.html
- MySQL OnlineDDL
参考资料: http://dev.mysql.com/doc/refman/5.6/en/innodb-create-index-overview.html http://www.mysqlperfo ...
- 基于mjpg_streamer视频服务器移植【转】
本文转载自:http://blog.csdn.net/wavemcu/article/details/7539560 MJPG简介: MJPG是MJPEG的缩写,但是MJPEG还可以表示文件格式扩展名 ...
- 161201、常用 SQL Server 规范集锦
常见的字段类型选择 1.字符类型建议采用varchar/nvarchar数据类型 2.金额货币建议采用money数据类型 3.科学计数建议采用numeric数据类型 4.自增长标识建议采用bigi ...
- Python 深拷贝和浅拷贝
Python中,对象的赋值,拷贝(深/浅拷贝)之间是有差异的,如果使用的时候不注意,就可能产生意外的结果. 下面本文就通过简单的例子介绍一下这些概念之间的差别. 对象赋值 直接看一段代码: will= ...
- rtc关机闹钟6 AlarmManagerService研究
这个是 private void setLocked(int type, long when) { if (mNativeData != 0) { // The kernel n ...
- YTU 2973: C语言习题5.25--文件操作2
2973: C语言习题5.25--文件操作2 时间限制: 1 Sec 内存限制: 128 MB 提交: 242 解决: 105 题目描述 文本文件score.dic 中存储了n名学生的信息(班级编 ...
- PHP判断键值数组是否存在,使用empty或isset或array_key_exists
<?php $a = array('a'=>1, 'b'=>0, 'c'=>NULL); echo 'a test by empty: ' , empty($a['a']) ...
- Webstrom快捷键大全
20:32:59 Ctrl+/ 或 Ctrl+Shift+/ 注释(// 或者 ) Shift+F6 重构-重命名 Ctrl+X ...