跳到正文

闲话

代码缓存

DanseSolo约 4 分钟
原文

这是 2026年5月29日 存档的第 4 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

收录
2026年3月16日
更新
2026年8月9日
篇幅
约 4 分钟
获赞
1

版本

  1. v5当前
  2. v4
  3. v3
  4. v2
更早的 1 个版本
  1. v1

蓝\mathbf{\large{\color{blue}蓝}} 剩余:00。

INFO

紫\mathbf{\large{\color{purple}紫}} 剩余:00。

INFO

黑\mathbf{\large{\color{black}黑}} 剩余:00。

INFO
C++
#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 long
using 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;
}