1 条题解

  • 0
    @ 2026-5-7 0:32:57

    好冷门的题,六个月了还没人写题解。
    哦不对,有一个人写题解了。
    哦不对,已经七个月了。

    link

    考虑直接模拟,时间复杂度 O(nt)O(nt)

    因为 tt 特别大,所以容易想到要么很快的就飞出格子,要么就无限死循环卡在那里不动。

    飞出去的情况很好判断,思考什么时候会死循环——RL

    也就是说,一个机器人只要遇到了形如 RL 的两个格子,就会被卡在那里。

    好吧,我承认我讲不出来了,看代码吧。

    #include<iostream>
    #include<cmath>
    using namespace std;
    const int N=1e6+10;
    int n;
    long long t;//开long long
    char c[N];
    int g[N],last[N],num=1;
    // g[i] 记录 i 所在块的编号;last[i] 表示所有在 i 块中的机器人若走了无数步数,最后会到块中的哪个位置。
    // 这里的块指最长连续相同字符的区间
    // 例如:      LL|RR|LLL|R
    // 块的编号为:1  2   3  4
    // last为:    1  4   5  8
    // 下标从一开始
    int cnt[N];
    int main(){
    	cin>>n>>c+1>>t;
    	if(c[1]=='L')last[1]=1;
    	for(int l=1,r=1;r<=n;r++){
    		if(c[l]==c[r])g[r]=num;
    		else{
    			if(c[r-1]=='R')last[num]=r-1;
    			g[r]=++num,l=r;
    			if(c[l]=='L')last[num]=l;
    		}
    	}
    	if(c[n]=='R')last[num]=n;
    	for(int i=1;i<=n;i++){
    		int res=last[g[i]];
    		if(c[i]=='L'&&res==1&&i<=t||c[i]=='R'&&res==n&&n-i+1<=t)continue;//飞了
    		else if(c[i]=='L'){
    			if(i-res>=t)cnt[i-t]++;//没有到达其他块
    			else cnt[res-(t-(i-res))%2]++;
          //走到res需要 i-res 步,还剩 t-(i-res) 步,模 2 余 0 在 L 处,否则在 R 处。
    		}
    		else{
    			if(res-i>=t)cnt[i+t]++;//没有到达其他块
    			else cnt[res+(t-(res-i))%2]++;
          //走到res需要 res-i 步,还剩 t-(res-i) 步,模 2 余 0 在 R 处,否则在 L 处。
    		}
    	}
    	for(int i=1;i<=n;i++)cout<<cnt[i]<<' ';
    	return 0;
    }
    

    时间复杂度:O(n)O(n)

    完结撒花。

    • 1

    信息

    ID
    10972
    时间
    1000ms
    内存
    256MiB
    难度
    10
    标签
    递交数
    2
    已通过
    1
    上传者