CSES - Putka Open 2020 – 3/5 - Results
Submission details
Task:Vaihdot
Sender:Laakeri
Submission time:2020-10-17 16:11:25 +0300
Language:C++11
Status:READY
Result:0
Feedback
groupverdictscore
#10
#20
Test results
testverdicttimegroup
#10.01 s1, 2details
#20.03 s2details

Code

#include <bits/stdc++.h>
using namespace std;

int a[111];

void solve() {
	int n;
	cin>>n;
	for (int i=1;i<=n;i++){
		cin>>a[i];
	}
	if (n==1) {
		cout<<0<<endl;
		return;
	}
	if (n==2) {
		if (a[1] == 1) {
			cout<<0<<endl;
		} else {
			cout<<-1<<endl;
		}
		return;
	}
	if (n==3) {
		if (a[2] != 2) {
			cout<<-1<<endl;
		} else {
			if (a[1] == 1) {
				cout<<0<<endl;
			} else {
				cout<<1<<endl;
				cout<<"1 3"<<endl;
			}
		}
		return;
	}
	vector<pair<int, int>> ans;
	for (int i=1;i<=n;i++){
		if (a[i] == i) continue;
		int p=0;
		for (int j=i+1;j<=n;j++){
			if (a[j] == i) {
				p=j;
			}
		}
		assert(p && a[p] == i);
		if (p>i+1){
			ans.push_back({i, p});
			swap(a[i], a[p]);
			continue;
		}
		if (p+1<n){
			ans.push_back({p, n});
			ans.push_back({n, i});
			swap(a[p], a[n]);
			swap(a[n], a[i]);
		} else {
			ans.push_back({p, 1});
			ans.push_back({1, n});
			ans.push_back({n, i});
			ans.push_back({p, 1});
			swap(a[p], a[1]);
			swap(a[1], a[n]);
			swap(a[n], a[i]);
			swap(a[p], a[1]);
			if (p == n) {
				ans.push_back({1, n});
				swap(a[1], a[n]);
			}
		}
	}
	cout<<ans.size()<<'\n';
	for (auto x : ans){
		cout<<x.first<<" "<<x.second<<'\n';
	}
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int tcs;
	cin>>tcs;
	for (int tc=1;tc<=tcs;tc++){
		solve();
	}
}

Test details

Test 1

Group: 1, 2

Verdict:

input
1000
1
1
2
1 2
...

correct output
0
0
-1
0
-1
...

user output
0
0
-1
0
-1
...

Test 2

Group: 2

Verdict:

input
1000
79
49 42 77 41 37 61 46 55 7 72 4...

correct output
81
67 79
70 78
3 77
60 76
...

user output
85
1 58
2 45
3 16
4 29
...