跳到正文

题解

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

_TwilightSnow_约 2 分钟
原文
文章信息与版本

文章信息

收录
2026年6月14日
更新
2026年6月20日
篇幅
约 2 分钟
获赞
1
题目
P16939

版本

  1. v2当前
  2. v1
其它系列

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

水贪心。

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

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

Code
C++
#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;
}
原文