Problem - 1442B - Codeforces
题目大意:给定你一个大小为 n n n的排列 a a a,现在你可以选择当前 a a a中的任意一个元素 a i a_i ai,然后移除 a i a_i ai,将 a i − 1 a_{i-1} ai−1或 a i + 1 a_{i+1} ai+1加入数组 b b b,求有多少种方式,可以通过排列 a a a生成数组 b b b。
解题思路:模拟,对于每个 b i b_i bi,至多只有两种办法来得到,那就是在排列 a a a中找到对应 b i b_i bi的 a j a_j aj,如果此时 a j − 1 , a j + 1 a_{j-1}, a_{j+1} aj−1,aj+1都不在 b i b_i bi的后面,那么就有两种,如果 a j − 1 a_{j-1} aj−1或者 a j + 1 a_{j+1} aj+1在 b i b_i bi的后面,那么一定没办法选择 a j − 1 a_{j-1} aj−1或 a j + 1 a_{j+1} aj+1。当 b i b_i bi成功的加入序列 b b b之后,它也可以作为被移除的选择,所以整个过程就是这样不断模拟的过程。
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define syncfalse ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
const int N = 2e5+5;
int a[N], b[N], id[N];
const int mod = 998244353;
void solve(){
int n, m;
cin>>n>>m;
for (int i = 1; i <= n; ++i){
cin>>a[i];
id[a[i]]=i;
}
set<int>s;
for (int i = 1; i <= m; ++i){
cin>>b[i];
s.insert(b[i]);
}
ll ans = 1;
for (int i = 1; i <= m; ++i){
int index = id[b[i]];
int vl = 0, vr = 0;
if (index>1)vl = a[index-1];
if (index<n)vr = a[index+1];
int now = 0;
if (vl!=0&&!s.count(vl))now++;
if (vr!=0&&!s.count(vr))now++;
ans=(ans*now)%mod;
if (now!=0)s.erase(b[i]);
}
cout << ans << "\n";
}
int main(){
syncfalse
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
int t;
cin>>t;
for (;t;--t){
solve();
}
return 0;
}