《挑战程序设计竞赛》 P77 题目: Fence Repair 用优先队列实现
来源:互联网 发布:程序员离职 编辑:程序博客网 时间:2024/06/09 21:30
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 Nplanks (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 <bits/stdc++.h>#include <iostream>using namespace std;int main(){ int N; // int L[20001]; cin >> N ; priority_queue<int, vector<int>, greater<int> > Q; int len=0; for(int i=1;i<=N;i++){ int t; cin >> t; len += t; Q.push(t); //cin >> L[i]; } int ans=0; while( Q.size()>1 ){ // 起初用的是 !Q.size()==1 ,未通过 int t1,t2; t1=Q.top() ; Q.pop(); t2=Q.top(); Q.pop(); ans = ans + t1 + t2; Q.push(t1+t2); } cout << ans ; return 0;}
- 《挑战程序设计竞赛》 P77 题目: Fence Repair 用优先队列实现
- [挑战程序设计竞赛] POJ 3253 - Fence Repair
- Fence Repair(挑战程序设计竞赛2.2)
- Fence Repair 【优先队列】
- Fence Repair (优先队列)
- 挑战程序设计竞赛 二叉堆(优先队列)的实现
- 挑战程序设计竞赛 poj 2431 优先队列
- POJ 题目3253 Fence Repair(优先队列,哈弗曼)
- POJ3253-- Fence Repair【优先队列】
- poj3253 Fence Repair【优先队列】
- poj3253 Fence Repair(优先队列)
- poj3235 Fence Repair【优先队列】
- Fence Repair (优先队列)
- POJ 3253 Fence Repair 题解(优先队列实现哈夫曼树)
- poj2431 Expedition (优先队列) 挑战程序设计竞赛
- 挑战程序设计竞赛 POJ 3253 贪心+霍夫曼思想+优先队列
- 挑战程序设计竞赛 POJ 2431 贪心+优先队列
- POJ 2431-Expedition [优先队列] 《挑战程序设计竞赛》2.4
- 实训第五天
- 一起有组织的网络攻击事件预警 | 内附企业网络安全建设实用指南
- dpkg ---- apt-get ------ aptitude 三种方式的区别 及命令格式
- iOS RSA 加密与解密
- 压力测试工具 ab
- 《挑战程序设计竞赛》 P77 题目: Fence Repair 用优先队列实现
- css BFC
- IDEA文件查找功能失效(ctrl+shift+N)
- (转载)Mac下idea运行main函数巨慢的解决方法
- EMV学习过程中问题解决及汇总
- java web工程,传递字符串参数小记
- 小程序-01基础配置
- 子查询
- 转发一篇很不错的面试总结汇总文章