题解:P14097 [POCamp 2022] 救火 / Brinnande träd

· · 题解

反悔贪心板子题。

思路很简单,如果当前的开始时间不比 X_i 晚,那就先拯救这棵树。一旦拯救不了、而且这棵树还比之前浪费时间最多的树要省时间,那就放弃取之前浪费时间最多的树,拯救这棵树。

过程可以用大根堆来维护。

\texttt{Code}

/*
Code by swate114514.
*/

#include <bits/stdc++.h>

using namespace std;

using i32 = int32_t;
using i64 = int64_t;
using ui32 = uint32_t;
using ui64 = uint64_t;
using PII = pair<int, int>;

int _;

namespace swate {
    inline int read() {int s = 0, w = 1; char ch = getchar();while (ch < '0' || ch > '9') {if (ch == '-') w *= -1;ch = getchar();}while (ch >= '0' && ch <= '9') {s = (s << 3) + (s << 1) + (ch ^ 48);ch = getchar();}return s * w;}
    inline void write(int x) {if (x < 0) x = -x, putchar('-');if (x > 9) write(x / 10);putchar(x % 10 + '0');}

    const int N = 4e5 + 5;
    int n, a[N], x[N], ans, sum;
    priority_queue<int> qu;

    inline void init() {

    }

    inline void solve() {
        n = read();
        for (int i = 1; i <= n; ++i) {
            x[i] = read();
        }
        for (int i = 1; i <= n; ++i) {
            a[i] = read();
        }
        for (int i = 1; i <= n; ++i) {
            // cout << sum << '\n';
            if (sum > x[i] && !qu.empty() && qu.top() > a[i]) {
                sum -= qu.top();
                qu.pop();
                ans--;
                // cout << 1 << ' ';
            }
            if (sum <= x[i]) {
                ans++;
                sum += a[i];
                qu.push(a[i]);
            }
            // sum += a[i];
        }
        write(ans);
    }
}

// #define Test

i32 main() {
#ifdef Test
    cin >> _;
#else
    _ = 1;
#endif

    for (int i = 1; i <= _; ++i) {
        swate::init();
        swate::solve();
    }

    return 0;
}