解法在注释里
/*
这道题表面上看起来很复杂,对于每个加油站都要选择是否要加油
但可以注意到,每到一个加油站,只要汽车还有油,对于加油和不加油的两种选择,接下来的解法都是一样的
这也就导致了这道题可以用贪心来做
做法:到加油站的时候都暂时不加油,等汽车没油了,再从已经经过的加油站里选一个油量最大的,因为数据比较大,直接比较会超时,所以用优先队列
*/
#include<stdio.h>
#include<queue>
#include<algorithm>
using namespace std;
#define MAX_N 1000000
struct Stop {
int dist;
int fuel;
};
int n,l,p;
Stop stop[MAX_N];
priority_queue<int> que;
int cmp(Stop a,Stop b) {
return a.dist<b.dist;
}
int solve() {
int ans=0;
int i=0,post=0;
while(1) {
while(post+p>=stop[i].dist && post!=l) {
p-=stop[i].dist-post;
post=stop[i].dist;
que.push(stop[i].fuel);
i++;
}
if (post>=l) break;
if (que.empty()) return -1;
p+=que.top();
que.pop();
ans++;
}
return ans;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
while(scanf("%d",&n)!=EOF) {
int i;
for(i=0;i<n;i++) {
scanf("%d%d",&stop[i].dist,&stop[i].fuel);
}
scanf("%d%d",&l,&p);
stop[n].dist=l;
for(i=0;i<n;i++) {
stop[i].dist=l-stop[i].dist;
}
sort(&stop[0],&stop[n],cmp);
printf("%d\n",solve());
}
return 0;
}