poj2431 优先队列
来源:互联网 发布:sql查询分析器使用 编辑:程序博客网 时间:2024/06/13 19:05
poj2431
题意:给你一辆有 P 单位油的卡车,让你开它走 L 单位距离到达小镇,每一单位距离消耗 1 单位油。告诉你沿途有 N 个加油站,每个加油站离小镇有 Ai 单位距离,在加油站中你将得到 Bi 单位的油。 问你若想到达城市你最少加油几次。
数据:是先给你加油站个数 N,之后是 N 个加油站的参数,再然后是 L 与 P 。并且加油站参数 Ai 是离小镇有多远,并不是离起点有多远。还有一点 N 个加油站不是按距离给的。。所以要先排序才可以。。。
思路:用优先队列就可以很简单地解决了。枚举从近到远(距起点)的 N 个加油站,若到第 i 个时仍有油或刚刚用好就将该加油站参数加进队列,若不是则将队列中的top取来加油P中再比较再将这个加入队列中。。。队列中的加油站都是已经经过的,那么我们就取油量最大的,且没有加过油的加油站加油就是此时的最优解啦。。。。
代码实现:
#include <iostream>#include <algorithm>#include <queue>#include <cstdio>#include <cstdlib>#include <cstring>using namespace std;struct node{ int num; int dis; friend bool operator < (node a, node b) { return a.num < b.num; }}tem, now_oil, oil[11000];priority_queue<struct node> q;int comp(node a, node b){ return a.dis > b.dis;}int main(){ int n; while(~scanf("%d", &n)) { int ans = 0; for(int i = 0; i < n; i++) { scanf("%d%d", &oil[i].dis, &oil[i].num); } sort(oil, oil+n, comp); int l, p; scanf("%d%d", &l, &p); for(int i = 0; i <= n; i++) { if(ans == -1) break; if(i < n) { now_oil.dis = l - oil[i].dis; now_oil.num = oil[i].num; } else now_oil.dis = l; while(now_oil.dis > p) { if(q.empty()) { ans = -1; break; } tem = q.top(); q.pop(); ans++; p += tem.num; } q.push(now_oil); } while(!q.empty()) q.pop(); printf("%d\n", ans); } return 0;}
若有错,请大家多多指教啦^ ^
0 0
- poj2431 Expedition 优先队列
- 优先队列--poj2431
- POJ2431 Expedition (优先队列)
- POJ2431 数据结构 (优先队列)
- poj2431(优先队列)
- poj2431 优先队列
- poj2431优先队列
- POJ2431(优先队列)
- POJ2431优先队列
- poj2431 优先队列
- poj2431 Expedition_STL优先队列
- poj2431 优先队列
- POJ2431 优先队列+贪心
- poj2431(优先队列)
- POJ2431 优先队列
- POJ2431 Expedition(排序+优先队列)
- [POJ2431]Expedition[贪心][优先队列]
- POJ2431 运用queueq优先队列
- 萌妹子图一张
- 【GDOI 2016 Day1】第二题 最长公共子串
- Android面试题集合
- gpg+expect自动解密脚本
- Android studio常用快捷键
- poj2431 优先队列
- Java出现No enclosing instance of type E is accessible
- 313. Super Ugly Number
- 计算几何
- easyUi--Uploadify文件上传
- 获取泛型中的数据并转换为数组
- Android SystemService的启动流程
- Java中equals和==的区别
- 0x00D83DDA 处有未经处理的异常(在 DXFont.exe 中): 0xC000041D: 用户回调期间遇到未经处理的异常