poj2431Expedition(贪心,优先队列)
来源:互联网 发布:风水罗盘软件安卓版 编辑:程序博客网 时间:2024/05/21 21:00
题意
有一辆车,要开l的路,它每开一单位的路要消耗一单位的油。车里原来有p的油,车的油箱容量是无限大的。现在路上不同位置有一些加油站,这些加油站里有一定量的油,问开到终点最少要加几次油。
思路
我们肯定尽量加多一点的油,那么我们这样考虑,假如我经过一个加油站,我就获得了加这个站的油的权利,那么我可以一直走,一直走到没油,这时候取出我前面能加的油里面最多的那个加上继续走
代码
#include <cstdio>#include <iostream>#include <queue>#include <algorithm>using namespace std;const int kMaxn = 10000 + 10;struct aa { int d,v;}a[kMaxn];bool cmp(aa x, aa y) { return x.d < y.d;}int main() { int n; while(~scanf("%d", &n)) { for(int i = 1; i <= n; i++) scanf("%d %d", &a[i].d, &a[i].v); int l,p; scanf("%d %d", &l, &p); for(int i = 1; i <= n; i++) a[i].d = l - a[i].d; sort(a + 1, a + 1 + n, cmp); n++; a[n].d = l; priority_queue<int>Q; int pos = 0; bool flag = true; int sum = 0; for(int i = 1; i <= n; i++) { p = p - (a[i].d - pos); pos = a[i].d; while(p < 0) { if(Q.empty()) { flag = false; break; } p += Q.top(); Q.pop(); sum++; } if(!flag) break; Q.push(a[i].v); } if(!flag) printf("-1\n"); else printf("%d\n", sum); } return 0;}
0 0
- poj2431Expedition(贪心,优先队列)
- POJ2431Expedition (优先队列)
- poj1042(贪心+优先队列)
- POJ1456Supermarket(贪心+优先队列)
- poj3465(贪心+优先队列)
- 推销员(优先队列+贪心)
- poj3190(贪心,优先队列)
- Sunscreen (贪心+优先队列)
- hdu 2850(贪心+优先队列)
- POJ 2431 Expedition (优先队列+贪心)
- POJ 2431 Expedition (贪心 + 优先队列)
- POJ-3190-贪心(优先队列)
- hdu4544 优先队列(小贪心)
- poj 2431 Expedition (贪心+优先队列)
- POJ 1862 Stripies (贪心、优先队列)
- POJ 2431 Expedition (贪心、优先队列)
- POJ 3614 Sunscreen (贪心、优先队列)
- Hiking(vector+优先队列 实现贪心)
- test
- 一、Android网络编程基本知识
- Codeforces #367 B. Interesting drink(二分)
- 华为 oj 简单密码破解&&汽水瓶&&删除字符串中出现次数最少的字符&&字符串排序
- 类的继承和虚函数
- poj2431Expedition(贪心,优先队列)
- 10个免费在线测试网页性能工具
- hdoj1087Super Jumping! Jumping! Jumping!(上升序列求最大和)
- Go语言_LiteIDE下引用Github上的项目
- chrome浏览器默认跳转google.com而不是google.com/hk
- Qt设置密码输入框格式QLineEdit
- [LeetCode] Perfect Squares 完全平方数
- 二、Android网络框架
- 阻止关机