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 (优先队列)的更多相关文章

  1. poj 3431 Expedition 优先队列

    poj 3431 Expedition 优先队列 题目链接: http://poj.org/problem?id=2431 思路: 优先队列.对于一段能够达到的距离,优先选择其中能够加油最多的站点,这 ...

  2. POJ 2431 Expedition(探险)

    POJ 2431 Expedition(探险) Time Limit: 1000MS   Memory Limit: 65536K [Description] [题目描述] A group of co ...

  3. POJ 2431 Expedition (贪心+优先队列)

    题目地址:POJ 2431 将路过的加油站的加油量放到一个优先队列里,每次当油量不够时,就一直加队列里油量最大的直到能够到达下一站为止. 代码例如以下: #include <iostream&g ...

  4. poj 2431 【优先队列】

    poj 2431 Description A group of cows grabbed a truck and ventured on an expedition deep into the jun ...

  5. POJ 2431 Expedition (贪心 + 优先队列)

    题目链接:http://poj.org/problem?id=2431 题意:一辆卡车要行驶L单位距离,卡车上有P单位的汽油.一共有N个加油站,分别给出加油站距终点距离,及加油站可以加的油量.问卡车能 ...

  6. POJ 2431——Expedition(贪心,优先队列)

    链接:http://poj.org/problem?id=2431 题解 #include<iostream> #include<algorithm> #include< ...

  7. poj 2431 Expedition 贪心+优先队列 很好很好的一道题!!!

    Expedition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10025   Accepted: 2918 Descr ...

  8. POJ 2431 Expedition (优先队列+贪心)

    题目链接 Description A group of cows grabbed a truck and ventured on an expedition deep into the jungle. ...

  9. poj 2431 Expedition 贪心 优先队列 题解《挑战程序设计竞赛》

    地址 http://poj.org/problem?id=2431 题解 朴素想法就是dfs 经过该点的时候决定是否加油 中间加了一点剪枝 如果加油次数已经比已知最少的加油次数要大或者等于了 那么就剪 ...

随机推荐

  1. poj 1386 Play on Words 有向欧拉回路

    题目链接:http://poj.org/problem?id=1386 Some of the secret doors contain a very interesting word puzzle. ...

  2. matrix_last_acm_2

    2014年广州站网络赛 北大命题 password 123 B http://acm.hust.edu.cn/vjudge/contest/view.action?cid=97257#problem/ ...

  3. The 15th Zhejiang University Programming Contest

    a  ZOJ 3860 求和大家不一样的那个数,签到,map水之 #include<cstdio> #include<map> using namespace std; map ...

  4. Linux软件安装方法小结(附:rpm详解)(转载)

    在使用Linux系统的过程中,软件包的安装是避免不了的,在Linux下,软件安装程序的种类很多,安装方法也各式各样,(舒适性自然比不上windows :-))不过我们常见的软件包有两种: 1)含有软件 ...

  5. UNITY_MATRIX_IT_MV[Matrix] (转载)

    转载 http://blog.csdn.net/cubesky/article/details/38682975 前面发了一篇关于unity Matrix的文章. http://blog.csdn.n ...

  6. Unity3D判断鼠标向右或向左滑动,响应不同的事件

    private var first = Vector2.zero; private var second = Vector2.zero; function Update () { } function ...

  7. 利用GBDT模型构造新特征具体方法

    利用GBDT模型构造新特征具体方法 数据挖掘入门与实战  公众号: datadw   实际问题中,可直接用于机器学**模型的特征往往并不多.能否从"混乱"的原始log中挖掘到有用的 ...

  8. IE8中能继续使用Expression的解决方案

    在实际工作中,长的报表需要固定表头,比如DataGrid等控件. 过去在用IE8以前版本的时候,只需要在css中加上 position:relative ; top:expresion(this.of ...

  9. 树莓派/RaspberryPi 内核编译

    1.获取所需源码 1)下载地址: 官方网址:https://github.com/raspberrypi 上面列出了树莓派所有的开源软件: linux:内核源码 tools:编译内核和其他源码所需的工 ...

  10. 优化DB2缓冲页的大小

    零部件日结无法进行下去,建议配置C:\Program Files\SQLLIB目录下的db2cli.ini文件,加入此节:[DMSCNDB]CLIPkg=5 并重启DB2试试 另外,可以在命令行处理器 ...