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

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).
题意:给你一个木板,让你切,每切一次的长度,就是你需要的花费的金钱,让你找出一种方式,使花费最小

解释一下样例:
题中给出的数据  8 5 8,按小为优先进入队列即为5 8 8,要费用最小,即每次锯成的两块木板的长度最小(这样他们的和就最小),如题中数据,先选出5 和 8, 5+8=13,ans=ans+13, 13是倒数第一步锯木头的行为的木板长度, 接着 13  8进入队列后自动以小优先排序即为 8 13, 在倒数第二步的锯木头行为 , 8+13=21, ans=ans+21, ans这样最终ans 最小取得 34

思路:

用优先队列实现(自动排序)
首先取出两个较小元素,求的和放入队列中,队列此时会重新排一次序,再选两个较小的首元素,直至队列为空

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include <set>
using namespace std;
typedef long long LL;
int main()
{priority_queue<LL,vector<LL>,greater<LL> > q;LL n;LL a[20010];cin>>n;for(int i=0;i<n;i++){cin>>a[i];q.push(a[i]);}LL sum=0,ans=0;while(q.size()>1){sum=q.top();q.pop();sum+=q.top();q.pop();ans+=sum;q.push(sum);}cout<<ans<<endl;return 0;
}

优先队列+哈夫曼树(Fence Repair)相关推荐

  1. POJ3253 Fence Repair【哈夫曼树+优先队列】

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 48026   Accepted: 15784 De ...

  2. Vijos P1097 合并果子【哈夫曼树+优先队列】

    描述 在一个果园里,多多已经将所有的果子打了下来,而且按果子的不同种类分成了不同的堆.多多决定把所有的果子合成一堆. 每一次合并,多多可以把两堆果子合并到一起,消耗的体力等于两堆果子的重量之和.可以看 ...

  3. SDUT 2127 树-堆结构练习——合并果子之哈夫曼树(优先队列)

    树-堆结构练习--合并果子之哈夫曼树 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Problem Description ...

  4. 【HDU - 5884】Sort(k叉哈夫曼树,优化tricks,两个队列代替优先队列)

    题干: Recently, Bob has just learnt a naive sorting algorithm: merge sort. Now, Bob receives a task fr ...

  5. [C++优先队列模板应用一] 哈夫曼树

    优先队列: priority_queue<Int> Q; 建立一个int类型的堆Q, 默认为大根堆 priority_queue<int, vector<int>, gr ...

  6. POJ-Fence Repair 哈夫曼树

    哈夫曼树,一个很耳熟的数据结构课上的内容.是用来处理编码的,准确的说是能够去压缩编码,本身是一种无损的压缩凡是,就是对于给定的字符做一个统计,然后根据统计构建一个长短不一的映射表,再根据这个映射表来恢 ...

  7. pta 构造哈夫曼树-有序输入 优先队列做法

    pta 构造哈夫曼树-有序输入 优先队列做法 构造哈夫曼树,然后输出它树的中序序列. 从小到大的顺序给出词频(不超过10个),根据词频构造哈夫曼树. 为确保构建的哈夫曼树唯一,本题做如下限定: (1) ...

  8. 哈夫曼树构造(优先队列)

    使用优先队列,从小往大取,构造哈夫曼树. #include <bits/stdc++.h> using namespace std; string a; int ans, t[200]; ...

  9. 哈夫曼树的带权路径长度(C++优先队列实现)

    哈夫曼树 题目描述 哈夫曼树,第一行输入一个数n,表示叶结点的个数.需要用这些叶结点生成哈夫曼树,根据哈夫曼树的概念,这些结点有权值,即weight,题目需要输出所有结点的值与权值的乘积之和的最小值. ...

  10. 超好理解的哈夫曼树(最优二叉树)与例题

    对于比较官方的说法就是给定n个权值作为n个叶子结点,构造一棵二叉树,若带权路径长度达到最小,称这样的二叉树为最优二叉树,也称为哈夫曼树(Huffman Tree).哈夫曼树是带权路径长度最短的树,权值 ...

最新文章

  1. php的基本语法和数据类型
  2. python numpy矩阵索引_python-为什么scipy csr矩阵的行索引比numpy数组...
  3. android获取毫秒,Android 日期转为为毫秒,毫秒转化为日期,获取当期日期年、月、日...
  4. opengl游戏引擎源码_渲染概念:1.引擎二三事
  5. windows系统上使用openssh client连接远程Linux服务器的日志分析
  6. iOS:Core Data 中的简单ORM
  7. 商标注册流程与注意事项 logo 商标注册类型分类解释
  8. 数据之路 - Python爬虫 - urllib库
  9. Java异步编程——深入源码分析FutureTask
  10. readelf 和 objdump 例子详解及区别 (ELF文件说明)
  11. 【Java】JavaSocket编程开发聊天室-服务器端部分
  12. Python如何实现图片显示
  13. 储存卡怎么格式化为fat32_64g内存卡怎么格式化成fat32格式化
  14. Jquery监听input回车事件
  15. 在QCreator IDE中 使用 Orge3D
  16. BI是什么?应用BI工具能给企业带来哪些帮助?
  17. Facebook 如何识别出性工作者?
  18. AdGuard添加规则方法
  19. 1.22-1.23板卡调试日志
  20. Mysql创建多表视图view

热门文章

  1. 如何安全地进行ddos压力测试
  2. 阿里云破世界记录,王坚说新登月计划需十年,我看不用!
  3. Sql server 数量累计求和
  4. 如何做好项目的需求与业务调研?
  5. jquery中的trigger()和preventDefault()方法
  6. Android游戏开发的开源框架
  7. RxJava -- 从 create 开始 (一)
  8. linux实现普通用户只允许使用部分命令
  9. 带宽与虚拟桌面的考虑
  10. Laravel 5.4 api 允许跨域访问