版本比较
代码缓存
正文
增加 0 行删除 81 行
13 行未改动1415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979813 行未改动---:::info[]
:::
```#include<bits/stdc++.h>#define LoveFurina ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)#define F(x, l, r) for(int x = l; x <= r; ++x)#define B(x, r, l) for(int x = r; x >= l; --x)#define int long longusing namespace std;
const int N = 2e5 + 10;
int n;struct Node { int a, b, t; bool operator < (const Node &x) const { return t > x.t; }} m[N];
priority_queue<Node> pq;
int ans = 0;
inline bool cmp(Node x, Node y) { return x.a == y.a ? x.t < y.t : x.a < y.a;}
signed main() { LoveFurina; cin >> n; F (i, 1, n) cin >> m[i].a >> m[i].b >> m[i].t; sort(m + 1, m + n + 1, cmp); int it = 0, beg = 0; // 贪心:选取从当前点开始,结束时间最早的 while (it < n || !pq.empty()) { // 将已经结束的弹出,若空则按序填入下一个 while (!pq.empty() && pq.top().b <= beg) pq.pop(); if (pq.empty()) { pq.push(m[++it]); beg = m[it].a; } // 取包含 beg 时间点,且 t 最小的区间 Node chos = pq.top(); int end = chos.b; // 如果仍能向后扩展则继续扩展 while (it < n) { Node &now = m[it + 1]; if (now.a >= end) break; // now 完全不被 chos 包含则无法再扩展 else if (now.t >= chos.t) { // now.t >= chos.t,则 chos 更优 it++; pq.push(now); continue; } else { // now.t < chos.t,则 now 更优 int beg1 = ceil(1.0 * (now.a - chos.a) / chos.t); int end1 = ceil(1.0 * (now.a + now.t - chos.a) / chos.t); end = beg1 == end1 ? beg1 : min(end, end1); // now 第一个区间被 chos 单个区间完全包含,则这一段 now 更优 // 否则取 chos.t 与 end1 最小值作为结束点 break; } } // 如果不能再扩展则计算当前段 chos 贡献 ans += (beg - end) / chos.t; beg = end; } cout << ans; return 0;}```