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.
Code
/*
^....0
^ .1 ^1^
.. 01
1.^ 1.0
^ 1 ^ ^0.1
1 ^ ^..^
0. ^ 0^
.0 1 .^
.1 ^0 .........001^
.1 1. .111100....01^
00 11^ ^1. .1^
1.^ ^0 0^
.^ ^0..1
.1 1..^
1 .0 ^ ^
00. ^^0.^
^ 0 ^^110.^
0 0 ^ ^^^10.01
^^ 10 1 1 ^^^1110.1
01 10 1.1 ^^^1111110
010 01 ^^ ^^^1111^1.^ ^^^
10 10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program:
Procedural thinking:
在卡车开往终点的图中,只有在加油站才可以加油。
可以认为,在到达加油站i的时候就获得了一次在之后的任何时候都可以加Bi单位汽油的权利,
即在之后需要加油的时候,认为是在之前经过的加油站加的油就可以了。
因此,希望到达终点时加油次数尽可能少,所以当燃料为0了之后再选加油量Bi最大的加油站。
在经过加油站i时,往优先队列里加入Bi;
当燃料空了时:
1.如果优先队列也是空的,则无法到达终点
2.否则取出优先队列中的最大元素,并用来给卡车加油
Functions required by the program:
Determination algorithm:
Determining data structure:
优先队列
*/
/* My dear Max said:
"I like you,
So the first bunch of sunshine I saw in the morning is you,
The first gentle breeze that passed through my ear is you,
The first star I see is also you.
The world I see is all your shadow."
FIGHTING FOR OUR FUTURE!!!
*/
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int N,L,P;
pair<int,int> Node[1000007];
void solve(){
priority_queue<int> que;
int ans=0,pos=0,tank=P;
for(int i=0;i<N;i++){
int d=Node[i].first-pos;
while(tank<d){
if(que.empty()){
cout<<"-1"<<endl;
return ;
}
tank+=que.top();
que.pop();
ans++;
}
tank-=d;
pos=Node[i].first;
que.push(Node[i].second);
}
cout<<ans<<endl;
}
int main(){
while (scanf("%d",&N)!=EOF){
for(int i=0;i<N;i++)
cin>>Node[i].first>>Node[i].second;
cin>>L>>P;
for(int i=0;i<N;i++)
Node[i].first=L-Node[i].first;
sort(Node,Node+N);
Node[N].first=L;Node[N].second=0;
N++;
solve();
}
return 0;
}