poj 2431

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.

题意:一辆卡车从起点行驶到终点路程为L,每行驶单位距离花费单位油量。起始时有油量P。在路途中有N个加油站,距离终点距离为Ai,有油量Bi。车如果没油就不能到达终点,假设车油箱无限大。问最少加几次油才能到达终点,不能达到输出-1。
题解:把加油站距离转化为到起点的距离便于操作。可以把问题看成在经过第i个加油站时,获得了加Bi油量的权利,等以后需要加油了就直接加。显然为了使加油次数最少,需要每次选尽可能大的Bi加油,所以需要优先队列。为方便操作,把终点也看成一个加油站。
 
 #include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
struct No{
int A;
int B;
};
No C[];
int N,P,L; bool cmp(No a,No b)
{
return a.A>b.A;
} int main()
{
int ans=;
while(cin>>N)
{
for(int i=;i<N;i++) cin>>C[i].A>>C[i].B;
cin>>L>>P;
sort(C,C+N,cmp);N++;
C[N].A=L,C[N].B=;
int pos=,tank=P;
priority_queue<int> que;
for(int i=;i<N;i++)
{
C[i].A=L-C[i].A;
int d=C[i].A-pos;
while(tank-d<)
{
if(que.empty()){
cout<<"-1"<<endl;
return ;
}
tank+=que.top();
que.pop();
ans++;
}
tank-=d;
pos=C[i].A;
que.push(C[i].B);
}
cout<<ans<<endl;
}
return ;
}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

poj 2431 【优先队列】的更多相关文章

  1. POJ 2431 优先队列

    汽车每过一单位消耗一单位油,其中有给定加油站可加油,问到达终点加油的最小次数. 做法很多的题,其中优先对列解这题是很经典的想法,枚举每个加油站,判断下当前油量是否小于0,小于0就在前面挑最大几个直至油 ...

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

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

  3. POJ 2431 Expedition(探险)

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

  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

    每次写poj的题都很崩溃,貌似从来没有一次一发就ac的,每次都有特别多的细节需要考虑.还有就是自己写的太粗糙了,应该把每种情况都想到的,总是急着交,然后刷一页wa. 优先队列直接用stl就可以,简单实 ...

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

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

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

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

随机推荐

  1. Delphi代码规范

    1. 前言 本文档主要是为Delphi开发人员提供一个源代码书写标准,以及程序和文件的命名标准,使他们在编程时有一致格式可遵循.这样,每个编程人员编写的代码能够被其他人理解. 2. 源程序书写规范 2 ...

  2. 线程安全之suspend(挂起) 和resume(执行)

    suspend()不会释放锁 如果加锁发生在resume()之前会发生死锁 t.join()是阻塞此方法,此线程再继续:通常用于在main()主线程内,等待其它线程完成再结束main()主线程.图中j ...

  3. MySQL抑制binlog日志中的BINLOG部分的方法

    1.mysqlbinlog之base64-output参数 --base64-output=value This option determines when events should be dis ...

  4. spring源码学习之AOP(一)

    继续源码学习,看了spring中基础的容器和AOP感觉自己也没有什么长进,哈哈,我也不知道到底有用没有,这可能是培养自己的一种精神吧,不管那么多,继续学习!AOP中 AOP中几个重要的概念:(1)Ad ...

  5. H5C3--过渡transition

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. Laravel-admin之Driver [] is not supported

    使用Laravel-admin做项目,原本好好的项目,今天一运行则报错:Driver [] is not supported,截图如下: 翻看百度翻译之后,才知道是不支持驱动器[],但是知道意思还是不 ...

  7. JS---元素隐藏的不同方式

    元素隐藏的不同方式 dispaly, visibility, opacity, height&border 为0 <!DOCTYPE html> <html lang=&qu ...

  8. Mysql 遇到的一些坑

    1.命令行按回车,或是输入\c,quit 都无法结束编辑状态,如图: 出现了"> ,然后不管输入什么都无法退出,这时输入>前的字符作为结束字符,再输入\c,既需要输入 " ...

  9. python中str的常用方法汇总(1)

    a = 'strABC' # Strabc : 首字母大写,其他全部小写 b = a.capitalize() print(b) # STRABC : 全部大写 c = a.upper() print ...

  10. python基础--函数的命名空间and作用域

    函数对象:函数是第一类对象,函数名指向的值是可以被当作参数进行传递的 1.函数名可以被传递 2.函数名可以被当作参数传递给其它函数 3.函数名可以被当作函数的返回值 4.函数名可以被当作容器类型的参数 ...