CSES - Putka Open 2020 – 3/5 - Results
Submission details
Task:Vaihdot
Sender:Gomhog
Submission time:2020-10-16 19:07:31 +0300
Language:C++ (C++11)
Status:READY
Result:100
Feedback
groupverdictscore
#1ACCEPTED35
#2ACCEPTED65
Test results
testverdicttimegroup
#1ACCEPTED0.01 s1, 2details
#2ACCEPTED0.03 s2details

Code

#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
#define F first
#define S second

using namespace std;

int n;
int add[101], a[101];

void smartSolve() {
	vector<pair<int,int>> ans;
	for (int i=1;i<n-2;i++) {
		if (add[i]==i) continue;
		if (add[i]==i+1) {
			ans.push_back({i+1,n});
			swap(a[i+1],a[n]);
			swap(add[a[n]],add[a[i+1]]);
			ans.push_back({i,n});
			swap(a[i],a[n]);
			swap(add[a[i]],add[a[n]]);
		} else {
			ans.push_back({i,add[i]});
			swap(a[i],a[add[i]]);
			swap(add[i],add[a[add[i]]]);
		}
		if (add[i]!=i) cerr<<"???"<<endl;
	}
	if (a[n-2]==n-2 && a[n-1]==n) {
		ans.push_back({1,n});
		ans.push_back({1,n-1});
		ans.push_back({1,n});
	}
	if (a[n-2]==n-1 && a[n-1]==n-2) {
		ans.push_back({n-2,n});
		ans.push_back({1,n});
		ans.push_back({1,n-1});
		ans.push_back({1,n});
		ans.push_back({n-2,n});
	}
	if (a[n-2]==n-1 && a[n-1]==n) {
		ans.push_back({n-2,n});
		ans.push_back({1,n});
		ans.push_back({1,n-1});
		ans.push_back({1,n});
	}
	if (a[n-2]==n && a[n-1]==n-2) {
		ans.push_back({1,n});
		ans.push_back({1,n-1});
		ans.push_back({1,n});
		ans.push_back({n-2,n});
	}
	if (a[n-2]==n && a[n-1]==n-1) {
		ans.push_back({n-2,n});
	}
	cout<<ans.size()<<"\n";
	for (auto x : ans) cout<<x.F<<" "<<x.S<<"\n";
}

void solve() {
	if (n==1) {
		cout<<"0\n";
		return;
	}
	if (n==2) {
		if (a[1]==1) cout<<"0\n";
		else cout<<"-1\n";
		return;
	}
	if (n==3) {
		if (a[2]!=2) {
			cout<<"-1\n";
			return;
		}
		if (a[1]==1) cout<<"0\n";
		else cout<<"1\n1 3\n";
		return;
	}
	smartSolve();
}

void readTst() {
	cin>>n;
	for (int i=1;i<=n;i++) {
		cin>>a[i];
		add[a[i]]=i;
	}
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int tst;
	cin>>tst;
	for (int t=0;t<tst;t++) {
		readTst();
		solve();
	}
}

Test details

Test 1

Group: 1, 2

Verdict: ACCEPTED

input
1000
1
1
2
1 2
...

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

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

Test 2

Group: 2

Verdict: ACCEPTED

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
81
1 58
2 45
3 16
4 29
...
Truncated