poj 3253 Fence Repair之优先队列解法
来源:互联网 发布:大富豪源码论坛 编辑:程序博客网 时间:2024/05/18 13:25
Fence Repair
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 50747 Accepted: 16646
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
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).
分析:给出n段木头长度,求将一块长木头分成这n段木头所需最小费用.
画个图就会发现,就是求一个路径权值均为1的哈弗曼树的所有路径长度之和.理论指导就是哈弗曼树.
用优先队列来算,就是每次取最小的两个值,求和后得到新的值,放入队列中,这样来建树,不断循环,求取最终结果.
/** Filename: code.cpp* Created: 2017-07-21* Author: wyl6 *[mail:17744454343@163.com]* Desciption: Desciption*/#include <cstdio>#include <cstdlib>#include <iostream>#include <stack>#include <queue>#include <algorithm>#include <cstring>#include <cmath>#include <vector>#include <bitset>#include <list>#include <sstream>#include <set>#include <functional>using namespace std;#define INT_MAX 1 << 30#define MAX 100typedef long long ll;int n;priority_queue<int,vector<int>,greater<int> > qu;int main(int argc, char const* argv[]){ scanf("%d",&n); int price; int price1; int price2; long long sum; long long ans = 0; while (n--){ scanf("%d",&price); qu.push(price); } while (!qu.empty()){ price1 = qu.top(); qu.pop(); if (!qu.empty()){ price2 = qu.top(); qu.pop(); sum = price1+price2; qu.push(sum); ans += sum; }else{ printf("%lld\n",ans); break; } } return 0;}
- poj 3253 Fence Repair之优先队列解法
- POJ 3253 Fence Repair(STL之优先队列)
- 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 哈夫曼树 优先队列
- 20140822 【 优先队列 】 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学习笔记(八)类(classes)
- jvm自动内存管理机制 之 java内存区域
- 百度SEO,让百度蜘蛛抓取自己网站
- leetcode 494. Target Sum
- spring源码附录(2)spring profile属性的简单使用
- poj 3253 Fence Repair之优先队列解法
- spring事物配置,声明式事务管理和基于@Transactional注解的使用
- Linux错误Connection activation failed: Device not managed by NetworkManager or unavailable
- RGB图像转为灰度图像原理
- 获取手机归属地
- ini文件读写
- 51Nod-1684-子集价值
- Properties配置文件中保存中文解决办法
- 图文助你理解Binder