poj 3253Fence Repair
来源:互联网 发布:南笙 知乎 编辑:程序博客网 时间:2024/06/05 04:51
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
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).
#include<iostream>#include<algorithm>using namespace std;#define maxn 20010typedef long long ll;long n,len;ll p[maxn];void push(ll k){ long t=++len; p[t]=k; while(t>1) { if(p[t/2]>p[t]) { swap(p[t],p[t/2]); t/=2; } else break; }}void pop(){ long t=1; p[t]=p[len--]; while(t*2<=len) { long k=t*2; if(k<len&&p[k]>p[k+1]) k++; if(p[t]>p[k]) { swap(p[t],p[k]); t=k; } else break; }}int main(){ cin>>n; long i; len=0; for(i=1;i<=n;i++) { cin>>p[i]; push(p[i]); } ll ans=0; while(len>1) { ll a,b; a=p[1]; pop(); b=p[1]; pop(); ans+=a+b; push(a+b); } cout<<ans<<endl;}STL优先队列版
#include <iostream>#include <queue>#include<cstdio>using namespace std;typedef long long ll;struct cmp{ bool operator ()(ll i,ll j) { return i>j; }};int main(){ priority_queue<ll,vector<ll>,cmp> s; ll n; while(cin>>n) { for(ll i=0;i<n;i++) { ll key; cin>>key; s.push(key); } ll sum=0; while(n>=2) { ll a,b; a=s.top(); s.pop(); b=s.top(); s.pop(); s.push(a+b); sum+=a+b; n--; } cout<<sum<<endl; } 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
- python 进阶学习之12
- 算法-欧几里得算法(C++实现)
- IOS添加小图标
- 编程名言
- - Fractal(3.4.1)
- poj 3253Fence Repair
- 暑期个人赛--第二场--B
- VC++笔记十
- do{...}while(0)的意义和用法
- 54 小明的存钱计划
- OpenCV的cvResize函数——图像缩放
- Linux MySQL的使用
- destructor(析构函数)
- Jbpm4.4 使用