CSES - Putka Open 2020 – 3/5 - Results
Submission details
Task:Vaihdot
Sender:Olli
Submission time:2020-10-17 09:22:18 +0300
Language:C++ (C++11)
Status:READY
Result:0
Feedback
groupverdictscore
#10
#20
Test results
testverdicttimegroup
#10.47 s1, 2details
#20.00 s2details

Compiler report

input/code.cpp: In function 'void solve()':
input/code.cpp:103: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;

const int N = 111;

bool h[N];

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 >= 3) {
			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);
	}

	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();
	}
}

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:

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
(empty)