POJ--3253 Fence Repair
来源:互联网 发布:期货行情软件下载 编辑:程序博客网 时间:2024/06/05 05:42
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).
两种做法:
第一种二叉树分割贪心求解:
以下是我的AC代码:
#include<iostream>#include<cstring>#include<cstdio>using namespace std;int aa[20010];int main(){ int n; while(cin>>n) { long long ans=0; for(int i=0;i<n;i++) cin>>aa[i]; while(n>1) { int min1=0,min2=1; if(aa[min1]>aa[min2]) swap(min1,min2); for(int i=2;i<n;i++) { if(aa[i]<aa[min1]) { min2=min1; min1=i; } else if(aa[i]<aa[min2]) { min2=i; } } int t=aa[min1]+aa[min2]; ans+=t; if(min1==n-1) swap(min1,min2); aa[min1]=t; aa[min2]=aa[n-1]; n--; } cout<<ans<<endl; } return 0;}
第二种为优先队列:(这个更加高效)
以下是我的AC代码:
#include<iostream>#include<cstring>#include<cstdio>#include<queue>#include<vector>using namespace std;typedef long long LL;int aa[20005];int main(){ //freopen("input.in","r",stdin); int n; while(scanf("%d",&n)!=EOF) { for(int i=0;i<n;i++) scanf("%d",&aa[i]); LL ans=0; priority_queue<int,vector<int>,greater<int> > que; for(int i=0;i<n;i++) { que.push(aa[i]); } while(que.size()>1) { int min1,min2; min1=que.top(); que.pop(); min2=que.top(); que.pop(); ans+=min1+min2; que.push(min1+min2); } printf("%lld\n",ans); } return 0;}
- 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
- LeetCode 258. Add Digits
- Java反转单链表
- 类的属性、对象的私有字段与主构造器、私有构造器、重载构造器
- 控件的外观代理对象以及+(void)initialize方法
- 【Nginx入门系列】第五章 tomcat在linux下的安装和集群预部署
- POJ--3253 Fence Repair
- InfluxDB权限管理
- Socket 基础&实例(上)
- 【Nginx入门系列】第六章 Nginx+tomcat集群负载均衡部署
- swift基本语法(总结提炼版)之005 Swift之 switch循环
- 最近读书笔记
- [c++]类的构造函数
- HierarchyViewer结合merge标签优化布局结构
- BZOJ 3750