题解:AT_abc392_c [ABC392C] Bib

封面

思路:

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

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

ii 个人戴着的帽子编号是 QiQ_i,而我们又知道 PiP_i 头上戴着的帽子编号是 QPiQ_{P_i},所以 ansQians_{Q_i} 会等于 QPiQ_{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;
}