POJ 2431 Expedition (贪心、优先队列)
来源:互联网 发布:凸轮弹簧机编程 编辑:程序博客网 时间:2024/05/01 07:55
题目链接:http://poj.org/problem?id=2431
题意:起点到终点之间有n个加油站,输入第一行为n,后面n行每行是该加油站距终点的距离和能加多少油。1单位油能开1单位距离。最后一行是起点到终点的距离和初始油箱里有多少油。问最少加几次油能到终点,不能到的话输出-1 。
先将所有的加油站按距离起点的距离从小到大排序,然后模拟车开的过程,每经过一个加油站只要油箱里还有油就不加油,将所有经过的加油站的加油数量存到一个大顶堆里,然后每当油箱没油了就从前面的加油站里选一个能加最多油的加上,也就是取大顶堆的顶。这样一直到终点。若任何一次油箱没油了也加不上油那就输出-1。
————挑战程序设计竞赛
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <queue>using namespace std;int L, P; //L为起点距终点的距离,P为起始有多少油 struct node{int dis, oil; //dis为加油站距离起点的距离 ,oil为能加多少油 };node sta[10010];bool cmp(node a, node b) {return a.dis < b.dis;}int main() {int n;while(~scanf("%d", &n)) {int i, j;for(i = 0; i < n; i++) {scanf("%d %d", &sta[i].dis, &sta[i].oil);}scanf("%d %d", &L, &P);for(i = 0; i < n; i++) {sta[i].dis = L - sta[i].dis; //输入的是加油站距离终点的距离,要转换成距离起点 }sort(sta, sta + n, cmp); //按照距离起点距离升序排序 int ans = 0, pos = 0; //ans加多少次油, pos当前位置 priority_queue<int> q;sta[n].dis = L; //将终点添加到加油站末尾,方便计算 sta[n].oil = 0; //终点不能加油 for(i = 0; i <= n; i++) {int tmp = sta[i].dis - pos; //当前位置距下一个加油站的距离 while(tmp > P) { //不能开这么远的话要加油 if(q.empty()) {printf("-1\n");return 0;}else {P += q.top();q.pop();ans++;}}P -= tmp;pos = sta[i].dis; q.push(sta[i].oil);}printf("%d\n", ans);} return 0;}
0 0
- POJ 2431 Expedition (优先队列+贪心)
- POJ 2431 Expedition (贪心 + 优先队列)
- poj 2431 Expedition (贪心+优先队列)
- POJ 2431 Expedition (贪心、优先队列)
- poj 2431Expedition(优先队列+贪心)
- POJ 2431 Expedition (贪心、优先队列)
- POJ 2431 Expedition (贪心+优先队列)
- POJ 2431 Expedition(贪心+优先队列)
- POJ 2431 Expedition (贪心 + 优先队列)
- POJ 2431 Expedition (优先队列 + 贪心)
- POJ 2431 Expedition(贪心+优先队列)
- poj 2431 Expedition(优先队列+贪心)
- poj 2431 Expedition 贪心 + 优先队列
- poj 2431 Expedition 贪心 + 优先队列
- 2431Expedition POJ- 优先队列 + 贪心
- POJ 2431 Expedition (贪心+优先队列)
- poj 2431 Expedition 贪心 + 优先队列
- poj,2431Expedition 贪心 + 优先队列
- asp.net 使用 MongoDB 初体验
- 使用telnet发送邮件
- dsn 查询方式
- 信号量(sem)
- android MediaRecorder录制音视频实现直播的基础
- POJ 2431 Expedition (贪心、优先队列)
- LeetCode:Convert Sorted Array(List) to Binary Search Tree
- Flume简介和配置实战
- httpclient4 取得cookie信息并保存
- 域名解析
- red5整合tomcat下载
- 关于Mysql删除语句delete相关问题
- 创建一个窗口
- spring整合JMS一同步收发消息(基于ActiveMQ的实现)