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

Compiler report

input/code.cpp: In function 'bool dfs(std::vector<int>, std::vector<std::pair<int, int> >)':
input/code.cpp:10:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < v.size(); ++i) {
                  ~~^~~~~~~~~~
input/code.cpp:13:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int j = 0; j < v.size(); ++j) {
                   ~~^~~~~~~~~~
input/code.cpp: In function 'void solve(std::vector<int>&)':
input/code.cpp:34:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < v.size(); ++i) {
                  ~~^~~~~~~~~~
input/code.cpp:36:9: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   if (i == v.size() - 1) {
       ~~^~~~~~~~~~~~~~~
input/code.cpp:43:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    else if (i + 3 < v.size()) k = i + 3;...

Code

#include <iostream>
#include <utility>
#include <vector>

using namespace std;

bool dfs(vector<int> v, vector<pair<int, int>> m) {
	if (m.size() > v.size()+2) return false;
	bool ok = true;
	for (int i = 0; i < v.size(); ++i) {
		if (v[i] == i+1) continue;
		ok = false;
		for (int j = 0; j < v.size(); ++j) {
			if (abs(j-i) <= 1) continue;
			auto v2 = v;
			auto m2 = m;
			swap(v2[i], v2[j]);
			m2.push_back(make_pair(i+1, j+1));
			if (dfs(v2, m2)) return true;
		}
	}
	if (ok) {
		cout << m.size() << endl;
		for (auto &p : m) {
			cout << p.first << " " << p.second << endl;
		}
		return true;
	}
	return false;
}

void solve(vector<int> &v) {
	vector<pair<int, int>> m;
	for (int i = 0; i < v.size(); ++i) {
		if (v[i] == i + 1) continue;
		if (i == v.size() - 1) {
			cout << -1 << endl;
			return;
		}
		if (v[i+1] == i + 1) {
			int k;
			if (i - 2 >= 0) k = i - 2;
			else if (i + 3 < v.size()) k = i + 3;
			else {
				cout << -1 << endl;
				return;
			}
			swap(v[i], v[k]);
			m.push_back(make_pair(i+1, k+1));
			swap(v[i+1], v[k]);
			m.push_back(make_pair(i+2, k+1));
			swap(v[i], v[k]);
			m.push_back(make_pair(i+1, k+1));
		} else {
			for (int j = i + 2; j < v.size(); ++j) {
				if (v[j] == i + 1) {
					swap(v[i], v[j]);
					m.push_back(make_pair(i+1, j+1));
				}
			}
		}
		if (m.size() > 5 * v.size()) {
			cout << -1 << endl;
			return;
		}
	}
	cout << m.size() << endl;
	for (auto &p : m) {
		cout << p.first << " " << p.second << endl;
	}
}

int main()
{
	int t;
	cin >> t;
	for (int i = 0; i < t; ++i) {
		int n;
		cin >> n;
		vector<int> v(n);
		for (int a = 0; a < n; ++a) {
			cin >> v[a];
		}
		if (n > 4) solve(v);
		else {
			if (!dfs(v, vector<pair<int, int>>())) {
				cout << -1 << endl;
			}
		}
	}
}

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