觉得 dp 还是比较显然的,肯定得排一个序,然后倒着。因为我们能唯一能确定的状态就是 这个编号,根本没有机器人,方案只有 。
表示后 个的方案数。显然可以 , 其中 是后面不受影响的。
问题在于后面那个怎么求。 next 即后面第一个大于 的,那不就是单调栈的最基本应用吗?
代码
#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]);
}