1.我们发现题目所述操作只能使得一个数字往后移动
2.可得出若a数组想变为b数组那么只需从后往前遍历a数组,当遇到与b数组相同的连续的数字且可以填的时候,填进去,若最后b数组能被填完则可以
3.为了方便处理,先预处理b数组,将一段连续的相同序列使用pair存储,first代表权值,second代表数量
#include<bits/stdc++.h>
#define endl '\n'
#define pb push_back
#define pp pop_back
#define x first
#define y second
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int N = 1e6+5, M = 1e6+5, MOD = 998244353;
int n, a[N], b[N];
int cnt[N];
void solve() {
cin >> n;
for(int i = 1; i <= n; i ++)
cin >> a[i];
for(int i = 1; i <= n; i ++)
cin >> b[i];
for(int i = 1; i <= n; i ++)
cnt[i] = 0;
int res = 1;
vector<PII> v;
for(int i = 2; i <= n; i ++) {
if(b[i] == b[i-1]) res ++;
else {
v.pb({b[i-1], res});
res = 1;
}
}
v.pb({b[n], res});
int p = 0;
for(int i = 1; i <= n; i ++) {
cnt[a[i]] ++;
if(a[i] == v[p].x && cnt[a[i]] >= v[p].y) {
cnt[a[i]] -= v[p].y;
p ++;
}
}
if(p == v.size()) cout << "YES\n";
else cout << "NO\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
int t = 1;
cin >> t;
while(t --) solve();
return 0;
}