跳到正文

题解

题解:P12574 [UOI 2021] 机器人

wkz_b20_efjb8hhc约 6 分钟
原文
文章信息与版本

文章信息

收录
2026年5月19日
更新
2026年5月21日
篇幅
约 6 分钟
题目
P12574

版本

  1. v2当前
  2. v1

这应该是一个极好的题解。

点击这里一口气看完完整题目

算法

由于直接模拟每一个机器人,时间复杂度为 O(n×t)\mathcal{O}(n \times t),是会直接超时的,所以我们可以使用倍增法来缩短时间。

运动规律

每个机器人根据当前所在格子的字母决定移动方向,由于字母固定,机器人的运动轨迹是确定的。不难发现,机器人的运动只有以下两种情况:

  1. 直线运动:若机器人一直向同一方向移动且不遇到反向格子,最终会从边界掉出。
  2. 振荡运动:当机器人进入相邻的 RL 或 LR 格子对时,会在两个格子间来回振荡,周期为 22。

初始预处理

定义函数 next⁡(i)\operatorname{next}(i) 表示从格子 ii 出发经过 11 秒后的位置:

next⁡(i)={i+1,if si=R ∧ i<ni−1,if si=L ∧ i>1−1,otherwise\operatorname{next}(i) = \begin{cases} i+1, & \text{if } s_i = R \ \wedge\ i < n \\[4pt] i-1, & \text{if } s_i = L \ \wedge\ i > 1 \\[4pt] -1, & \text{otherwise} \end{cases}

设 f(k)(i)f^{(k)}(i) 表示从位置 ii 出发经过 2k2^k 秒后的位置(若出界则为 −1-1)。

Base case (k=0k=0):

f(0)(i)=next⁡(i),∀i∈{1,2,…,n}f^{(0)}(i) = \operatorname{next}(i), \quad \forall i \in \{1, 2, \dots, n\}

Recurrence (k≥1k \ge 1):

f(k)(i)={f(k−1) ⁣(f(k−1)(i)),if f(k−1)(i)≠−1−1,if f(k−1)(i)=−1f^{(k)}(i) = \begin{cases} f^{(k-1)}\!\left( f^{(k-1)}(i) \right), & \text{if } f^{(k-1)}(i) \neq -1 \\[4pt] -1, & \text{if } f^{(k-1)}(i) = -1 \end{cases}

预处理计算所有 i∈[1,n]i \in [1, n] 和 k∈[0,⌊log⁡2t⌋]k \in [0, \lfloor \log_2 t \rfloor] 的 f(k)(i)f^{(k)}(i)。


查询步骤

对于每个初始位置 i∈{1,2,…,n}i \in \{1, 2, \dots, n\}:

  1. Initialization
cur←i,rem←tcur \leftarrow i, \quad rem \leftarrow t
  1. Binary lifting

令 K=⌊log⁡2t⌋K = \lfloor \log_2 t \rfloor,对于 k=K,K−1,…,0k = K, K-1, \dots, 0:

if rem≥2k ∧ cur≠−1  then:\text{if } rem \ge 2^k \ \wedge\ cur \neq -1 \ \text{ then:} cur←f(k)(cur)rem←rem−2k\begin{aligned} cur &\leftarrow f^{(k)}(cur) \\ rem &\leftarrow rem - 2^k \end{aligned}
  1. Counting
if cur≠−1  then:ans[cur]←ans[cur]+1\text{if } cur \neq -1 \ \text{ then:} \quad ans[cur] \leftarrow ans[cur] + 1

遍历所有 ii 并输出序列 ans[1],ans[2],…,ans[n]ans[1], ans[2], \dots, ans[n]。

具体代码

C++
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;        //简单的宏定义
const int N=1e6+5;           //最多格子数
const int LOG=60;            //倍增表最大层数,即 log_2 t
int n;                       //格子数
ll t;                        //时间
int f[N][LOG+5];             //跳转
int ans[N];                  //答案
string s;                    //各个格子状态
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);  //加快输入输出
    cin>>n>>s>>t;

    // 预处理走 2^0 步 (1步)
    for(int i=1;i<=n;i++){
        if(s[i-1]=='R'){
            f[i][0]=(i<n)?i+1:-1;//判断越界
        }else{
            f[i][0]=(i>1)?i-1:-1;//判断越界
        }
    }

    // 倍增预处理
    for(int k=1;k<=LOG;k++){
        for(int i=1;i<=n;i++){//遍历每个格子
            if(f[i][k-1]!=-1){
                f[i][k]=f[f[i][k-1]][k-1];//递推
            }else{
                f[i][k]=-1;//越界
            }
        }
    }

    // 查询步骤
    for(int i=1;i<=n;i++){
        int cur=i;
        ll rem=t;//表示剩余要走的步数
        for(int k=LOG;k>=0;k--){
            if(rem>=(1LL<<k)){//判断能不能跳这一步
                if(cur==-1) break;//判断越界
                cur=f[cur][k];
                rem-=(1LL<<k);
            }
        }
        if(cur!=-1){
            ans[cur]++;//该格子机器人数量增加
        }
    }

    // 遍历输出答案
    for(int i=1;i<=n;i++){
        cout<<ans[i]<<" ";
    }
    return 0;
}

此代码时间复杂度在 O(nlog⁡2t)\mathcal{O}(n \log_2 t),可以完美通过本题。

原文