官网链接
D Cyclic Rotation
There is an array a of length n. You may perform the following operation any number of times:
- Choose two indices l and r where 1 ≤ l < r ≤ n and al=ar. Then, set a[l…r]=[al+1,al+2,…,ar,al].
You are also given another array b of length n which is a permutation of a. Determine whether it is possible to transform array a into an array b using the above operation some number of times.
Input
Each test contains multiple test cases. The first line contains a single integer t (1 ≤ t ≤ 104) — the number of test cases. The description of the test cases follows.
The first line of each test case contains an integer n (1 ≤ n ≤ 2⋅105) — the length of array a and b.
The second line of each test case contains n integers a1,a2,…,an (1 ≤ ai ≤ n) — elements of the array a.
The third line of each test case contains n integers b1,b2,…,bn (1 ≤ bi ≤ n) — elements of the array b.
It is guaranteed that b is a permutation of a.
It is guaranteed that the sum of n over all test cases does not exceed 2⋅105
Output
For each test case, print “YES” (without quotes) if it is possible to transform array a to b, 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
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
YES
YES
NO
YES
NO
Note
In the first test case, we can choose l=2 and r=5 to form [1,3,3,2,2].
In the second test case, we can choose l=2 and r=4 to form [1,4,2,2,1]. Then, we can choose l=1 and r=5 to form [4,2,2,1,1].
In the third test case, it can be proven that it is not possible to transform array a to b using the operation.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define lowbit(x) ((x)&(-x))
#define fi first
#define se second
#define endl '\n'
#define pb push_back
template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; }
template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; }
template <typename T> void inline read(T &x) {
int f = 1; x = 0; char c = getchar();
while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); }
while (c <= '9' && c >= '0') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
x *= f;
}
void solve()
{
int n;
cin >> n;
vector<int> a(n),b(n),c(n + 1);
for(int i = 0;i < n;i++) cin >> a[i];
for(int i = 0;i < n;i++) cin >> b[i];
int j = 0;//双指针
for(int i = 0;i < n;i++)
{
if(a[i] == b[j])
{
j++;
while(a[i] == b[j] && j < n && c[a[i]] > 0)
c[a[i]]--,j++;
}
else c[a[i]]++;
}
cout << (j == n ? "YES" : "NO") << endl;
}
int main()
{
IOS;
int T;
cin >> T;
while (T--)
{
solve();
}
return 0;
}
F1 Array Shuffling
oolimry has an array a of length n which he really likes. Today, you have changed his array to b, a permutation of a, to make him sad.
Because oolimry is only a duck, he can only perform the following operation to restore his array:
Choose two integers i,j such that 1 ≤ i,j ≤ n.
Swap bi and bj.
The sadness of the array b is the minimum number of operations needed to transform b into a.
Given the array a, find any array b which is a permutation of a that has the maximum sadness over all permutations of the array a.
Input
Each test contains multiple test cases. The first line contains a single integer t (1 ≤ t ≤ 104) — the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer n (1 ≤ n ≤ 2⋅105) — the length of the array.
The second line of each test case contains n integers a1,a2,…,an (1 ≤ ai ≤ n) — elements of the array a.
It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.
Output
For each test case, print n integers b1,b2,…,bn — describing the array b. If there are multiple answers, you may print any.
Example
input
2
2
2 1
4
1 2 3 3
output
1 2
3 3 2 1
Note
In the first test case, the array [1,2] has sadness 1. We can transform [1,2] into [2,1] using one operation with (i,j)=(1,2).
In the second test case, the array [3,3,2,1] has sadness 2. We can transform [3,3,2,1] into [1,2,3,3] with two operations with (i,j)=(1,4) and (i,j)=(2,3) respectively.
#include<bits/stdc++.h>
using ll = long long;
#define IOS std::ios::sync_with_stdio(0),std::cin.tie(0),std::cout.tie(0)
void solve()
{
int n;
std::cin >> n;
std::unordered_map<int,int> mp;
int mx = 0;
std::vector<int> a(n);
for(int i = 0;i < n;i++) std::cin >> a[i],mp[a[i]]++,mx = std::max(mx,mp[a[i]]);
auto v = a;
std::sort(v.begin(),v.end());
std::vector<std::vector<int>> ans (n + 1);//小细节
for(int i = 0;i < n;i++) ans[v[i]].push_back(v[(i + mx) % n]);
for(int i = 0;i < n;i++)
std::cout << ans[a[i]].back() << " \n"[i == n - 1],ans[a[i]].pop_back();
return;
}
int main()
{
IOS;
int T;
std::cin >> T;
while (T--)
{
solve();
}
return 0;
}