跳到正文

题解

题解 ABC158F

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

文章信息

发布
2021年4月2日
收录
2026年11月4日
更新
2026年11月18日
篇幅
约 2 分钟
获赞
12

版本

  1. v1当前

觉得 dp 还是比较显然的,肯定得排一个序,然后倒着。因为我们能唯一能确定的状态就是 n+1n+1 这个编号,根本没有机器人,方案只有 11。

fif_i 表示后 ii 个的方案数。显然可以 fi=fi+1+fnextf_i = f_{i+1} + f_{next}, 其中 nextnext 是后面不受影响的。

问题在于后面那个怎么求。 next 即后面第一个大于 x+d−1x + d - 1 的,那不就是单调栈的最基本应用吗?

代码

C++
#include <cstdio>
#include <stack>
#include <algorithm>
const int N = 200005, p = 998244353;
int n, f[N];
std::pair<int, int> a[N];
std::stack<std::pair<int, int> > st;
int main() {
	scanf("%d", &n);
	for(int i = 1; i <= n; i++) scanf("%d%d", &a[i].first, &a[i].second);
	std::sort(a+1, a+1+n);
	f[n+1] = 1;
	for(int i = n; i >= 1; i--) {
		int t = a[i].first + a[i].second, next = i+1;
		while(!st.empty() && t > a[st.top().first].first) {
			next = st.top().second;
			st.pop();
		}
		st.push({i, next});
		f[i] = (f[i+1] + f[next]) % p;
	}
	printf("%d", f[1]);
}
原文
存档评论2 条抓取于