#include <bits/stdc++.h>
using namespace std;
#define print(l) for(auto i:l) cout<<i<<" ";cout<<endl;
#define input(t,l,n) vector<t>l(n);for(int i = 0;i<n;i++)cin>>l[i];
#define int long long
#define pb push_back
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define all(l) l.begin(),l.end()
#define pii pair<int,int>
#define fi first
#define se second
const int M = 1e9+7;
const int inf = 1e18;
int bp(int x, int y, int p){
int res = 1;
x = x % p;
while (y > 0) {
if (y & 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
int MI(int n, int p){
return bp(n, p - 2, p);
}
int mul(int x,int y, int p){
return x * 1ull * y % p;
}
int di(int x,int y, int p){
return mul(x, MI(y, p), p);
}
int n , m , k , q;
vector<int>a,b,bad;
vector<vector<int>>ansx,ans;
map<int,int>done,isbad;
void dox(){
// print(bad);
for(int i:bad){
if(isbad[i] == 0)continue;
if(isbad[b[i]-1] and i != b[i]-1){
isbad[i] = 0;
isbad[b[i]-1] = 0;
ansx.pb({i,b[i]-1});
// print(a);
// print(b);
int j = b[i]-1;
swap(b[i],a[j]);
swap(a[i],b[j]);
}
}
vector<int>lf;
for(int i:bad){
if(isbad[i])lf.pb(i);
}
// print(lf);
for(int i = 0;i<lf.size();i+=2){
int ii = lf[i];
int j = lf[i+1];
isbad[ii] = 0;
isbad[j] = 0;
// cout<<ii<<" "<<j<<endl;
ansx.pb({ii,j});
swap(a[ii],b[j]);
swap(b[ii],a[j]);
}
}
void solve(int testcase_number){
cin>>n;
input(int,A,n);
a.clear();
b.clear();
done.clear();
isbad.clear();
for(int i = 0;i<n/2;i++){
a.pb(A[i]);
}
for(int i = n-1;i>=n/2;i--){
b.pb(A[i]);
}
// reverse(all(b));
bad.clear();
for(int i = 0;i<n/2;i++){
int x = min(a[i],b[i]);
if(n-x+1 != max(a[i],b[i])){
cout<<-1<<endl;
return;
}
if(a[i] > b[i]){
bad.pb(i);
isbad[i] = 1;
}
}
if(bad.size()%2){
cout<<-1<<endl;
return;
}
dox();
// print(a);
// print(b);
// dop();
for(auto i:ansx){
ans.pb({i[0]+1,n-i[1]});
}
// print(a);
// print(b);
map<int,int>ind;
for(int i = 0;i<n/2;i++){
ind[a[i]] = i;
}
for(int i = 1;i<=n/2;i++){
if(ind[i] != i-1){
int j = ind[i];
ans.pb({ind[i]+1,i});
swap(ind[i],ind[a[i-1]]);
swap(a[i-1],a[j]);
swap(b[j],b[i-1]);
}
}
// for(int i = 0;i<n/2;i++){
// if(a[i] != i+1){
// int j = a[i]-1;
// cout<<i+1<<" "<<j+1<<endl;
// swap(a[i],a[j]);
// swap(b[i],b[j]);
// ans.pb({i+1,j+1});
// }
// }
cout<<ans.size()<<" "<<ans.size()<<endl;
for(auto i:ans){
print(i);
}
// print(a);
// print(b);
ansx.clear();
ans.clear();
}
signed main(){
ios::sync_with_stdio(0);//DO NOT USE IN INTERACTIVE
cin.tie(0), cout.tie(0);
cout << fixed<<setprecision(9);
int t = 1;
cin>>t;
for(int i = 1;i<=t;i++){
solve(i);
}
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |