题目如下,来源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

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).
代码如下:
#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更易解。

转载于:https://www.cnblogs.com/Never-Land/p/10103911.html

ACM_贪心法_queue_Fence Repair相关推荐

  1. 《c陷阱与缺陷》之贪心法

    在词法分析中,有条规则:每个符号应该包含尽可能多的字符,被称为"贪心法"或"大嘴法". K&R表述如下:如果(编译器的)输入流截止至某个字符之前都已经被 ...

  2. 贪心问题JAVA_java背包问题(贪心法)

    完全背包问题 一个旅行者有一个最多能用m公斤的背包,现在有n种物品,每件的重量分别是W1,W2,...,Wn,每件的价值分别为C1,C2,...,Cn.若的每种物品的件数足够多.求旅行者能获得的最大总 ...

  3. 【LeetCode笔记】55. 跳跃游戏(Java、贪心法)

    文章目录 题目描述 解法 & 思路 题目描述 可以用动态规划来做,dp[i]代表以第i个下标做起点,可以到达的最远的地方,从后往前走,最后dp[0] >= len-1说明可达. 不过我的 ...

  4. 1214线段覆盖问题——贪心法

    题目描述: 给定x轴上的N(0<N<100)条线段,每个线段由它的二个端点a_I和b_I确定,I=1,2,--N.这些坐标都是区间(-999,999)的整数.有些线段之间会相互交叠或覆盖. ...

  5. 学渣的刷题之旅 leetcode刷题 53.最大子序和(动态规划,贪心法)

    给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. 输入: [-2,1,-3,4,-1,2,1,-5,4], 输出: 6 解释: 连续子数组 [4, ...

  6. 【算法】一文详解贪心法

    一.概述 贪心法将一个复杂问题分解为一系列较为简单的局部最优解,每一步都是对当前解的一个扩展,直到获得问题的完全解.贪心法的典型应用时求解最优化问题,而且即使是非最优解,最终得出的解也和最优解比较近似 ...

  7. NOJ 1056 地道 普里姆算法+贪心法

    复习算法至普里姆算法,书上的代码不全,在oj上找了一题巩固一下. 普里姆算法应用在最小代价生成树一类的题型中,其主要执行思想是:选择一个点作为源点,此时生成树只包含一个源点,后从未加入生成树的点中选择 ...

  8. 算法设计与分析-----贪心法

    算法设计与分析-----贪心法(c语言) 一.贪心法 1.定义 2.贪心法具有的性质 1.贪心选择性质 2.最优子结构性质 3.贪心法的算法框架 5.求解活动安排问题 6.求解最优装载问题 二. 贪心 ...

  9. Java乘船_ACM:贪心法:乘船问题。

    题目:有n个人,第i个人的重量为wi,每艘船的最大载重量均为C,且最多只能乘两个人.用最少的船装载所有人. 分析:贪心法! 考虑最轻的人i,他应该和谁一起坐呢?如果每个人都无法和他一起坐船,那么唯一的 ...

最新文章

  1. grep之字符串搜索算法Boyer-Moore由浅入深(比KMP快3-5倍)
  2. android 上传字符串,Android中发送Http请求(包括文件上传、servlet接收)的实例代码...
  3. JRebel for Android 编译神器
  4. h5居中loading_Html5新增标签与样式及让元素水平垂直居中
  5. OpenGL 光照方程的计算
  6. CodeForces - 787D - Legacy(线段树优化建图+最短路)
  7. Mysql数据库函数(数字,字符串,日期时间)
  8. 互联网+大赛作品_“颂中国力量 绘美好梦想”全市中小学生互联网+书画大赛作品展示(五)...
  9. RHEL账号总结一:账号的分类
  10. ORA-01919: role 'OLAPI_TRACE_USER' does not exist
  11. 字符串 -- 3.1 Valid Palindrome -- 图解
  12. FRR BGP 协议分析 5 -- 路由更新(2)
  13. 专业PDF转换Word/PPT/Excel工具:AnyBizSoft PDF Converter绿色便携版
  14. 线上收单和线下收单的区别
  15. 2019 10月 月末总结
  16. 松柏先生:参透五百年不遇的《求贤令》,何愁品牌不立!
  17. 时间(格林尼治时间/协调世界时/世界时间)
  18. activiti实现任务超时,创建job不执行的原因
  19. java linux gui_关于Java:安装Linux桌面/ GUI环境
  20. 用计算机求值根号12345,【中考数学专题】特殊角的妙用——“12345模型”

热门文章

  1. java float 加法_Java-杂项:Float 加减精度问题
  2. php strace 工具,Linux程序调试工具工具—strace命令
  3. gitlab 无法git clone 的一个小点
  4. fastdfs安装_FastDFS 安装部署文档
  5. 计算机可以保研什么专业吗,【计算机专业推免】推免生是什么?成为推免生要达到那些要求?...
  6. FPGA之道(9)BLOCK RAM以及DSP硬核
  7. HDLBits 系列(18) BCD码计数器的设计
  8. 【 MATLAB 】使用 MATLAB 求某输入的稳态响应
  9. Android进程保活方案
  10. innodb表 手工导入导出