POJ 2431 Expedition 贪心 + 优先队列
来源:互联网 发布:fdd lte td lte 知乎 编辑:程序博客网 时间:2024/06/06 01:25
题目:
http://poj.org/problem?id=2431
题意:
一辆车从起点到终点,路程为l,初始油量p,车的油箱无限大,路上有n个加油站,给出加油站的位置(距离终点的位置)和储存的油量,问车能不能从起点开到终点,若能,给出最少的加油次数
思路:
首先把加油站的位置处理为距离起点的位置。可以认为车通过一个加油站,便获得了可以从当前加油站加油的权利,当车需要加油时,就从可以加油的加油站中选择储存油量最多的那个,一直重复这个过程即可。
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <queue>using namespace std;const int N = 100010, INF = 0x3f3f3f3f;struct node{ int x, y;} arr[N];bool cmp(node a, node b){ return a.x < b.x;}int main(){ int n, l, p; while(~ scanf("%d", &n)) { for(int i = 1; i <= n; i++) scanf("%d%d", &arr[i].x, &arr[i].y); scanf("%d%d", &l, &p); for(int i = 1; i <= n; i++) arr[i].x = l - arr[i].x; sort(arr+1, arr+1+n, cmp); priority_queue<int> que; int res = 0, i = 1; while(p < l)//油量和路程是1:1的,故直接把油量当路程了 { for(; i <= n; i++) { if(p >= arr[i].x) que.push(arr[i].y); else break; } if(! que.empty()) { p += que.top(); que.pop(); res++; } else break; } if(p < l) printf("-1\n"); else printf("%d\n", res); } return 0;}
0 0
- poj 2431 Expedition 贪心 + 优先队列
- POJ 2431 Expedition (优先队列+贪心)
- POJ 2431 Expedition (贪心 + 优先队列)
- poj 2431 Expedition 贪心 + 优先队列
- 2431Expedition POJ- 优先队列 + 贪心
- poj 2431 Expedition (贪心+优先队列)
- POJ 2431 Expedition (贪心+优先队列)
- POJ 2431 Expedition (贪心、优先队列)
- poj 2431 Expedition 贪心 + 优先队列
- poj 2431Expedition(优先队列+贪心)
- poj,2431Expedition 贪心 + 优先队列
- POJ 2431 Expedition (贪心、优先队列)
- Poj 2431 Expedition【优先队列+贪心】
- POJ 2431 Expedition 贪心 + 优先队列
- POJ 2431 Expedition 优先队列+贪心
- POJ 2431 Expedition 贪心 + 优先队列
- POJ 2431 Expedition (贪心+优先队列)
- POJ 2431 Expedition(贪心+优先队列)
- 关于ThinkPHP框架开发微信公众号在微信后台配置服务器URL提示Token验证失败的解决方案
- php 关于 preg_match 正则匹配值的输出
- ld加载so路径设置
- android studio 链接逍遥安卓
- go MD5 HASH 算法使用
- POJ 2431 Expedition 贪心 + 优先队列
- 通用点阵字库生成工具
- 判断imageView的image是否为空
- 学习笔记-DEMO:Js实现中英文翻译
- 打包jar包并混淆
- 呵呵
- App主界面布局的实现方式(一)
- softmax(python实现)
- running android lint has encountered a problem.