[JSOI2007]建筑抢修
Description
Solution
这个题猫锟讲过我还不会……
就是按着deadline排一下序,先做能做的,如果一个东西做不了,那就把他和之前做的用时最长的换一下,这样总用时会少,也不影响其他的任务,何乐而不为呢。
Code
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
namespace wyx {
#define ll long long
ll read() {
ll ans = 0, fl = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') fl = -1;
c = getchar();
}
ans = c - '0';
for (c = getchar(); c >= '0' && c <= '9'; c = getchar())
ans = ans * 10 + c - '0';
return ans * fl;
}
#define pi std::pair<ll, ll>
#define dl first
#define tm second
const int N = 150010;
const int ha = 10000;
pi a[N];
std::priority_queue<ll> q;
int n;
void main() {
n = read();
for (int i = 1; i <= n; ++i) a[i].tm = read(), a[i].dl = read();
std::sort(a + 1, a + n + 1);
ll nw = 0, ans = 0;
for (int i = 1; i <= n; ++i) {
if (nw + a[i].tm <= a[i].dl)
nw += a[i].tm, ans++, q.push(a[i].tm); // 这里还忘入队了……
else if (!q.empty()) {
ll u = q.top();
if (u > a[i].tm) { // 贪心写反了……
q.pop();
q.push(a[i].tm);
nw -= u - a[i].tm;
}
}
}
printf("%lld\n", ans);
}
} // namespace wyx
int main() {
wyx::main();
return 0;
}