CF Codeforces Global Round 20 D题
There is an array aa of length nn. You may perform the following operation any number of times:
- Choose two indices ll and rr where 1≤l<r≤n1≤l<r≤n and al=aral=ar. Then, set a[l…r]=[al+1,al+2,…,ar,al]a[l…r]=[al+1,al+2,…,ar,al].
You are also given another array bb of length nn which is a permutation of aa. Determine whether it is possible to transform array aa into an array bb using the above operation some number of times.
Input
Each test contains multiple test cases. The first line contains a single integer tt (1≤t≤1041≤t≤104) — the number of test cases. The description of the test cases follows.
The first line of each test case contains an integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the length of array aa and bb.
The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤n1≤ai≤n) — elements of the array aa.
The third line of each test case contains nn integers b1,b2,…,bnb1,b2,…,bn (1≤bi≤n1≤bi≤n) — elements of the array bb.
It is guaranteed that bb is a permutation of aa.
It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052⋅105
Output
For each test case, print "YES" (without quotes) if it is possible to transform array aa to bb, and "NO" (without quotes) otherwise.
You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
Example
input
Copy
5 5 1 2 3 3 2 1 3 3 2 2 5 1 2 4 2 1 4 2 2 1 1 5 2 4 5 5 2 2 2 4 5 5 3 1 2 3 1 2 3 3 1 1 2 2 1 1
output
Copy
YES YES NO YES NO
Note
In the first test case, we can choose l=2l=2 and r=5r=5 to form [1,3,3,2,2][1,3,3,2,2].
In the second test case, we can choose l=2l=2 and r=4r=4 to form [1,4,2,2,1][1,4,2,2,1]. Then, we can choose l=1l=1 and r=5r=5 to form [4,2,2,1,1][4,2,2,1,1].
In the third test case, it can be proven that it is not possible to transform array aa to bb using the operation.
思路:这个题说要我们将数组a变成数组b,我们可以反向操作,我们去改变b,看b能否改变成a。这里b数组的操作是有规则的,因为a是由从前往后插的,所以这里需要在b反向操作时判断是否为插入时也要反向判断,即b[i]=b[i-1],因为b[i]是多余的!b[i] 跑到后面它现在是多余的!只有这样才能说明b[i]是由前面得来的,而且一定切记我们移动的是b[i],而不是b[i-1]。我们可以将b[i]移动到 j-1 前的任意位置。有两个数组,所以我们用双指针,现在就会有4情况:1.当b[j]=a[i]时,我们将 j 与 i 同时++;2.当它俩不相等时,我们先判断是否能够移动b来实现,何时能够移动b呢?首先就是b的前一个与当前的相等,然后放哪呢?当然是放在与a值相同的位置,并且这个a值没有和b匹配过。3.就是当何种情况下a值与b不匹配,就是 i 小于等于 n 里任意一个数,也就是当前a值与当前的b值不同。4.还剩到达不了的状态,那就是a数组都存完了(存的是不能匹配的数),而b数组没有走完,且b不能匹配a没有匹配的(这里注意的是要按顺序匹配,因为不按顺序很可能就是一种不合法状态转移过来的,防止不合法状态)
完整代码:
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int mod=1e9+7;
const int N=2e5+10;
int a[N],b[N];
int cnt[N];
void solve()
{
memset(cnt,0,sizeof cnt);
int n;
cin>>n;
for(int i=1;i<=n;i++)cin>>a[i];
for(int i=1;i<=n;i++)cin>>b[i];
int j=1,i=1;
bool ok=true;
while(j<=n)
{
if(a[i]==b[j]&&i<=n)i++,j++;
else
{
if(cnt[b[j]]&&b[j]==b[j-1])cnt[b[j]]--,j++;
else if(i<=n)cnt[a[i]]++,i++;
else
{
ok=false;
break;
}
}
}
if(ok)cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin>>t;
while(t--)
{
solve();
}
return 0;
}