POJ 3253 贪心法,优先队列
来源:互联网 发布:苹果vr软件 编辑:程序博客网 时间:2024/05/17 03:06
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
Lines 2..N+1: Each line contains a single integer describing the length of a needed plank
Output
Sample Input
3858
Sample Output
34
Hint
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).
遇到的问题和思路:
首先吧,看到这道题真心不会。因为优先队列的知识没有学过,所以做起来有点吃力。起初看了题解以为只是单纯的排序好放进去就好了,根本就没有想到有优先队列,后来才知道,原来优先队列还是有自动排序的,长知识了,有收获!
给出AC代码:
#include<cstdio>#include<algorithm>#include<cstring>#include<cmath>#include<queue>using namespace std;int a[20005];int t;long long ans;//首先注意queue的()不能忘了,然后通过ans来计数不能忘了。还有一定要先对a数组进行排序 void solve(){priority_queue <int, vector<int>, greater<int> > que;ans = 0; for (int i = 0; i < t; i++) que.push(a[i]);while (que.size() > 1){int a1 = que.top();que.pop();int a2 = que.top();que.pop();que.push(a1 + a2);ans = ans + a1 + a2;}printf("%I64d\n",ans);}int main(){while(scanf("%d",&t)!=EOF){memset(a, 0, sizeof(a));for(int i = 0; i < t; i++){scanf("%d",&a[i]);}sort(a,a+t);solve();}return 0;}
- POJ 3253 贪心法,优先队列
- POJ 3253 贪心 优先队列
- POJ 3253 Fence Repair (贪心&优先队列)
- POJ 3253 Fence Repair(贪心,优先队列)
- POJ 3253 Fence Repair(贪心+优先队列)
- POJ 3253 Fence Repair 贪心+优先队列
- 【POJ 3253】【贪心+优先队列】Fence Repair
- Poj 3253 Fence Repair【优先队列+贪心】
- poj 3253(贪心 + 优先队列)
- poj-2431 贪心+优先队列
- poj 2431 优先队列&贪心
- poj 3190 优先队列+贪心
- poj 2010 优先队列+贪心
- poj 2431 贪心+优先队列
- POJ 3190 贪心+优先队列
- poj 3253 类似霍夫曼贪心思想,优先队列的运用
- poj 3253 Fence Repair (贪心+哈弗曼思想+优先队列)
- POJ 3253 Fence Repair 霍夫曼树 贪心 优先队列
- HDU1081
- spring-模板设计模式
- iOS黑魔法-Method Swizzling
- 谈谈安装MYSQL ODBC遇到的报错(Error 1918)
- 键值对应列表
- POJ 3253 贪心法,优先队列
- soupUI导出webservice客户端
- iphone相对精准nstimer实现
- maven 普通java项目 / war 包
- 进程间通信
- C++:泛型编程(统计同成绩学生人数)
- 微信编辑器新手入门必读
- 自己收集/编写的方法
- 备忘录模式