CSES - Putka Open 2020 – 3/5 - Results
Submission details
Task:Vaihdot
Sender:ArktinenKarpalo
Submission time:2020-10-16 19:36:47 +0300
Language:C++ (C++11)
Status:READY
Result:0
Feedback
groupverdictscore
#10
#20
Test results
testverdicttimegroup
#10.01 s1, 2details
#2ACCEPTED0.06 s2details

Code

#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define M 1000000007
#define N (1<<18)

using namespace std;

int n, t, a[1010], r[1010];

void asdfasdf(vector<pair<int,int>> *ans, int k1, int k2) {
	ans->push_back(make_pair(k1, k2));
	swap(r[a[k1]], r[a[k2]]);
	swap(a[k1], a[k2]);
}

int main() {
	cin.tie(0);
	cout.tie(0);
	ios_base::sync_with_stdio();
	cin >> t;
	for(int g=0; g<t; g++) {
		cin >> n;
		for(int i=1; i<=n; i++) {
			cin >> a[i];
			r[a[i]] = i;
		}
		if(n == 1) {
			cout << 0;
			cout << "\n";
		} else if(n == 2) {
			if(a[1] != 1)
				cout << -1;
			else
				cout << 0;
			cout << "\n";
		} else if(n == 3) {
			if(a[2] == 2) {
				if(a[1] == 1) {
					cout << 0;
				} else {
					cout << "1\n1 3";
				}
			} else {
				cout << -1;
			}
			cout << "\n";
		} else {
			vector<pair<int,int>> ans;
			if(a[1] != 1) {
				if(r[1] < 3) {
					asdfasdf(&ans, r[1], n);
				}
					asdfasdf(&ans, r[1], 1);
			}
			if(a[2] != 2) {
				if(r[2] < 4) {
					asdfasdf(&ans, r[2], n);
				}
				asdfasdf(&ans, r[2], 2);
			}
			for(int i=3; i<=n; i++) {
				int tmp1 = r[i];
				asdfasdf(&ans, r[i], 1);
				asdfasdf(&ans, r[i], i);
				if(a[1] != 1)
				asdfasdf(&ans, tmp1, 1);
			}
			cout << ans.size() << "\n";
			for(auto u:ans)
				cout << u.first << " " << u.second << "\n";
		}
	}
}

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