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. word-break属性和css换行显示

    这几天在做项目的时候,遇到了比较棘手的问题,便是在一个标签里边展示内容,如果说展示中文汉字,一点问题都没有,但是只要连续展示英文字母或者中文的标点符号(中间不带空格),那么所渲染的内容就不会换行,而是 ...

  2. 双向链表C++实现

    双向链表实现,通过C++实现 #ifndef LinkList_hpp #define LinkList_hpp typedef struct Node{ int data; Node* next; ...

  3. MFC框架下Opengl窗口闪屏问题解决方案

    转自https://blog.csdn.net/niusiqiang/article/details/43116153 虽然启用了双缓冲,但是仍然会出闪屏的情况,这是由于OpenGL自己有刷新背景的函 ...

  4. Reflection (computer programming) -反射-自身结构信息

    n computer science, reflection is the ability of a computer program to examine, introspect, and modi ...

  5. day19-1 迭代器,三元表达式,列表推导式,字典生成式,

    目录 迭代器 可迭代对象 迭代器对象 总结 三元表达式(三目表达式) 列表推导式 字典生成式 迭代器 可迭代对象 拥有iter方法的对象就是可迭代对象 # 以下都是可迭代的对象 st = '123'. ...

  6. Tensorflow学习笔记----基础(3)

    目录: 一.TensorFlow的系统架构 二.TensorFlow的设计理念 三.TensorFlow的运行流程 四.TensorFlow的编程模型:边.节点.图.设备.变量.变量初始化.内核 五. ...

  7. ASP.NET Menu控件点击区域太小解决方法

    ASP.NET自带的Menu控件点击区域比较小,基本就是文本范围和图片范围,在区域外虽然选择的项有颜色变化,但是这个时候点击是没有用的,体验不是很好 检查前台生成的HTML,是用td嵌套a标签,a标签 ...

  8. Tensorflow学习笔记——Summary用法

    tensorboard 作为一款可视化神器,可以说是学习tensorflow时模型训练以及参数可视化的法宝. 而在训练过程中,主要用到了tf.summary()的各类方法,能够保存训练过程以及参数分布 ...

  9. 利用GitHub搭建Hexo博客并开启HTTPS

    Hexo 是一个快速.简洁且高效的博客框架.Hexo 使用 Markdown(或其他渲染引擎)解析文章,在几秒内,即可利用靓丽的主题生成静态网页. GitHub 是一个面向开源及私有软件项目的托管平台 ...

  10. .NET开源项目一览