poj 3253 Fence Repair
来源:互联网 发布:一代宗师 知乎 编辑:程序博客网 时间:2024/05/17 07:31
Description
Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needsN (1 ≤ N ≤ 20,000) planks of wood, each having some integer lengthLi (1 ≤ Li ≤ 50,000) units. He then purchases a single long board just long enough to saw into theN 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 theN-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 theN 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
Lines 2..N+1: Each line contains a single integer describing the length of a needed plank
Output
Sample Input
3858
Sample Output
34
Hint
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;}}
其实这道题 还有别的解法 比如哈夫曼树。 下次写的时候再贴上来。
- POJ 3253 Fence Repair
- poj 3253 Fence Repair
- POJ 3253 Fence Repair
- POJ 3253 Fence Repair
- POJ 3253 Fence Repair
- POJ 3253 Fence Repair
- poj 3253 Fence Repair
- POJ 3253Fence Repair
- POJ--3253 -- Fence Repair
- poj-3253-Fence Repair
- POJ 3253 Fence Repair
- POJ 3253 Fence Repair
- poj 3253 Fence Repair
- poj 3253 Fence Repair
- POJ - 3253 Fence Repair
- POJ 3253 Fence Repair
- poj 3253 Fence Repair
- poj 3253---Fence Repair
- Android-短信的接收和发送
- POJ 1700的详细解析
- MFC 相关MDI
- dynamic和匿名对象
- my.cnf /my.ini常用的一些服务端参数
- poj 3253 Fence Repair
- 17个新手常见Python运行时错误
- SWT 获取表格选中的行
- java的InputStream和OutputStream的理解【转】
- MySQL Server 5.0绿色版安装方法图文教程
- 一张图让你看懂各开源License
- Quartz实例及IIS闲置超时解决方案
- Activity四种加载模式
- Java多线程基础篇二------常用方法