题解:P11457 [USACO24DEC] Job Completion G
XYM20230422 · · 题解
思路分析
前置题目:
P4053 [JSOI2007] 建筑抢修
P11328 [NOISG 2022 Finals] Gym Badges
这是一道反悔贪心,先考虑一种情况:
设
若要使第
化简得
排完序后再考虑一种情况:
如果
可知:
所以可以得出:
所以去掉一个最大的可以保证一定可以放下。
上代码:
#include <bits/stdc++.h>
using namespace std;
long long n, t;
struct ttt {
long long v, e;
} a[600000];
bool cmp(ttt a, ttt b) {
if (a.e == b.e) {
return a.v < b.v;
}
return a.e+a.v < b.e+b.v;
}
int main() {
cin >> t;
while (t--) {
cin >> n;
memset(a, 0, sizeof a);
for (long long i = 1; i <= n; i++) {
cin >> a[i].e >> a[i].v;
}
sort(a + 1, a + 1 + n, cmp);
priority_queue<long long>q;
long long ans = 0, anm = 0;
for (long long i = 1; i <= n; i++) {
q.push(a[i].v);
ans++;
anm += a[i].v;
if (anm - a[i].v > a[i].e) {
anm -= q.top();
ans--;
q.pop();
}
}
cout << ans << endl;
}
}