Fence Repair
来源:互联网 发布:网络推手成名的网红 编辑:程序博客网 时间:2024/06/15 18:10
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).
Source
USACO 2006 November Gold
题意:FJ想要据割木板,每据一块长为x的木板花费x元钱,给出要据的块数N,和每块的长度,求最少的花费
哈夫曼树构造过程,
取两个最小的,分别pop,相加的和push.jin
#include<stdio.h>#include<iostream>using namespace std;#include<algorithm>#include<queue>using namespace std;int main(){ int n,t,a,b; long long sum; while(~scanf("%d",&n)) { sum=0; priority_queue<int,vector<int>,greater<int> >pq; for(int i=0;i<n;i++) { scanf("%d",&t); pq.push(t); } sum=0; if(pq.size()==1) { a=pq.top(); sum+=a; pq.pop(); } while(pq.size()>1) { a=pq.top(); pq.pop(); b=pq.top(); pq.pop(); sum+=(a+b); pq.push(a+b); } printf("%lld\n",sum); }}
- Fence Repair
- Fence Repair
- Fence Repair
- Fence Repair
- Fence Repair
- Fence Repair
- Fence Repair
- Fence Repair
- Fence Repair
- Fence repair
- Fence Repair
- Fence Repair
- Fence Repair
- Fence Repair
- Fence Repair
- Fence Repair
- Fence Repair
- Fence Repair
- 何时使用hadoop fs、hadoop dfs与hdfs dfs命令
- 机器视觉学习笔记--图像滤波3
- (转)Thread.sleep(0)的意义& 多线程详解
- P4 游戏中的Human角色
- Word文档损坏如何恢复?
- Fence Repair
- 博客收藏
- mybatis常见技巧
- 使用CodeIgniter框架搭建RESTful API服务
- java poi 通过excel模板导出并且生成多个sheet
- Linux下如何将一个用户下的文件拷贝到另一个用户里
- 210. Course Schedule II\199. Binary Tree Right Side View\279. Perfect Squares
- Hadoop:hadoop fs、hadoop dfs与hdfs dfs命令的区别
- C语言 扫雷游戏代码及详细注释