利用优先队列,找出经过加油站的最大油量,重复操作。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <queue>
using namespace std;
const int maxn = 10005;
struct stop{
int d,f;
friend bool operator<(const stop &a,const stop &b){
return a.d<b.d;
}
}st[maxn];
int main(){
int n;
while(~scanf("%d",&n)){
priority_queue<int>que;
for(int i = 0;i < n;i++)
scanf("%d%d",&st[i].d,&st[i].f);
int L,P;
scanf("%d%d",&L,&P);
for(int i = 0;i < n;i++)
st[i].d = L - st[i].d;
int ans = 0;
int pos = 0;
st[n].d = L;
st[n].f = 0;
sort(st, st+n+1);
int flag = 1;
for(int i = 0;i <= n;i++){
int temp = st[i].d - pos;
while(P < temp){
if(que.empty()){
flag = 0;
break;
}
P += que.top();
que.pop();
ans++;
}
P -= temp;
pos = st[i].d;
que.push(st[i].f);
}
if(flag)
printf("%d\n",ans);
else
printf("-1\n");
}
return 0;
}