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. Codeforces Gym 100431G Persistent Queue 可持久化队列

    Problem G. Persistent QueueTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudg ...

  2. 利用text插件和css插件优化web应用

    JavaScript的模块化开发到如今,已经相当成熟了,当然,一个应用包含的不仅仅有js,还有html模板和css文件. 那么,如何将html和css也一起打包,来减少没必要的HTTP请求数呢? 本文 ...

  3. 内核工具 – Sparse 简介

    转载:http://www.cnblogs.com/wang_yb/p/3575039.html Sparse是内核代码静态分析工具, 能够帮助我们找出代码中的隐患. 主要内容: Sparse 介绍 ...

  4. Invalidate、RedrawWindow与UpdateWindow

    Invalidate.RedrawWindow与UpdateWindow的区别 Invalidate()是强制系统进行重画,但是不一定就马上进行重画.因为Invalidate()只是通知系统,此 时的 ...

  5. JAVA基础之StringBuilder基础方法

    StringBuilder sb = new StringBulder("kikikiki"); sb.apend("123")    //追加 输出kikik ...

  6. vs2010 MFC执行流程

    从大学时候学过到现在可能又要用到,搞这个东西真是痛苦.不过首先把繁琐的c++函数执行流程搞明白吧,好好多设置几个断点观测一下啦. Step1.在vs2010新建MFC工程,那么对应的代码页名字为:CS ...

  7. Ubuntu环境下配置Nginx

    /etc/nginx目录文件下: drwxr-xr-x   5 root root 4096 Apr 27 12:47 ./ drwxr-xr-x 104 root root 4096 Apr 27 ...

  8. SQL Server 格式化时间format

    select format(sysdatetime(),'yyyy-MM-dd HH:mm:ss'); SQL Server 2012才开始有这功能 这种样式很像oracle的to_char(sysd ...

  9. Adobe Edge Animate –获取鼠标位置及跟随鼠标功能实现

    Adobe Edge Animate –获取鼠标位置及跟随鼠标功能实现 版权声明: 本文版权属于 北京联友天下科技发展有限公司. 转载的时候请注明版权和原文地址. 在网络上浏览有关Edge相关问题的时 ...

  10. [改善Java代码]equals应该考虑null值的情景

    建议46: equals应该考虑null值情景 继续上一建议的问题,我们解决了覆写equals的自反性问题,是不是就很完美了呢?再把main方法重构一下: public class Client { ...