R - Fence Repair POJ - 3253
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).
给我一段木头,每次锯断的是他的长度,问我怎么样最小,从小到大排列,5,8,8然后变成了13,8,sum = 13,然后变成了21,sum = 34;
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;
#define int long long
int n,sum,x[200005];
int t,p;
priority_queue<int,vector<int>,greater<int> > pq;
signed main()
{
//freopen("in","r",stdin);
//freopen("out","w",stdout);
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
while(cin >> n){
sum = 0;
for(int i = 0; i < n; i++){
cin >> x[i];
pq.push(x[i]);
}
while(pq.size() > 1) {
t = pq.top();
pq.pop();
p = pq.top();
pq.pop();
sum += t + p;
pq.push(t + p);
}
cout << sum << endl;
}
return 0;
}
R - Fence Repair POJ - 3253的更多相关文章
- Fence Repair POJ - 3253 (贪心)
Farmer John wants to repair a small length of the fence around the pasture. He measures the fence an ...
- Fence Repair (POJ 3253)
农夫约翰为了修理栅栏,要将一块很长的木板切割成N块.准备切成的木板长度为L1.L2.L3...LN,未切割前的木板长度恰好为切割后木板长度的总和.每次切断木板时,需要的开销为这块木板的长度.例如长度为 ...
- 贪心算法——Fence Repair(POJ 3253)
题目描述 农夫约翰为了修理栅栏,要将一块很长的木板切割成N块.准备切成的木板长度为L1,L2,L3--LN,未切割前木板的长度恰好为切割后木板长度的总和.每次切断木板时,需要的开销为这块木板的长度.请 ...
- Fence Repair POJ - 3253 哈夫曼思想 优先队列
题意:给出一段无限长的棍子,切一刀需要的代价是棍子的总长,例如21切一刀 变成什么长度 都是代价21 列如7切成5 和2 也是代价7题解:可以利用霍夫曼编码的思想 短的棍子就放在底层 长的尽量切少一次 ...
- POJ 3253 Fence Repair(修篱笆)
POJ 3253 Fence Repair(修篱笆) Time Limit: 2000MS Memory Limit: 65536K [Description] [题目描述] Farmer Joh ...
- POJ 3253 Fence Repair (贪心)
Fence Repair Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
- POJ 3253 Fence Repair (优先队列)
POJ 3253 Fence Repair (优先队列) Farmer John wants to repair a small length of the fence around the past ...
- poj 3253 Fence Repair 优先队列
poj 3253 Fence Repair 优先队列 Description Farmer John wants to repair a small length of the fence aroun ...
- POJ 3253 Fence Repair (哈夫曼树)
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 19660 Accepted: 6236 Des ...
随机推荐
- Flink流处理(五)- 状态与一致性模型
状态(State)与一致性模型 接下来我们转向另一个在流处理中十分重要的点:状态(state).状态在数据处理中是无处不在的.为了产生一个结果,函数一般会聚合某个时间段内(或是一定数量的)events ...
- python正则分组
python的正则表达式本身每一个字符串都是独立的 看下面的例子就理解分组的含义了~ ab*表示的是查找a和(0个或多个b),就是*是单独针对b的,所以返回a (ab)*则表示ab是一个组是一个整体, ...
- Visual C++ 2013 and Visual C++ Redistributable Package 更新版官网下载地址
Visual C++ 2013 and Visual C++ Redistributable Visual C++ 2013 and Visual C++ Redistributable Packag ...
- c语言getipaddrtable获得ip地址与sendArp获得mac地址以及一些字节序问题记录
https://docs.microsoft.com/zh-cn/windows/win32/api/iphlpapi/nf-iphlpapi-getipaddrtable msdn,有很多c的源码还 ...
- Sql Server跨服务器操作数据
var serversSql = "select count(*) count from sys.servers WHERE name='ITSV'"; var result = ...
- 【C语言】分别用下标法,地址法和指针法输出数组中的全部元素
#include<stdio.h> int main() { ] = { ,,,, }; int i, * p; printf("下标法:\n"); ; i < ...
- awk基本介绍
AWK 是一种用于处理文本的编程语言工具.awk经过改进生成的新的版本nawk,gawk,现在默认linux系统下日常使用的是gawk,用命令可以查看正在应用的awk的来源(ls -l /bin/aw ...
- 搭建vue工程遇到的问题汇总
1.vue搭建环境- vue init webpack my-project无响应(如下报错) 原因:node 版本过高.v8.1.0 解决拌饭: 降级,终端指令 n v8.0.0; mac用户: s ...
- SniperOj-compare_flag-Writeup
SniperOj-compare_flag-Writeup 题干如上,只给了一个nc命令,那么连接到服务器如下 有如下的python代码 #!/usr/bin/env python from time ...
- Java日期时间API系列21-----Jdk8中java.time包中的新的日期时间API类,xk-time时间转换,计算,格式化,解析的工具
通过工作之余,对Java8中java.time包源码的不断学习,使用和总结,开发了xk-time,初步完成,欢迎试用和提出建议! xk-time xk-time is a datetime conve ...