poj 3253 Fence Repair
2013-11-18 17:26
363 查看
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).
这是个贪心题吧。
假设要切成a1,a2,a3,a4,a5,a6,a7;7块木板
那么首先要切成 (a1,a2),a3,a4,a5,a6,a7;6块木板
但 这个过程费用怎样才能最低呢。 当然a1,a2 要是最短的两块木板,
这样就讲n块木板的合并问题 转化为 n-1块木板的合并问题 当合并成最后一块的时候 任务就完成了
其实这道题 还有别的解法 比如哈夫曼树。 下次写的时候再贴上来。
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 21203 | Accepted: 6759 |
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).
这是个贪心题吧。
假设要切成a1,a2,a3,a4,a5,a6,a7;7块木板
那么首先要切成 (a1,a2),a3,a4,a5,a6,a7;6块木板
但 这个过程费用怎样才能最低呢。 当然a1,a2 要是最短的两块木板,
这样就讲n块木板的合并问题 转化为 n-1块木板的合并问题 当合并成最后一块的时候 任务就完成了
#include<iostream> #include<algorithm> using namespace std; typedef long long ll; int min1,min2,n,t; int aa[20010]; ll sum; int main() { int i; while(cin>>n) { sum=0; for(i=0;i<n;i++){ cin>>aa[i]; } while(n>1) { min1=0;min2=1; if(aa[min1]>aa[min2]){ min1=1;min2=0;} for(i=2;i<n;i++){ if(aa[i]<aa[min1]) {min2=min1; min1=i;} else if(aa[i]<aa[min2]) min2=i; } t=aa[min1]+aa[min2]; sum+=t; if(min1==n-1) { aa[min2]=t; } else { aa[min1]=t; aa[min2]=aa[n-1]; } n--; } cout<<sum<<endl; } }
其实这道题 还有别的解法 比如哈夫曼树。 下次写的时候再贴上来。
相关文章推荐
- 1.一些 贪心算法 的简单思维题:
- 最大容器
- 满足和为定值的两个数或多个数
- Codeforces 193 Div 2
- Sicily 2013. Pay Back
- HDU-1009 FatMouse' Trade
- uva11729
- [CTSC2007]挂缀pendant
- Pku2376 Cleaning Shifts
- Pku2054 Color a Tree
- BZOJ 1909 Berth Allocation
- 七夕祭( Poetize系列)
- 快递员工作-贪心-区间
- [Usaco 2011 Dec]Umbrellas for Cows
- [SCOI2009]生日礼物
- [usaco2004 dec]打扫机房II-Pku2376 Cleaning Shifts(区间问题)
- Pku2054 Color a Tree(并查集优化+堆优化+贪心)
- POJ - 1700 Crossing River【贪心】
- 2013. Pay Back
- ZOJ-1076 Gene Assembly