Submission #950342

# Submission time Handle Problem Language Result Execution time Memory
950342 2024-03-20T08:38:35 Z pcc Cat (info1cup19_cat) C++17
100 / 100
361 ms 13992 KB
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define pll pair<ll,ll>
#define pii pair<int,int>
#define fs first
#define sc second
#define tlll tuple<ll,ll,ll>


const int mxn = 3e6+10;
int N;
int arr[mxn];
bitset<mxn> ch;
bitset<mxn> vis;
vector<pii> ans;
vector<pii> psyco;

void change(int a,int b){
	swap(arr[a],arr[b]);
	swap(arr[N-a+1],arr[N-b+1]);
	return;
}

void combine(int a,int b){
	ans.push_back(pii(a,b));
	change(a,b);
	return;
}

void work(int h){
	int now = min(arr[h],N+1-arr[h]);
	while(now != h){
		//cerr<<now<<":"<<h<<":"<<ch[now]<<endl;
		if(arr[h]>arr[N+1-h]){
			swap(arr[h],arr[N+1-h]);
			swap(arr[now],arr[N+1-now]);
			ans.push_back(pii(h,N+1-now));
		}
		else{
			ans.push_back(pii(h,now));
		}
		swap(arr[h],arr[now]);
		swap(arr[N+1-h],arr[N+1-now]);
		now = min(arr[h],N+1-arr[h]);
	};
	//for(int i = 1;i<=N;i++)cerr<<ch[i];cerr<<endl;
	return;
}

void calc(){
	vector<int> odds;
	for(int i = 1;i+i<=N;i++){
		if(vis[i])continue;
		psyco.push_back(pii(i,0));
		int now = i;
		do{
			vis[now] = true;
			psyco.back().sc += ch[now];
			int nxt = min(arr[now],N+1-arr[now]);
			now = nxt;
		}while(now != i);
		if(psyco.back().sc&1){
			odds.push_back(i);
			psyco.pop_back();
		}
	}
	for(int i = 0;i<odds.size();i+=2){
		combine(odds[i],odds[i+1]);
	}
	for(int i = 1;i+i<=N;i++){
		ch[i] = false;
		if(arr[i]>arr[N+1-i])ch[i] = true;
	}
	//for(int i = 1;i<=N;i++)cerr<<ch[i];cerr<<endl;
	for(auto &i:psyco)work(i.fs);
	for(int i = 0;i<odds.size();i+=2)work(odds[i]);
}

void solve(){
	for(int i = 0;i<=N;i++)vis[i] = ch[i] = false;
	psyco.clear();
	ans.clear();
	cin>>N;
	for(int i = 1;i<=N;i++)cin>>arr[i];
	int f = 0;
	for(int i = 1;i+i<=N;i++){
		if(arr[i]+arr[N-i+1] != N+1){
			cout<<"-1\n";
			return;
		}
		if(arr[i]>arr[N-i+1]){
			f++;
			ch[i] = true;
		}
	}
	//for(int i = 1;i<=N;i++)cerr<<ch[i];cerr<<endl<<endl;
	if(f&1){
		cout<<"-1\n";
		return;
	}
	calc();
	cout<<ans.size()<<' '<<ans.size()<<'\n';
	for(auto &i:ans)cout<<i.fs<<' '<<i.sc<<'\n';
	return;
}

int main(){
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int t;
	cin>>t;
	while(t--)solve();
}

Compilation message

cat.cpp: In function 'void calc()':
cat.cpp:69:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   69 |  for(int i = 0;i<odds.size();i+=2){
      |                ~^~~~~~~~~~~~
cat.cpp:78:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   78 |  for(int i = 0;i<odds.size();i+=2)work(odds[i]);
      |                ~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 2648 KB Output is correct
2 Correct 15 ms 2652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2396 KB Output is correct
2 Correct 15 ms 2648 KB Output is correct
3 Correct 15 ms 2652 KB Output is correct
4 Correct 17 ms 2648 KB Output is correct
5 Correct 7 ms 2396 KB Output is correct
6 Correct 6 ms 2392 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 2648 KB Output is correct
2 Correct 15 ms 2652 KB Output is correct
3 Correct 317 ms 12028 KB Output is correct
4 Correct 298 ms 11820 KB Output is correct
5 Correct 331 ms 13704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2396 KB Output is correct
2 Correct 15 ms 2648 KB Output is correct
3 Correct 15 ms 2652 KB Output is correct
4 Correct 17 ms 2648 KB Output is correct
5 Correct 7 ms 2396 KB Output is correct
6 Correct 6 ms 2392 KB Output is correct
7 Correct 317 ms 12028 KB Output is correct
8 Correct 298 ms 11820 KB Output is correct
9 Correct 331 ms 13704 KB Output is correct
10 Correct 326 ms 11472 KB Output is correct
11 Correct 295 ms 9940 KB Output is correct
12 Correct 314 ms 12728 KB Output is correct
13 Correct 361 ms 13992 KB Output is correct
14 Correct 312 ms 12064 KB Output is correct