Poj3253 Fence Repair (优先队列)
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 67319 | Accepted: 22142 |
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
3
8
5
8
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 <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
typedef long long ll;
int main()
{
ll n,x;
while(cin>>n){
priority_queue<int,vector<int>,greater<int> > pq;
while(n--){
cin>>x;
pq.push(x);
}
ll sum=;
while(pq.size()>){
ll x1=pq.top();
pq.pop();
ll x2=pq.top();
pq.pop();
sum+=x1+x2;
pq.push(x1+x2);
}
cout<<sum<<endl;
}
return ;
}
Poj3253 Fence Repair (优先队列)的更多相关文章
- poj 3253 Fence Repair 优先队列
poj 3253 Fence Repair 优先队列 Description Farmer John wants to repair a small length of the fence aroun ...
- 优先队列 poj3253 Fence Repair
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 51411 Accepted: 16879 De ...
- POJ - 3253 Fence Repair 优先队列+贪心
Fence Repair Farmer John wants to repair a small length of the fence around the pasture. He measures ...
- POJ Fence Repair(优先队列)
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 51346 Accepted: 16857 De ...
- poj3253 Fence Repair【哈夫曼树+优先队列】
Description Farmer John wants to repair a small length of the fence around the pasture. He measures ...
- poj-3253 fence repair(贪心题)
题目描述: Farmer John wants to repair a small length of the fence around the pasture. He measures the fe ...
- poj3253 Fence Repair
http://poj.org/problem?id=3253 Farmer John wants to repair a small length of the fence around the pa ...
- POJ3253 Fence Repair(贪心)
分割木板的顺序是自由的,所以每次选择两块最短的板,组合在一起,增加队列,原来两个板出队,直到队列中为空或者仅仅剩下一个板时结束.这里使用优先队列较为方便. #include<iostream&g ...
- poj 3053 Fence Repair(优先队列)
题目链接:http://poj.org/problem?id=3253 思路分析:题目与哈夫曼编码原理相同,使用优先队列与贪心思想:读入数据在优先队列中,弹出两个数计算它们的和,再压入队列中: 代码如 ...
随机推荐
- (转)Geth控制台使用及Web3.js使用实战
在开发以太坊去中心化应用,免不了和以太坊进行交互,那就离不开Web3.Geth 控制台(REPL)实现了所有的web3 API及Admin API,使用好 Geth 就是必修课.结合Geth命令用法阅 ...
- CentOS7安装Jdk1.8
一.前期准备 a) 首先从官网上下载Jdk 8 for Linux x64到window下. b) 我这边用的最小安装,所以没有安装centos自带的openjdk,如果你安装时,不是最小安装的话,可 ...
- [文摘]那些一心想要离开 BAT 的人,后来怎么样了?
人心是个无底洞,填不满也掏不空 <Working at Google seemed like a dream job. The reality has been a tedious, point ...
- Hybrid设计--H5和Native,收口
Native提供容器,不要涉及太多的业务,否则就失去了通用性. H5和Native的差异是短时间内解决不了的,React Native 超越Hydrid跨平台解决方案. 前端和 Native约定了一个 ...
- Charles 安装图解(Mac 抓包工具)
概述 Fidder使用C#开发的,所以就不能在Mac上使用了,不过还有另外一个抓包神器,就是Charles,它是Java开发的,所以跨平台,不仅可以在Mac上使用,Linux以及Window下都是可以 ...
- 32.js 判断当前页面是否被浏览
可以通过document.hidden属性判断当前页面是否是激活状态. 兼容性:IE10+,Firefox10+,Chrome14+,Opera12.1+,Safari7.1+ 兼容性写法示例: va ...
- VScode编辑器个性化配置
一.设置方法 “文件” - > “首选项” -> "设置" 二.字体大小和缩进 "editor.tabSize": 2, "editor. ...
- Service Fabric本地开发部署修改数据目录
以修改5节点非安全模式为例: 在C:\Program Files\Microsoft SDKs\Service Fabric\ClusterSetup\NonSecure\FiveNode目录下,修改 ...
- js控制元素隐藏和显示
原生: 方法一: document.getElementById("idname").style.visibility="hidden"; document.g ...
- OpenGL and Vulkan resources
OpenGL https://www.zhihu.com/question/22005157https://open.gl/https://github.com/cybercser/OpenGL_3_ ...