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无关(因为两个车完全可以在同 ...
随机推荐
- Java笔试面试题整理第七波
转载至:http://blog.csdn.net/shakespeare001/article/details/51388516 作者:山代王(开心阳) 1.super的作用 在Java中su ...
- 【待考察】Appium使用技巧,助你快速入门移动端自动化!
Appium使用技巧,助你快速入门移动端自动化! 原创: 柠檬班superman 柠檬班软件测试 1月4日 关注并置顶[柠檬班]的小哥哥小姐姐 “猪”年行大运 说说最近研究移动端的自动化 移动端的自动 ...
- 权限管理demo-Http请求前后监听工具
工具作用: 1. 输出每次请求的参数 2. 接口的请求时间 package com.mmall.common; import com.mmall.util.JsonMapper; import lom ...
- linux 时间和时区设置
在linux中与时间相关的文件有 /etc/localtime /etc/timezone 其中,/etc/localtime是用来描述本机时间,而 /etc/timezone是用来描述本机所属的时区 ...
- Azkaban安装及分布式部署(multiple-executor)
参考文章:https://blog.csdn.net/weixin_35852328/article/details/79327996 官网:https://azkaban.readthedocs.i ...
- __module__ 和 __class__
__module__ 查看当前方法来之于那个文件 __class__ 查看当前方法来之于那个类
- python_09 文件处理流程,文件操作方法
文件处理流程 1.打开文件,得到文件句柄并赋值给一个变量 2.通过句柄对文件进行操作 3.关闭文件 f=open('test.txt',encoding='gbk') data = f.read() ...
- laravel不能读取session
Laravel用redis存储session,论坛有讨论说关于session无法保存的问题:https://laravel-china.org/topics/13510/points-to-be-pa ...
- 443. String Compression
原题: 443. String Compression 解题: 看到题目就想到用map计数,然后将计数的位数计算处理,这里的解法并不满足题目的额外O(1)的要求,并且只是返回了结果array的长度,并 ...
- 27.Hibernate-缓存和懒加载.md
目录 1.一级缓存 2.缓存的有效性 3.list和iterator缓存的区别 4.懒加载 4.1get 4.2load 1.一级缓存 Hibernate中一级缓存是Session缓存,有效范围在Se ...