Submission #468808

# Submission time Handle Problem Language Result Execution time Memory
468808 2021-08-29T18:15:52 Z errorgorn Cat (info1cup19_cat) C++17
25 / 100
547 ms 26812 KB
//雪花飄飄北風嘯嘯
//天地一片蒼茫

#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 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];
		
		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;
			return 0;
		}
		
		//for (auto &it:bad) cout<<it<<" "; cout<<endl;
	
		vector<ii> v;
		rep(x,0,sz(bad)/2){
			v.pub(ii(bad[x]+1,2*n-bad[x+1]));
			swap(arr[bad[x]],arr[bad[x+1]]);
			par[bad[x]]=0,par[bad[x+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;
			int npar=0;
			int pos=-1;
			
			do{
				vis[curr]=true;
				if (x!=curr){
					if (par[curr]) v.pub(ii(x+1,2*n-curr));
					else v.pub(ii(x+1,curr+1));				
				}
				curr=arr[curr];
			} while (curr!=x);
			
			if (npar) bad.pub(pos);
		}
		
		cout<<sz(v)<<" "<<sz(v)<<endl;
		for (auto &it:v) cout<<it.fi<<" "<<it.se<<endl;
	}
}
# Verdict Execution time Memory Grader output
1 Incorrect 17 ms 15948 KB Wrong answer
# Verdict Execution time Memory Grader output
1 Correct 47 ms 16148 KB Output is correct
2 Correct 39 ms 16104 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 17 ms 15948 KB Wrong answer
# Verdict Execution time Memory Grader output
1 Correct 47 ms 16148 KB Output is correct
2 Correct 39 ms 16104 KB Output is correct
3 Correct 525 ms 25348 KB Output is correct
4 Correct 504 ms 24576 KB Output is correct
5 Correct 547 ms 26812 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 17 ms 15948 KB Wrong answer