poj 3253 Fence Repair (贪心,优先队列)
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).
Source
题意:
有一位农夫要把一个木板钜成n块给定长度的小木板,每次锯都要收取一定费用,这个费用就是当前锯的这个木板的长度
给定各个要求的小木板的长度,及小木板的个数n,求最小的费用;
PS:
以
3
5 8 5为例:
先从无限长的木板上锯下长度为 21 的木板,花费 21
再从长度为21的木板上锯下长度为5的木板,花费5
再从长度为16的木板上锯下长度为8的木板,花费8
总花费 = 21+5+8 =34
思路:
要使总费用最小,那么每次只选取最小长度的两块木板相加,再把这些“和”累加到总费用中即可;
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
using namespace std;
struct cmp
{
bool operator()(long long x, long long y)
{
return x > y;
}
};
priority_queue<long long ,vector<long long>,cmp>q;
int main()
{
int n;
int i;
long long x,y;
long long s = ;
cin >> n;
for (i = ; i <= n; i++)
{
cin >> x;
q.push(x);
}
while (q.size()>=)
{
long long r;
x = q.top(); q.pop();
y = q.top(); q.pop();
s = s + x + y;
r = x + y;
q.push(r);
}
cout << s << endl;
return ;
}
poj 3253 Fence Repair (贪心,优先队列)的更多相关文章
- POJ 3253 Fence Repair 贪心+优先队列
题意:农夫要将板割成n块,长度分别为L1,L2,...Ln.每次切断木板的花费为这块板的长度,问最小花费.21 分为 5 8 8三部分. 思路:思考将n部分进行n-1次两两合成最终合成L长度和题目 ...
- 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 (贪心)
Fence Repair Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
- POJ 3253 Fence Repair 贪心 优先级队列
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 77001 Accepted: 25185 De ...
- poj 3253 Fence Repair(优先队列+哈夫曼树)
题目地址:POJ 3253 哈夫曼树的结构就是一个二叉树,每个父节点都是两个子节点的和. 这个题就是能够从子节点向根节点推. 每次选择两个最小的进行合并.将合并后的值继续加进优先队列中.直至还剩下一个 ...
- poj 3253 Fence Repair 贪心 最小堆 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=3253 题解 本题是<挑战程序设计>一书的例题 根据树中描述 所有切割的代价 可以形成一颗二叉树 而最后的代价总和是与子节点和深 ...
- POJ 3253 Fence Repair(优先队列,哈夫曼树,模拟)
题目 //做哈夫曼树时,可以用优先队列(误?) //这道题教我们优先队列的一个用法:取前n个数(最大的或者最小的) //哈夫曼树 //64位 //超时->优先队列,,,, //这道题的优先队列用 ...
- POJ 3253 Fence Repair STL 优先队列
这题做完后觉得很水,主要的想法就是逆过程思考,原题是截断,可以想成是拼装,一共有n根木棍,最后要拼成一根完整的,每两根小的拼成一根大的,拼成后的木棍长度就是费用,要求费用最少.显然的是一共会拼接n-1 ...
- 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(修篱笆)
POJ 3253 Fence Repair(修篱笆) Time Limit: 2000MS Memory Limit: 65536K [Description] [题目描述] Farmer Joh ...
随机推荐
- Java——IO类,字节流缓冲区
body, table{font-family: 微软雅黑} table{border-collapse: collapse; border: solid gray; border-width: 2p ...
- cmake 手册详解
cmake 手册中文版,后续再相关博客的其他链接中,感谢翻译的好人! https://www.cnblogs.com/coderfenghc/archive/2012/06/16/CMake_ch_0 ...
- 用UILabel实现文字滚动播放(跑马灯)效果
- (void)viewDidLoad { [super viewDidLoad]; //数据源 self.messageArray = [NSArray arrayWithObjects: &quo ...
- Linux rm的一次误用
今天在Linux下误用了一次rm -rf,经历惨痛,记录一下. 原因是我删除了一个文件到回收站,然后点错了将home下的所有东西都删到了回收站,然后我又从回收站拷贝回home目录而不是使用恢复,因为h ...
- spring读取propertyes 新方法
<context:property-placeholder location="classpath:mysql.properties"/> <util:prope ...
- STM32中TIMx的映射及其通道
TIMx,通道x,无映射,部分映射,完全映射 TIM1_CH1, PA8, PE9, TIM1_CH2, PA9, PE11 TIM1_CH3, PA10, PE1 ...
- 和菜鸟一起学linux内核源码之基础准备篇
来源:http://blog.csdn.net/eastmoon502136/article/details/8711104 推荐阅读:linux内核源码最初版linux内核源代码,简单易懂,适合初学 ...
- Ajax和SpringMVC之间JSON交互
Ajax和SpringMVC之间的json数据传输有两种方式: 1.直接传输Json对象 2.将Json序列化成json字符串 1.直接传输Json对象 前端Ajax $(document).read ...
- 《DSP using MATLAB》Problem 3.4
代码: %% ------------------------------------------------------------------------ %% Output Info about ...
- (精)AVL树旋转共8种情况(涵盖所有考研的范围)