POJ 2431 Expedition (贪心、优先队列)
2016-07-28 18:30
281 查看
题目链接: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
.dis = L; //将终点添加到加油站末尾,方便计算
sta
.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;
}
题意:起点到终点之间有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
.dis = L; //将终点添加到加油站末尾,方便计算
sta
.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;
}
相关文章推荐
- Otter-入门篇2(Manager安装配置)
- HDU 5773 The All-purpose Zero (DP)
- createjs mask 填坑过程
- poj 2251 < bfs 求3D迷宫出路>
- VS使用命令行参数
- 从零开始搭建JAVA编程环境
- 轻松把玩HttpClient之设置代理,可以访问FaceBook
- 2016多校联合第四场 HDU5768
- tensorflow源码例子mnist源码——fully_connected_feed.py
- 轻松把玩HttpAsyncClient之模拟post请求示例
- GCC在C语言中内嵌汇编 asm __volatile__
- Maven的配置文件pom.xml
- tornado RequestHandler request.body & request.arguments
- [转]30分钟学会反向Ajax
- rehat 出现GDB debuginfo-install 问题处理
- 基于稀疏和HOG特征的人耳识别(代码)
- Unity树形结构
- 采用Spring管理Bean和依赖注入
- tensorflow源码例子mnist源码——mnist.py
- 【POJ】2251 - Dungeon Master(dfs + 队列)