poj2431

#include <iostream>
#include <algorithm>
#include <queue>

using namespace std;

struct st {
	int d;
	int f;
} s[10005];

bool cmp(st a, st b) {
	return a.d < b.d;
}

int n, l, p, i;

int main() {
	
	while(~scanf("%d", &n)) {
		
		for(i=0; i<n; i++) {
			scanf("%d%d", &s[i].d, &s[i].f);
		}
			
		scanf("%d%d", &l, &p);
		
		for(i=0; i<n; i++)
			s[i].d = l - s[i].d;
		
		s[n].d = l;
		s[n].f = 0;
		n++;
		
		sort(s, s+n, cmp);
		
		priority_queue<int> q;
		
		int ans = 0, pos = 0, tank = p;
		for(i=0; i<n; i++) {
			
			int dis = s[i].d - pos;
			
			while(tank < dis) {
				if(q.empty()) {
					printf("-1\n");
					return 0;
				}			
			
				tank += q.top();
				q.pop();
				ans++;
			}
			
			tank -= dis;
			pos = s[i].d;
			q.push(s[i].f);
		}
		
		printf("%d\n", ans);
	}
	
	return 0;
}

posted @ 2016-07-09 15:34  StevenLuke  阅读(120)  评论(0编辑  收藏  举报