1. 程式人生 > >[JSOI2007]建築搶修

[JSOI2007]建築搶修

des etc else if get name 不為 tor getc push

Description

BZOJ1029

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;
}

[JSOI2007]建築搶修