Expedition
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 18655   Accepted: 5405

Description

A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Being rather poor drivers, the cows unfortunately managed to run over a rock and puncture the truck's fuel tank. The truck now leaks one unit of fuel every unit of distance it travels.

To repair the truck, the cows need to drive to the nearest town (no more than 1,000,000 units distant) down a long, winding road. On this road, between the town and the current location of the truck, there are N (1 <= N <= 10,000) fuel stops where the cows can stop to acquire additional fuel (1..100 units at each stop).

The jungle is a dangerous place for humans and is especially dangerous for cows. Therefore, the cows want to make the minimum possible number of stops for fuel on the way to the town. Fortunately, the capacity of the fuel tank on their truck is so large that there is effectively no limit to the amount of fuel it can hold. The truck is currently L units away from the town and has P units of fuel (1 <= P <= 1,000,000).

Determine the minimum number of stops needed to reach the town, or if the cows cannot reach the town at all.

Input

* Line 1: A single integer, N

* Lines 2..N+1: Each line contains two space-separated integers describing a fuel stop: The first integer is the distance from the town to the stop; the second is the amount of fuel available at that stop.

* Line N+2: Two space-separated integers, L and P

Output

* Line 1: A single integer giving the minimum number of fuel stops necessary to reach the town. If it is not possible to reach the town, output -1.

Sample Input

4
4 4
5 2
11 5
15 10
25 10

Sample Output

2

Hint

INPUT DETAILS:

The truck is 25 units away from the town; the truck has 10 units of fuel. Along the road, there are 4 fuel stops at distances 4, 5, 11, and 15 from the town (so these are initially at distances 21, 20, 14, and 10 from the truck). These fuel stops can supply up to 4, 2, 5, and 10 units of fuel, respectively.

OUTPUT DETAILS:

Drive 10 units, stop to acquire 10 more units of fuel, drive 4 more units, stop to acquire 5 more units of fuel, then drive to the town.

 
 
 
 
 
 
题意:我现在开着汽车,车上有p升油,离目的地有l公里,每升油能跑1公里。
    中途会有n个加油站,第i个加油站距目的地a[i]公里,可以加b[i]升油。
    问:能否到达终点,如果能到输出最少加油次数。
 
 
解析:我们可以每次都把当前的油跑完,然后看经过了哪些加油站,找一个能加最多的加油(假设我当时就加过油),
    然后继续跑,记录加油的次数即可。
 
    可以用multiset或者优先队列解这道题,即存走过的加油点。
 
代码:
#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
#include <set>
using namespace std;
typedef long long ll;
#define INF 2147483647 struct node{ int a;int b;
}s[]; multiset <int> t;
multiset <int>::iterator it; bool cmp(node x,node y){
return x.a < y.a;
} int main(){
int n,l,p;
cin >> n;
for(int i = ;i < n; i++){
cin >> s[i].a >> s[i].b;
}
cin >> l >> p;
for(int i = ;i < n; i++){
s[i].a = l-s[i].a;
}
sort(s,s+n,cmp); int ans = ; int con = p;
int k = ;
while(con < l){
for(;s[k].a <= con; k++){
int e = s[k].b;
t.insert(e);
}
if(t.size() < ){
cout << - << endl;
return ;
}
it = t.end();it--;
con += *it;
ans ++;
t.erase(it);
}
cout << ans << endl;
return ;
}

POJ 2431 Expedition (priority_queue或者multiset可解)的更多相关文章

  1. POJ 2431 Expedition(探险)

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

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

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

  3. POJ 2431 Expedition (STL 优先权队列)

    Expedition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8053   Accepted: 2359 Descri ...

  4. poj - 2431 Expedition (优先队列)

    http://poj.org/problem?id=2431 你需要驾驶一辆卡车做一次长途旅行,但是卡车每走一单位就会消耗掉一单位的油,如果没有油就走不了,为了修复卡车,卡车需要被开到距离最近的城镇, ...

  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 贪心 优先队列 题解《挑战程序设计竞赛》

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

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

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

  9. POJ 2431 Expedition(优先队列、贪心)

    题目链接: 传送门 Expedition Time Limit: 1000MS     Memory Limit: 65536K 题目描述 驾驶一辆卡车行驶L单位距离.最开始有P单位的汽油.卡车每开1 ...

随机推荐

  1. [makefile]如何设置不同目录的代码(.c),生成到指定目录下(./debug/.o))

    部分代码跟makefile不在同一目录,有没有好的方法来设置依赖关系,我找到三种方法,但感觉都不完美,下面我会把他列出来并加以说明,不知有没有更好的方法,makefile本身也不是很熟,请大家指教: ...

  2. android编译ffmpeg+x264

    下载最新版的x264ftp://ftp.videolan.org/pub/videolan/x264/snapshots/1.解压到指定的目录2.切换当前目录为该目录3.创建一个shell脚本buil ...

  3. Neo4j沙盒实验申请过程步骤(图文详解)

    不多说,直接上干货! 参考博客 http://blog.csdn.net/u012318074/article/details/72793632    (对此表示感谢) 前期博客 我暂时是将Neo4j ...

  4. SqlServer数据库字典

    网上有很多SQL Server数据库字典的SQL语句,七零八落,我在工作整理了一下思路,总结SQL代码如下.数据库字典包括表结构(分2K和2005).索引和主键. 外键.约束.视图.函数.存储过程.触 ...

  5. Web api 测试 工具WebApiTestClient

    1.打开Nuget  安装 WebApiTestClient 2.在HelpPageConfig.cs 里面添加这段文字 config.SetDocumentationProvider(new Xml ...

  6. java中常用的转义字符

    Day02_SHJavaTraing_4-3-2017 Java中允许使用转义字符‘\’来将其后的字符转变为特殊字符型常量. 一.JAVA中常用的转义字符

  7. 路飞学城Python-Day37

    36-多表查询练习 37-权限管理 1.创建账号 本地账号 create user 'panda' @ 'loacalhost' inentified by'123' 远程账号 create user ...

  8. 获得a-b的差[返回BigDecimal 类型]

    /*** * 返回 a-b 的差 [返回 BigDecimal 类型] * @param a 被减数 * @param b 减数 * @return */ public static BigDecim ...

  9. P3387 【模板】缩点 && P3388 【模板】割点(割顶)

    Tarjan算法 应用: 有向图的强连通分量 无向图割点和桥 双连通分量 接下来主要谈论前面两者的应用(主要是第三种还没学会) 算法简要介绍 我们需要先理解一下知识:搜索树 有向图的搜索树的4种边,如 ...

  10. python链接mysql数据库

    1.安装pycharm python3.6    pip 在windows+R  cmd where pip pip install mysql-client 如何看自已mysql-client有没有 ...