poj - 2431 Expedition (优先队列)
http://poj.org/problem?id=2431
你需要驾驶一辆卡车做一次长途旅行,但是卡车每走一单位就会消耗掉一单位的油,如果没有油就走不了,为了修复卡车,卡车需要被开到距离最近的城镇,在当前位置和城镇之间有n个加油站可以加油。
为了减少危险,需要最少的加油次数,卡车的油箱可以看作无限大,卡车初始距离城镇L单位,自身有P单位的油。
注意输入的距离是与城镇的距离不是与开始点的距离。转换一下就好。
思想:把经过的所有加油站都加入优先队列,当燃料不足时就取出优先队列的最大元素,用来给卡车加油,如果优先队列也是空的,就不能到达终点。
#include <cstdio>
#include <queue>
#include <algorithm>
using namespace std;
const int maxn = + ;
int L, P, N;
struct node
{
int x,y; //x是与初始点的距离 y是这个加油站有多少油
bool operator < (const node &a) const
{
return x < a.x; //按与初始点的距离排序
}
}f[maxn]; void solve() {
f[N].x = L; //把终点也加入,但是油 为0 为了方便处理
f[N].y = ;
N++; priority_queue<int> que; int ans = , pos = , tank = P; // ans: 加油次数 , pos:现在所在位置 ,tank:邮箱中汽油的量
for(int i = ; i < N; i++) {
int d = f[i].x - pos; //接下来要前进的距离
while(tank - d < ) { //如果油不够 就不断加油
if(que.empty()) {
puts("-1");
return;
}
tank += que.top();
que.pop();
ans++;
}
tank -= d;
pos = f[i].x;
que.push(f[i].y);
}
printf("%d\n", ans);
} int main()
{
scanf("%d",&N);
for(int i = ; i < N; i++) {
scanf("%d%d",&f[i].x,&f[i].y);
}
scanf("%d%d",&L,&P);
for(int i = ; i < N; i++) {
f[i].x = L - f[i].x;
}
sort(f,f+N);
solve();
return ;
}
poj - 2431 Expedition (优先队列)的更多相关文章
- poj 3431 Expedition 优先队列
poj 3431 Expedition 优先队列 题目链接: http://poj.org/problem?id=2431 思路: 优先队列.对于一段能够达到的距离,优先选择其中能够加油最多的站点,这 ...
- POJ 2431 Expedition(探险)
POJ 2431 Expedition(探险) Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] A group of co ...
- POJ 2431 Expedition (贪心+优先队列)
题目地址:POJ 2431 将路过的加油站的加油量放到一个优先队列里,每次当油量不够时,就一直加队列里油量最大的直到能够到达下一站为止. 代码例如以下: #include <iostream&g ...
- poj 2431 【优先队列】
poj 2431 Description A group of cows grabbed a truck and ventured on an expedition deep into the jun ...
- POJ 2431 Expedition (贪心 + 优先队列)
题目链接:http://poj.org/problem?id=2431 题意:一辆卡车要行驶L单位距离,卡车上有P单位的汽油.一共有N个加油站,分别给出加油站距终点距离,及加油站可以加的油量.问卡车能 ...
- POJ 2431——Expedition(贪心,优先队列)
链接:http://poj.org/problem?id=2431 题解 #include<iostream> #include<algorithm> #include< ...
- poj 2431 Expedition 贪心+优先队列 很好很好的一道题!!!
Expedition Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10025 Accepted: 2918 Descr ...
- POJ 2431 Expedition (优先队列+贪心)
题目链接 Description A group of cows grabbed a truck and ventured on an expedition deep into the jungle. ...
- poj 2431 Expedition 贪心 优先队列 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=2431 题解 朴素想法就是dfs 经过该点的时候决定是否加油 中间加了一点剪枝 如果加油次数已经比已知最少的加油次数要大或者等于了 那么就剪 ...
随机推荐
- Vim配置文件(Vimrc)
嘛……后面的比赛基本都是在NOI Linux下进行了,windows下的开发环境基本都不能用了>_>果断转了vim,记录一下vim的配置文件- set nu syntax on filet ...
- 设计模式 - Template Method
今天下午主要研究了设计模式中的Template Method(模版方法设计模式). 在Spring中,对各种O/RM进行了封装,比如对Hibernate有HibernateTemplate封装:对JD ...
- noi2006day2_最大获利 网络流
这道题是上一题的数据加强版,dinic表示毫无压力: #include<iostream> #include<cstdio> #include<cstring> # ...
- centos6.5安装图形界面,windows远程linux图形界面
1. 查询是否已安装图形界面 yum grouplist |more 在grouplist的输出结果中的“Installed Groups:”部分中,如果你能找到“X Window System”和G ...
- JAVA数据结构系列 栈
java数据结构系列之栈 手写栈 1.利用链表做出栈,因为栈的特殊,插入删除操作都是在栈顶进行,链表不用担心栈的长度,所以链表再合适不过了,非常好用,不过它在插入和删除元素的时候,速度比数组栈慢,因为 ...
- POJ 2039
#include<iostream> #include<stdio.h> #include<string> #define MAXN 20 using namesp ...
- jvm性能调优---jstat的用法
Jstat是JDK自带的一个轻量级小工具.全称“Java Virtual Machine statistics monitoring tool”,它位于java的bin目录下,主要利用JVM内建的指令 ...
- POJ 3304 Segments (直线与线段是否相交)
题目链接 题意 : 能否找出一条直线使得所有给定的线段在该直线上的投影有一个公共点. 思路 : 假设存在一条直线a使得所有线段在该直线上的投影有公共点,则必存在一条垂直于直线a的直线b,直线b与所有线 ...
- ExtJs布局之Column
<!DOCTYPE html> <html> <head> <title>ExtJs</title> <meta http-equiv ...
- [Sharepoint]备份 迁移 还原
在sharepoint 的备份当中,一般分为一个sharepoint 站点的备份和sharepoint 整个站点的备份.我们可以用sharepoint designer 进行备份, 也可以用 stsa ...