This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//雪花飄飄北風嘯嘯
//天地一片蒼茫
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#define ll long long
#define ii pair<ll,ll>
#define iii pair<ii,ll>
#define fi first
#define se second
#define endl '\n'
#define debug(x) cout << #x << ": " << x << endl
#define pub push_back
#define pob pop_back
#define puf push_front
#define pof pop_front
#define lb lower_bound
#define ub upper_bound
#define rep(x,start,end) for(auto x=(start)-((start)>(end));x!=(end)-((start)>(end));((start)<(end)?x++:x--))
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define indexed_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
//change less to less_equal for non distinct pbds, but erase will bug
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
const int MOD=998244353;
ll qexp(ll b,ll p,int m){
ll res=1;
while (p){
if (p&1) res=(res*b)%m;
b=(b*b)%m;
p>>=1;
}
return res;
}
ll inv(ll i){
return qexp(i,MOD-2,MOD);
}
ll fix(ll i){
i%=MOD;
if (i<0) i+=MOD;
return i;
}
ll fac[1000005];
ll ifac[1000005];
ll nCk(int i,int j){
if (i<j) return 0;
return fac[i]*ifac[j]%MOD*ifac[i-j]%MOD;
}
int n;
int arr[200005];
int par[200005];
bool vis[200005];
int brr[200005];
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin.exceptions(ios::badbit | ios::failbit);
fac[0]=1;
rep(x,1,1000005) fac[x]=fac[x-1]*x%MOD;
ifac[1000004]=inv(fac[1000004]);
rep(x,1000005,1) ifac[x-1]=ifac[x]*x%MOD;
int TC;
cin>>TC;
while (TC--){
cin>>n;
rep(x,0,n) cin>>arr[x];
rep(x,0,n) par[x]=0;
rep(x,0,n) brr[x]=arr[x];
bool can=true;
rep(x,0,n) if (arr[x]+arr[n-1-x]!=n+1) can=false;
if (!can){
cout<<"-1"<<endl;
continue;
}
n/=2;
rep(x,0,n) if (arr[x]>n) arr[x]=2*n+1-arr[x],par[x]=1;
rep(x,0,n) arr[x]--;
//rep(x,0,n) cout<<arr[x]<<" "; cout<<endl;
//rep(x,0,n) cout<<par[x]<<" "; cout<<endl;
vector<int> bad;
rep(x,0,n) vis[x]=false;
rep(x,0,n) if (!vis[x]){
int curr=x;
int npar=0;
int pos=-1;
do{
vis[curr]=true;
if (par[curr]) npar^=1,pos=curr;
curr=arr[curr];
} while (curr!=x);
if (npar) bad.pub(pos);
}
if (sz(bad)%2==1){
cout<<"-1"<<endl;
continue;
}
//for (auto &it:bad) cout<<it<<" "; cout<<endl;
vector<ii> v;
rep(x,0,sz(bad)/2){
v.pub(ii(bad[x*2]+1,2*n-bad[x*2+1]));
swap(arr[bad[x*2]],arr[bad[x*2+1]]);
par[bad[x*2]]=0,par[bad[x*2+1]]=0;
}
//rep(x,0,n) cout<<arr[x]<<" "; cout<<endl;
//rep(x,0,n) cout<<par[x]<<" "; cout<<endl;
rep(x,0,n) vis[x]=false;
rep(x,0,n) if (!vis[x]){
int curr=x;
do{
vis[curr]=true;
if (x!=curr){
if (par[x]){
v.pub(ii(x+1,2*n-curr));
par[x]=par[curr]^1;
}
else{
v.pub(ii(x+1,curr+1));
par[x]=par[curr];
}
}
curr=arr[curr];
} while (curr!=x);
}
cout<<sz(v)<<" "<<sz(v)<<endl;
for (auto &it:v) cout<<it.fi<<" "<<it.se<<endl;
for (auto &it:v){
swap(brr[it.fi-1],brr[it.se-1]);
swap(brr[n*2-it.fi],brr[n*2-it.se]);
}
//rep(x,0,n*2) cout<<brr[x]<<" "; cout<<endl;
}
}
# | 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... |