POJ3253《Fence Repair》方法:优先队列
2013-04-09 10:27
274 查看
题目大意:需要切成20000块木块,每切一次的费用就等于该模板长度。
解题思路:因此首先将待切木板排序,每次取2块最小的木板,比如有8, 5, 8三块木板,首先切21的木板,然后切5,再切8,21+5+8=34。反过来就是5, 8, 8,第一次5+8=13, 8,13,第二次8+13=21。和为13+21=34。将这些小块木板合起来的最小费用等于将大块木板分割的最小费用。
解题思路:因此首先将待切木板排序,每次取2块最小的木板,比如有8, 5, 8三块木板,首先切21的木板,然后切5,再切8,21+5+8=34。反过来就是5, 8, 8,第一次5+8=13, 8,13,第二次8+13=21。和为13+21=34。将这些小块木板合起来的最小费用等于将大块木板分割的最小费用。
// 480k 49ms #include <iostream> #include <vector> #include <queue> #include <cstring> using namespace std; class cmp { public: bool operator()(const __int64 a, const __int64 b) const { return a > b; } }; int main() { freopen("temp.txt", "r", stdin); int n; while (cin >> n) { priority_queue<__int64, vector<__int64>, cmp> Queue; for (int i = 1; i <= n; ++i) { __int64 tmp; scanf("%I64d", &tmp); Queue.push(tmp); } __int64 mincost = 0; while (Queue.size() > 1) { __int64 a = Queue.top(); Queue.pop(); __int64 b = Queue.top(); Queue.pop(); Queue.push(a+b); mincost += a+b; } printf("%I64d\n", mincost); while (!Queue.empty()) { Queue.pop(); } } return 0; }
相关文章推荐
- POJ3253 Fence Repair【哈夫曼树+优先队列】
- poj3253 Fence Repair 优先队列,C++STL中priority_queue的使用
- POJ3253 Fence Repair (二叉堆 | 优先队列 | huffman树 )
- POJ3253 Fence Repair (二叉堆 | 优先队列 | huffman树 )
- poj3253 Fence Repair【优先队列】
- POJ3253 Fence Repair (二叉堆 | 优先队列 | huffman树 )
- poj3253 Fence Repair【优先队列】
- 【Huffman树贪心+优先队列】POJ3253-Fence Repair
- 用“双优先队列”方法解决双/多指标的规划问题
- POJ 题目3253 Fence Repair(优先队列,哈弗曼)
- 【POJ 3253 Fence Repair】+ 优先队列
- STL中的优先队列priority_queue的使用方法
- POJ 3253 Fence Repair 优先队列
- poj3235 Fence Repair【优先队列】
- POJ 3253 优先队列 Fence Repair
- hdu 1026 Ignatius and the Princess I (bfs记录路径 两种记录路径方法模拟优先队列和优先队列两种做法)
- 优先队列(和fence repair完全一样)
- 优先队列的构造以及使用方法的总结(for priority_queue)
- STL中优先队列的使用方法
- 20140822 【 优先队列 】 POJ 3253 Fence Repair