由于油箱是无限的,所以每次经过加油站都不选择加油,等到后面油不够了,再来“反悔”,把前面经过的油站的油加上,尽可能选量大的,用优先队列维护。
//#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<iostream> #include<sstream> #include<cmath> #include<climits> #include<string> #include<map> #include<queue> #include<vector> #include<stack> #include<set> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int,int> pii; #define pb(a) push(a) #define INF 0x1f1f1f1f #define lson idx<<1,l,mid #define rson idx<<1|1,mid+1,r #define PI 3.1415926535898 template<class T> T min(const T& a,const T& b,const T& c) { return min(min(a,b),min(a,c)); } template<class T> T max(const T& a,const T& b,const T& c) { return max(max(a,b),max(a,c)); } void debug() { #ifdef ONLINE_JUDGE #else freopen("in.txt","r",stdin); //freopen("d:\out1.txt","w",stdout); #endif } int getch() { int ch; while((ch=getchar())!=EOF) { if(ch!=' '&&ch!=' ')return ch; } return EOF; } const int maxn=10005; pair<int,int> da[maxn]; int n,L,P; void solve() { priority_queue<int> q; int now=0,remain=P,cnt=0; int flag=0; for(int i=0;i<n;i++) { if(remain>=L-now){flag=1;break;} while(remain<da[i].first-now) { if(!q.empty()) { int x=q.top();q.pop(); remain+=x; cnt++; }else {printf("-1 "); return ;} } remain-=(da[i].first-now); now=da[i].first; q.push(da[i].second); } while(remain<L-now&&!q.empty()) { remain+=q.top();q.pop(); cnt++; } if(remain>=L-now) flag=1; printf("%d ",flag?cnt:-1); } int main() { while(scanf("%d",&n)!=EOF) { for(int i=0;i<n;i++) { int a,b;scanf("%d%d",&a,&b); da[i]=make_pair(a,b); } scanf("%d%d",&L,&P); for(int i=0;i<n;i++) da[i].first=L-da[i].first; sort(da,da+n); solve(); } return 0; }