Expedition
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 12980   Accepted: 3705

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.

 
题意:司机要从起点开到终点,中途会经过若干的加油站,每个加油站距离终点的距离以及每个加油站能够加多少油条件都会给出,求至少在多少个加油站加油能到达终点
思路:问题可以考虑成:当车还有油时每经过一个加油站可以将加油站压入堆中(按每个加油站能加油的多少来决定压入堆后加油站的位置,车要加油时有多的加油站可以先出队列),当油耗尽还没到下一个加油站时再从堆中将之前经过的加油站一个一个pop出来,直到这些油足够能撑到下一个加油站为止。记录一共加几次油。
AC代码:
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
const int N_MAX = ;
struct fuel_stop {
int ditance;
int amount;
bool operator <(const fuel_stop&b)const {
return amount<b.amount || (amount == b.amount &&this->ditance>b.ditance);
}
};
const bool cmp(const fuel_stop&a,const fuel_stop&b){
return a.ditance < b.ditance;
}
priority_queue<fuel_stop>que;
fuel_stop fuel[N_MAX+];
int main() {
int N;
while (cin >> N) {
int dist[N_MAX];
for (int i = ;i <N;i++)
scanf("%d%d",&dist[i],&fuel[i].amount);
int L, P;
cin >> L >> P;
for (int i = ;i <N;i++)
fuel[i].ditance = (L - dist[i]);
sort(fuel,fuel+N,cmp);
fuel[N].amount = ;fuel[N].ditance = L;//把终点当做一个特殊的加油站点
int pos = ,tank=P,ans=;//ans为加油次数,tank为油箱中油量,pos为当前位置
for (int i = ;i <=N;i++) {
int d = fuel[i].ditance - pos;//d为当前距离下一个加油站的距离
while (tank < d) {
if (que.empty()) {
cout<<-<<endl;
return ;
}
tank += que.top().amount;
ans++;
que.pop();
}
tank -= d;
pos = fuel[i].ditance;
que.push(fuel[i]);
}
cout << ans << endl;
}
return ;
}
 
 
 

poj 2431 Expedition的更多相关文章

  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. c#加密 可逆与不可逆MD5 加密

    1.方法一 (不可逆加密) srxljl public string EncryptPassword(string PasswordString,string PasswordFormat )     ...

  2. some websit

    Baidu:VideoView onVideoSizeChanged http://code.taobao.org/p/TangHuZhao/src/ http://code.taobao.org/p ...

  3. IOS 常用第三方库

    名称 特性 效果图 FXBlurView 实时背景模糊效果   FDFullscreenPopGesture 让UINavigationController在屏幕任何位置均可滑动返回 NJKWebVi ...

  4. Android 实现书籍翻页效果----升级篇

    自从之前发布了<Android 实现书籍翻页效果----完结篇 >之后,收到了很多朋友给我留言,前段时间由于事情较多,博客写得太匆忙很多细节地方没有描述清楚.所以不少人对其中的地方有不少不 ...

  5. rand值出现负数的解决方案

    当rand($number)或者mt_rand($number)的时候,$number太大,超过pow(2,31) - 1;即整型最大值的时候,会出现负数. 如果只是单纯转换成整数而已的话,可以采用s ...

  6. 第2章 数字之魅——寻找最大的K个数

    寻找最大的K个数 问题描述 在面试中,有下面的问答: 问:有很多个无序的数,我们姑且假定它们各不相等,怎么选出其中最大的若干个数呢? 答:可以这样写:int array[100] …… 问:好,如果有 ...

  7. UNIX基础知识之系统调用与库函数的区别与联系

    上图为UNIX操作系统的体系结构.内核的接口被称为系统调用(system call),公用函数库构建在系统调用接口之上,应用软件既可以使用公用函数库,也可直接使用系统调用. 更详细的说明如下: 所有操 ...

  8. Android:TabHost实现Tab切换

    TabHost是整个Tab的容器,包含TabWidget和FrameLayout两个部分,TabWidget是每个Tab的表情,FrameLayout是Tab内容. 实现方式有两种: 1.继承TabA ...

  9. c语言中的unsigned 和 signed

    我们来一起看下,C语言中,对于Integer Type(整数形式)的unsigned与signed两种形式的区别,以及在内存中的存储方式是如何的 Integer type(整数形式)是C语言中的基本数 ...

  10. debian root用户在shell下如何能够使用颜色

    转载:http://hi.baidu.com/aivera/item/f31c4a590ef72609e6c4a596 编辑 /root/.bashrc 这个文件, 把里面这几行前面的#号去掉就可以了 ...