CSES - Putka Open 2020 – 3/5 - Results
Submission details
Task:Vaihdot
Sender:Olli
Submission time:2020-10-17 09:26:28 +0300
Language:C++ (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 'void solve()':
input/code.cpp:101:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0; i < ans.size(); ++i) {
                 ~~^~~~~~~~~~~~

Code

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

typedef pair<int, int> pii;

vector<pii> ans;

vector<int> v;
int n;

bool sol;

void solveBrute(int k, int d) {
	if(sol) return;
	if(d >= 6) return;
	bool thisSol = true;
	for(int i = 1; i <= k; ++i) {
		if(v[i-1] != i) {
			thisSol = false;
			break;
		}
	}
	if(thisSol) {
		sol = true;
		return;
	}


	for(int i = 0; i < k; ++i) {
		for(int j = i+2; j < k; ++j) {
			ans.push_back({i+1, j+1});
			swap(v[i], v[j]);
			solveBrute(k, d+1);
			if(sol) {
				break;
			}
			swap(v[i], v[j]);
			ans.pop_back();
		}
		if(sol) break;
	}
}

void solve() {
	if(n == 1) {
		cout << "0\n";
		return;
	}
	if(n == 2) {
		if(v[0] == 1) {
			cout << "0\n";
		} else {
			cout << "-1\n";
		}
		return;
	}
	if(n == 3) {
		if(v[1] != 2) {
			cout << "-1\n";
		} else {
			if(v[0] == 1) {
				cout << "0\n";
			} else {
				cout << "1\n";
				cout << "1 3\n";
			}
		}
		return;
	}
	for(int i = n; i >= 1; --i) {
		int j = -1;
		for(int k = 0; k < n; ++k) {
			if(v[k] == i) {
				j = k;
				break;
			}
		}
		if(j == i-1) continue;
		if(j < i-2) {
			ans.push_back({j+1, i});
			swap(v[j], v[i-1]);
			continue;
		}
		if(j >= 2) {
			ans.push_back({j+1, 1});
			swap(v[j], v[0]);
			ans.push_back({i, 1});
			swap(v[i-1], v[0]);
			continue;
		}

		solveBrute(4, 0);
		sol = false;
		break;
	}

	cout << ans.size() << "\n";
	for(int i = 0; i < ans.size(); ++i) {
		cout << ans[i].first << " " << ans[i].second << "\n";
	}
}

int main() {
	iostream::sync_with_stdio(false);
	cin.tie(0);
	
	int t;
	cin >> t;
	for(int q = 1; q <= t; ++q) {
		cin >> n;
		for(int i = 1; i <= n; ++i) {
			int a;
			cin >> a;
			v.push_back(a);
		}
		solve();
		v.clear();
		ans.clear();
	}
}

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