重生之我们只有一个小时打 ACM - L

· · 题解

:::info[其它系列]

重生之我们只有一个小时打 ACM - A

重生之我们只有一个小时打 ACM - B

重生之我们只有一个小时打 ACM - H

重生之我们只有一个小时打 ACM - I

重生之我们只有一个小时打 ACM - K :::

被队友带飞了,我是废物。

水贪心。

先按 t 排序,然后一个一个点加入,记录 s - w 的和,设为 tot,那么当 tot < 0 时就需要找前面的最小 s - w 的模块减去(相当于是删去了,数是肯定是负数),然后计数器加 1,最后输出答案即可。

直接暴力会 TLE,所以我们用一个堆维护即可。

:::success[Code]

#include <bits/stdc++.h>
#include <queue>
using namespace std;
#define int long long
#define rep(i, s, t) for (int i = s; i <= t; i++)
#define pre(i, s, t) for (int i = s; i >= t; i--)
const int N = 2e5 + 2;
int n, tot, res;
priority_queue<int, vector<int>, greater<int>> q;
struct node { int w, s, t; } a[N];
bool cmp(node &x, node &y) { return x.t < y.t; }
signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    cin >> n;
    rep(i, 1, n) cin >> a[i].w >> a[i].s >> a[i].t;
    sort(a + 1, a + n + 1, cmp);
    rep(i, 1, n) {
        tot += a[i].s - a[i].w;
        q.push(a[i].s - a[i].w);
        while (tot < 0) tot -= q.top(), q.pop(), res++;
    }
    cout << res << '\n';
    return 0;
}

:::