CSES - Putka Open 2020 – 3/5 - Results
Submission details
Task:Vaihdot
Sender:mango_lassi
Submission time:2020-10-17 18:46:48 +0300
Language:C++11
Status:READY
Result:100
Feedback
groupverdictscore
#1ACCEPTED35
#2ACCEPTED65
Test results
testverdicttimegroup
#1ACCEPTED0.01 s1, 2details
#2ACCEPTED0.03 s2details

Compiler report

input/code.cpp: In function 'int getInd(int, const std::vector<int>&)':
input/code.cpp:6:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < cur.size(); ++i) {
                  ~~^~~~~~~~~~~~

Code

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

int getInd(int v, const vector<int>& cur) {
	for (int i = 0; i < cur.size(); ++i) {
		if (cur[i] == v) return i;
	}
	return -1;
}

void makeSwap(int a, int b, vector<int>& cur, vector<pair<int, int>>& res) {
	res.emplace_back(a+1, b+1);
	swap(cur[a], cur[b]);
}

void solve() {
	int n;
	cin >> n;

	vector<int> as(n);
	for (int& a : as) {
		cin >> a;
		--a;
	}

	if (n == 1) cout << 0 << '\n';
	else if (n == 2) {
		if (as[0] != 0) cout << -1 << '\n';
		else cout << 0 << '\n';
	} else if (n == 3) {
		if (as[1] != 1) cout << -1 << '\n';
		else if (as[0] == 0) cout << 0 << '\n';
		else cout << 1 << '\n' << 1 << ' ' << 3 << '\n';
	} else {
		vector<pair<int, int>> res;
		for (int t = n-1; t > 3; --t) {
			int ind = getInd(t, as);
			if (ind == t) continue;
			if (ind == t-1) {
				makeSwap(0, ind, as, res);
				ind = 0;
			}
			makeSwap(ind, t, as, res);
		}

		int ind = getInd(1, as);
		if (ind == 2) {
			makeSwap(0, 2, as, res);
			ind = 0;
		}
		if (ind == 0) {
			makeSwap(0, 3, as, res);
			ind = 3;
		}
		if (ind == 3) {
			makeSwap(1, 3, as, res);
		}

		ind = getInd(2, as);
		if (ind == 3) {
			makeSwap(0, 3, as, res);
			ind = 0;
		}
		if (ind == 0) {
			makeSwap(0, 2, as, res);
		}
		
		if (as[0] != 0) makeSwap(0, 3, as, res);

		cout << res.size() << '\n';
		for (auto pr : res) cout << pr.first << ' ' << pr.second << '\n';
	}
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);

	int t;
	cin >> t;
	for (int ti = 0; ti < t; ++ti) 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
...

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
67 79
70 78
3 77
60 76
...