AtCoder Beginner Contest 158 F Removing Robots 逆向动归(dp)

AtCoder Beginner Contest 158   比赛人数8029   codeforces比赛结束不久,就开打了,还好状态还可以,就是网站太慢,浪费了20分钟.

AtCoder Beginner Contest 158 F Removing Robots  逆向动归(dp)

总目录详见https://blog.csdn.net/mrcrack/article/details/104454762

在线测评地址https://atcoder.jp/contests/abc158/tasks/abc158_f

思路同https://www.cnblogs.com/zdragon1104/p/12441877.html

题解:首先先将N个机器人按Xi升序排好序,令dp[x]代表x~n的集合数,当第x个机器人不激活时,那么就是dp[x+1];当第x个机器人被激活时,那么就是dp[y],y代表离x最近的不被x影响(包括间接)的机器人。

#include <cstdio>
#include <algorithm>
#define maxn 200010
#define LL long long
#define mod 998244353
using namespace std;
struct node{
	LL x,y;
}a[maxn];
int st[maxn],top,r;//st[]记录脚标,r用来记录留下的脚标
LL dp[maxn];
int cmp(node a,node b){
	return a.x<b.x;
}
int main(){
	int n,i;
	scanf("%d",&n);
	for(i=1;i<=n;i++)scanf("%lld%lld",&a[i].x,&a[i].y),a[i].y+=a[i].x;
	sort(a+1,a+1+n,cmp);//按机器人安放顺序进行排序
	a[n+1].x=2e9+1,a[n+1].y=2e9+1,st[++top]=n+1,dp[n+1]=1;//边界设置
	for(i=n;i>=1;i--){
		while(top&&a[i].y>a[st[top]].x)top--;
		r=st[top];
		st[++top]=i;
		dp[i]=(dp[i+1]+dp[r])%mod;//dp[n]=2的说明,第n位机器人不动是一种情况,动又是另一种情况
	}
	printf("%lld\n",dp[1]);
	return 0;
}
发布了620 篇原创文章 · 获赞 550 · 访问量 46万+

猜你喜欢

转载自blog.csdn.net/mrcrack/article/details/104950168
今日推荐