背景:题有很多陷阱,专治理不认真理解题意的。 题目给的数据不一定像样例一样是有顺序的。而且不知为何-1,要用puts单独输出。期间代码里ans++,写错了顺序wa,没有找到原因,是重查代码的时候没有静心,一心ac。
思路:已经走过的加油站,就全部放到优先队列里,当油不够到达下一个加油站的时候,就取优先队列里最大的一个数来加油。
代码:
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int M = 10009, INF = 0x3fffffff;
int a[M], n, L, P;
pair<int, int> c[M];
int main(void) {
while(~scanf("%d", &n)) {
for(int i = 1; i <= n; i++) scanf("%d%d", &c[n - i + 1].first, &c[n - i + 1].second);
scanf("%d%d", &L, &P);
for(int i = 1; i <= n; i++) c[i].first = L - c[i].first;
sort(c + 1, c + n +1);
a[n + 1] = L - c[n].first;
for(int i = 1; i <= n; i++) {
if(i != 1) a[i] = c[i].first - c[i - 1].first;
else a[i] = c[i].first;
}
priority_queue<int> pq;
int ans = 0, ok = 1;
for(int i = 1; i <= n + 1; i++) {
if(P - a[i] < 0) {
if(pq.empty()) {ok = 0; puts("-1"); break;}
while(P - a[i] < 0 && !pq.empty()) {
P += pq.top();
pq.pop();
ans++;
}
if(P - a[i] < 0 && pq.empty()) {ok = 0; puts("-1"); break;}
}
pq.push(c[i].second);
P -= a[i];
}
if(ok) printf("%d\n", ans);
}
return 0;
}