题解:abc392_c Bib

· · 题解

思路:

对于第 i 个人他看着的人是 P_i,而 P_i 头上戴着的帽子编号是 Q_{P_i}

定义一个 ans 数组,其中 ans_i 表示头上戴着的帽子编号为 i 的人他看着的人头上戴着的帽子的编号。

i 个人戴着的帽子编号是 Q_i,而我们又知道 P_i 头上戴着的帽子编号是 Q_{P_i},所以 ans_{Q_i} 会等于 Q_{P_i}

代码:

#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define itn int
#define ull unsigned long long
LL N;
LL P[int(3*1e5+15)],Q[int(3*1e5+15)];
LL ans[int(3*1e5+15)];
int main(){
    cin>>N;
    for(int i=1;i<=N;i++){
        cin>>P[i];
    }
    for(int i=1;i<=N;i++){
        cin>>Q[i];
    }
    for(int i=1;i<=N;i++){
        ans[Q[i]]=Q[P[i]];
    }
    for(int i=1;i<=N;i++)cout<<ans[i]<<" ";
    return 0;
}