POJ 3253 Fence Repair
2016-04-03 20:43
309 查看
Fence Repair
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
Sample Output
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).
题意看之前写的贪心里的,一样的题,只是现在使用优先队列解答
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 37154 | Accepted: 12033 |
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).
题意看之前写的贪心里的,一样的题,只是现在使用优先队列解答
#include <cstdio> #include <queue> using namespace std; typedef long long ll; int n; int L[20001]; void solve() { ll ans = 0; priority_queue<int, vector<int>, greater<int> > que; //声明一个从小到大取出数值的优先队列 for (int i = 0; i < n; i++) que.push(L[i]); while (que.size() > 1){ //循环到只剩一块木板为止 int l1, l2; //取出最短的木板和次短的木板 l1 = que.top(); que.pop(); l2 = que.top(); que.pop(); ans += l1 + l2; //把两块木板合并 que.push(l1 + l2); } printf("%lld\n", ans); } int main() { while (scanf("%d", &n) != EOF){ for (int i = 0; i < n; i++){ scanf("%d", &L[i]); } solve(); } return 0; }
相关文章推荐
- CSS中的长度值
- JavaScript之变量与常量
- js防刷新的倒计时
- JavaScript编写人机对战五子棋(一)
- JSP课程设计感悟
- 初识angularJs
- Liferay 6.1开发学习(二):创建一个Portlet工程
- Liferay 6.1开发学习(一):环境搭建
- JavaScript之jQuery-10 T-Query(T-Query、基本选择器、T-Query对象、操作方法、创建元素、事件绑定)
- Javascript中数组的常用方法。
- JavaScript版HashMap的简单实现——通过原型prototype扩展
- jquery实现ajax实例
- 【Caffe实践】基于Caffe的人脸识别实现
- UVA - 1471 Defense Lines 树状数组/二分
- 【Caffe实践】基于Caffe的人脸检测实现
- HTML框架
- 关于js中的原型
- 【笔记】 《js权威指南》- 第9章 类和模块 - 9.5 类和类型
- js+json实现ajax实例
- HTML 兼容性