【POJ3253】Fence Repair(优先队列+贪心)
来源:互联网 发布:域名绑定公网ip 编辑:程序博客网 时间:2024/06/06 08:27
记录一个菜逼的成长。。
题目链接
题目大意:
将一块长度为L的木板切割为两块,花费为L,切割后的两块木板的长度和为切割前的长度。给你N个切割后的木板的长度,问最小花费是多少。
书上的一道贪心题。用huffman编码的思想贪心,感觉挺有趣的。
我们可以考虑将两块合并成一块木板,花费为合并后的木板的长度。
根据huffman思想,每次选取长度最小的两个合并,以此贪心,最后可以保证花费最少。
如何选取最小的两个,可以使用优先队列。
#include <cstdio>#include <iostream>#include <cstring>#include <algorithm>#include <vector>#include <queue>using namespace std;typedef long long LL;int main(){ ios::sync_with_stdio(false); cin.tie(0); int n; while(cin>>n){ priority_queue<int,vector<int>,greater<int> >q; LL ans = 0; for( int i = 0; i < n; i++ ){ int x;cin>>x; q.push(x); } while(q.size() > 1){ int x = q.top();q.pop(); x += q.top();q.pop(); ans += (LL)x; q.push(x); } cout<<ans<<endl; } return 0;}
0 0
- 【POJ3253】Fence Repair(优先队列+贪心)
- poj3253 Fence Repair 贪心/优先队列
- POJ3253-- Fence Repair【优先队列】
- poj3253 Fence Repair【优先队列】
- poj3253 Fence Repair(优先队列)
- POJ3253 Fence Repair 贪心+优先队列(堆维护)
- POJ3253《Fence Repair》方法:优先队列
- poj3253 Fence Repair【哈夫曼树+优先队列】
- poj3253 Fence Repair (优先队列)
- POJ3253 Fence Repair【哈夫曼树+优先队列】
- poj3253 Fence Repair( 贪心 )
- POJ3253 Fence Repair(贪心)
- poj3253 Fence Repair 贪心
- POJ3253-贪心-Fence Repair
- POJ3253 Fence Repair (贪心)
- POJ3253 Fence Repair (二叉堆 | 优先队列 | huffman树 )
- POJ3253 Fence Repair 小顶堆+贪心
- POJ3253-Fence Repair-贪心-哈夫曼树
- 怎样在weblogic服务器中发布web项目
- IOC(控制反转)和DI(依赖注入)
- 黑白棋
- 有关于链表的题目(打印链表、翻转链表、合并两个排序链表)
- 新年第一篇啊
- 【POJ3253】Fence Repair(优先队列+贪心)
- ubuntu软件备份及重新安装方法
- 说说关于Android使用Gson解析Json所需实体类的那些事~
- getopts
- java 蓝桥杯 高斯日记
- ABP入门系列(1)——通过模板创建MAP版本项目
- ABP入门系列(2)——领域层创建实体
- ABP入门系列(3)——领域层定义仓储并实现
- ABP入门系列(4)——创建应用服务