답안 #468815

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
468815 2021-08-29T18:35:44 Z errorgorn Cat (info1cup19_cat) C++17
25 / 100
568 ms 27620 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];
		rep(x,0,n) par[x]=0;
		
		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*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;
			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;
	}
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 16 ms 15948 KB Unexpected end of file - int32 expected
# 결과 실행 시간 메모리 Grader output
1 Correct 39 ms 16184 KB Output is correct
2 Correct 39 ms 16144 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 16 ms 15948 KB Unexpected end of file - int32 expected
# 결과 실행 시간 메모리 Grader output
1 Correct 39 ms 16184 KB Output is correct
2 Correct 39 ms 16144 KB Output is correct
3 Correct 545 ms 25700 KB Output is correct
4 Correct 504 ms 25056 KB Output is correct
5 Correct 568 ms 27620 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 16 ms 15948 KB Unexpected end of file - int32 expected