poj_3253 Fence Repair(优先队列 / 堆)
来源:互联网 发布:澳门网络娱乐nb88.com 编辑:程序博客网 时间:2024/06/06 03:23
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).
//优先队列#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <stack>#include <bitset>#include <queue>#include <set>#include <map>#include <string>#include <algorithm>#define FOP freopen("data.txt","r",stdin)#define inf 0x3f3f3f3f#define maxn 1000010#define mod 1000000007#define PI acos(-1.0)#define LL long longusing namespace std;priority_queue<int, vector<int>, greater<int> > pq;int main(){ int n, x; while(~scanf("%d", &n)) { LL ans = 0; int t1, t2; for(int i = 1; i <= n; i++) scanf("%d", &x), pq.push(x); while(pq.size() > 1) { t1 = pq.top(), pq.pop(); t2 = pq.top(), pq.pop(); ans += t1 + t2; pq.push(t1+t2); } if(!pq.empty()) pq.pop(); if(n == 1) printf("%d\n", x); else printf("%lld\n", ans); } return 0;}
//小顶堆#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <stack>#include <bitset>#include <queue>#include <set>#include <map>#include <string>#include <algorithm>#define FOP freopen("data.txt","r",stdin)#define inf 0x3f3f3f3f#define maxn 20010#define mod 1000000007#define PI acos(-1.0)#define LL long longusing namespace std;int n;int a[maxn];void Heap(int s, int m){ int temp = a[s]; for(int i = 2 * s; i <= m; i *= 2) { if(i < m && a[i+1] < a[i]) i++; if(temp < a[i]) break; a[s] = a[i], s = i; } a[s] = temp;}int main(){ //FOP; while(~scanf("%d", &n)) { LL ans = 0; for(int i = 1; i <= n; i++) scanf("%d", &a[i]); for(int i = n/2; i >= 1; i--) Heap(i, n); while(n > 1) { a[0] = a[1]; a[1] = a[n--]; Heap(1, n); a[1] += a[0]; ans += a[1]; Heap(1, n); } printf("%lld\n", ans); } return 0;}
- poj_3253 Fence Repair(优先队列 / 堆)
- Fence Repair (优先队列)
- POJ3253 Fence Repair 贪心+优先队列(堆维护)
- Fence Repair 【优先队列】
- Fence Repair (优先队列)
- POJ 3253 Fence Repair (优先队列)
- POJ 3253 Fence Repair(优先队列)
- POJ 3253 Fence Repair(优先队列)
- poj3253 Fence Repair (优先队列)
- 【POJ】-3253-Fence Repair(优先队列)
- POJ:3253 Fence Repair(优先队列)
- Fence Repair(优先队列) POJ 3253
- POJ 3253 Fence Repair 堆的应用,优先队列
- POJ3253 Fence Repair (二叉堆 | 优先队列 | huffman树 )
- POJ3253-- Fence Repair【优先队列】
- poj3253 Fence Repair【优先队列】
- poj3253 Fence Repair(优先队列)
- poj3235 Fence Repair【优先队列】
- 当你输入一个网址的时候,实际会发生什么?
- Android加载机器学习分类器问题
- 周志湖Spark系列
- Xcode8运行swift项目报错解决方法
- 云客Drupal8源码分析之核心处理流程HttpKernel(drupal8执行流程)
- poj_3253 Fence Repair(优先队列 / 堆)
- 五个数从小到大排列
- [linux私房菜笔记]第六章 linux文件权限与目录配置
- 使用HttpClient访问网路数据
- json
- js设计模式
- Amsterdam is shopper's paradise
- eclipse实现JavaWeb项目 增量打包
- 安卓真机测试:Installation error: INSTALL_CANCELED_BY_USER