ACM_贪心法_queue_Fence Repair
题目如下,来源POJ
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 Nplanks. 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 <stdio.h>
#include <queue>
using namespace std;
priority_queue<long long int,vector<long long int>,greater<long long int> >p;
long long int n,b,c,d,cou;
long long int plank;
int main(){
while(~scanf("%d",&n)&&n){
cou=0;d=0;
for(int a=0;a<n;a++){
scanf("%d",&plank);
p.push(plank);
}
while(1){
b=0;c=0;
b=p.top();
p.pop();
if(p.empty()) break;
c=p.top();
p.pop();
d=b+c;
cou=d+cou;
p.push(d);
}
printf("%lld\n",cou);
}
return 0;
}
这题一眼看去,第一思路是由整块木板开始,先把大块的切下来,但这种思路是错误的(可证反)。正确思路是从树表底层开始,不断将最小的两块合并,提取每次合并的和,即可得到答案,常用于霍夫曼编码规则(从树状表底层开始编码)。用queue更易解。
ACM_贪心法_queue_Fence Repair的更多相关文章
- 贪心法 codevs 1052 地鼠游戏
1052 地鼠游戏 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 题目描述 Description 王钢是一名学习成绩优异的学生,在平 ...
- Codeforces 401C Team 贪心法
本题使用贪心法.关键是考贪心策略.同一时候要求要细心,我提交的时候也WA了几次.大意题目就是怎样依照给定的规则排列一个01字符串,引用原题例如以下: C. Team time limit per te ...
- LeetCode刷题总结-二分查找和贪心法篇
本文介绍LeetCode上有关二分查找和贪心法的算法题,推荐刷题总数为16道.具体考点归纳如下: 一.二分查找 1.数学问题 题号:29. 两数相除,难度中等 题号:668. 乘法表中第k小的数,难度 ...
- C 编译器的“贪心法”
C语言中有单字符符号和多字符符号之分,那么,当C编译器读入一个字符‘/’后又跟了一个字符‘*’,那么编译器就必须做出判断:是将其作为两个分别的符号对待,还是合起来作为一个符号对待.C语言对这个问题的解 ...
- Codeforces Round #249 (Div. 2)B(贪心法)
B. Pasha Maximizes time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- 11636 - Hello World! (贪心法)
Problem A Hello World! Input: Standard Input Output: Standard Output When you first made the comput ...
- 贪心法:K叉哈夫曼树
NOI2015荷马史诗 一部<荷马史诗>中有 n 种不同的单词,从 1 到 n 进行编号.其中第 i 种单词出现的总次数为 wi.Allison 想要用 k 进制串 si 来替换第 i 种 ...
- uva11054 - Wine trading in Gergovia(等价转换,贪心法)
这个题看上去麻烦,实际上只要想清楚就很简单.关键是要有一种等价转换的思维方式.其实题意就是个一排数,最后通过相邻的互相移动加减使得所有数都变成零,移动过程中每次都耗费相应值,让耗费的值最小.虽然从实际 ...
- uva11134 - Fabled Rooks(问题分解,贪心法)
这道题非常好,不仅用到了把复杂问题分解为若干个熟悉的简单问题的方法,更是考察了对贪心法的理解和运用是否到位. 首先,如果直接在二维的棋盘上考虑怎么放不好弄,那么注意到x和y无关(因为两个车完全可以在同 ...
随机推荐
- 通过eclipse打开jdk native源码
1.下载 eclipse http://www.eclipse.org/downloads/eclipse-packages/ 建议下载 Eclipse IDE for Eclipse Committ ...
- nodejs模块循环引用讲解
CommonJS 模块的重要特性是加载时执行,即脚本代码在require的时候,就会全部执行.一旦出现某个模块被"循环加载",就只输出已经执行的部分,还未执行的部分不会输出. 让我 ...
- Java 学习 UUID 与 时间格式化、时间操作
UUID : UUID 是 通用唯一识别码(Universally Unique Identifier)的缩写,是一种软件建构的标准,亦为开放软件基金会组织在分布式计算环境领域的一部分.其目的,是让分 ...
- 有一个VC的bug:非标准语法
---恢复内容开始--- 主函数中调用类的成员函数时报错: “error C3867:非标准语法:请使用 "&" 来创建指向成员的指针” 这时在函数前老老实实加上& ...
- 【机器学习_5】Anaconda:初学Python、入门机器学习的首选
Anaconda是一个用于科学计算的Python发行版,提供了包管理与环境管理的功能,可以很方便地解决多版本python并存.切换以及各种第三方包安装问题. 集成包功能: NumPy:提供了矩阵运算的 ...
- 注解_Annotation
---恢复内容开始--- 一.什么是注解 注解,英文Annotation,它不是程序本身,是对程序的解释,在这里我会想到为什么不能使用注释呢,因为注解是关于程序对信息的处理的流程的一些说明,而且格式也 ...
- for 没有作用域的说话
for i in range(10): passprint(i) 打印的结果就是9 打印的最后一次结果
- eclipse的Git忽略某些不需要提交的文件
Eclipse切换到Navigator视图,找到.gitignore文件(如果是maven项目,一般找作为modules的项目的.gitignore文件),添加内容: .settings .proje ...
- [INet] WebSocket 数据收发的详细过程
WebSocket 和 HTTP 相似,只是一个应用层协议,对下层透明,所以不涉及 TCP/IP. 由于浏览器支持了 WebSocket,所以在用 JS 写客户端的时候,是无需考虑数据的编码解码的. ...
- texmaker报错:could not start command 解决
我当时文件命名加了邮箱,引入特殊字符@,然后就报错了