题意

切割木板, 比如一根长21的木板要切割成5, 8, 8的三块, 每次切割花费的金额为两断的长度. 比如先把21切成16和5, 花费21元, 再把16切成8和8, 花费16元, 总计消费37元. 若先把21切成13和8, 花费21元, 再把13切成8和5, 花费13元, 总计消费34元, 花销更少
现在就要求出最小开销

思路

这道题可以用贪心, 这里主要记录一下白书上提供的优先队列 这个非常好的思路
把切割逆向看作拼接过程, 定义一个从小到大排列的优先队列

priority_queue<int, vector<int>, greater<int> > fen;

每次取出最短的两根木板拼接, 再将这个拼接后的长木板推入优先队列
直到fen.size() == 1, 这时木板被拼接完毕

记得看数据范围需要用long long !!

AC代码

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>using namespace std;
const int maxn = 20000 + 100;
priority_queue<int, vector<int>, greater<int> > fen;
typedef long long LL;
//int s[maxn];int main()
{int n, a;while( ~scanf("%d",&n) ){LL sum = 0;while( !fen.empty() ) fen.pop();for( int i = 0; i < n; i++ ){scanf("%d",&a);fen.push(a);}while( fen.size() > 1 ){int l1 = fen.top();fen.pop();int l2 = fen.top();fen.pop();sum += l1 + l2;fen.push(l1+l2);}printf("%lld\n",sum);}return 0;
}

转载于:https://www.cnblogs.com/JinxiSui/p/9740588.html

POJ 3253 - Fence Repai ( 优先队列 )相关推荐

  1. POJ 3253 Fence Repair(修篱笆)

    POJ 3253 Fence Repair(修篱笆) Time Limit: 2000MS   Memory Limit: 65536K [Description] [题目描述] Farmer Joh ...

  2. POJ 3253 -- Fence Repair

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 55661   Accepted: 18331 De ...

  3. 贪心算法-----poj 3253 Fence Repair(切木板)

    Description Farmer John wants to repair a small length of the fence around the pasture. He measures ...

  4. POJ 3253 Fence Repair C++ STL multiset 可解

    Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 53106 Accepted: 17508 Descri ...

  5. POJ 3253 Fence Repair 贪心

    每次合并选最短的两块木板,用优先队列优化. //#pragma comment(linker, "/STACK:1024000000,1024000000") #include&l ...

  6. POJ No. 3253 Fence Repair

    POJ No. 3253 Fence Repair 官方地址 题目 农夫约翰为了修理栅栏,要将一块很长的木板切割成N块.准备切成的木板的长度为L1.L2.-.LN,未切割前木板的长度恰好为切割后木板长 ...

  7. Fence Repair POJ - 3253

    Farmer John wants to repair a small length of the fence around the pasture. He measures the fence an ...

  8. 【POJ - 3253】Fence Repair(贪心,时光倒流)

    题干: Description Farmer John wants to repair a small length of the fence around the pasture. He measu ...

  9. Fence Repair(优先队列)

    Fence Repair Farmer John wants to repair a small length of the fence around the pasture. He measures ...

最新文章

  1. linux服务之git
  2. 关于随机验证码的一些小见解。
  3. Linux安装Elasticsearch+Kibana(7.10.2)
  4. 布客·ApacheCN 编程/后端/大数据/人工智能学习资源 2020.9
  5. linux镜像默认的安装位置,Linux下正确修改Docker镜像和容器的默认存储位置,亲测有效...
  6. nginx之lua_shared_dict命令
  7. ES6:Rest 参数和参数默认值【转】
  8. 用户已关闭隐式函数计算_针对隐式反馈和LightFM模型的杂谈
  9. php要求输入是个数求平均值、_如何在Excel中求数字个数
  10. spring mvc 返回json的配置
  11. 关于小米8玩王者荣耀加载缓慢的情况
  12. 使用序列号激活优动漫PAINT(附激活码)
  13. C#学员管理系统(源代码)
  14. 亚马逊fire充不上电_亚马逊Fire Tablet vs.Fire Tablet Kids:有什么区别?
  15. 无为WiFi正在被DD
  16. 什么使格瓦拉风行至今
  17. python类的魔法方法和装饰器
  18. 【Python系列】Python写csv文件长数字变成科学记数法的解决方案
  19. java模拟器ios版安装失败,iOS 在模拟器上安装 Debug 调试包(.app)
  20. 浅析疯狂动物城的制作流程

热门文章

  1. Python基础知识个人总结
  2. AnalyticDB 如何支撑数据银行超大规模低成本实时分析
  3. 数据库 网状模型和层次模型
  4. [词性] 十三、介词 4 [ before ] [ behind ] [ below ] [ beside ] [ between ]
  5. linux shell 日志函数
  6. 当前的几种开源游戏服务端介绍
  7. 经济基础知识(中级)【7】
  8. 2018最值得期待:云行业估值最高的独立云服务商金山云
  9. KDE设置快捷键打开剪贴板历史记录Klipper
  10. Financial Vocabulary