CSES - Putka Open 2020 – 3/5 - Results
Submission details
Task:Vaihdot
Sender:rasastusni
Submission time:2020-10-17 17:26:38 +0300
Language:C++ (C++17)
Status:READY
Result:0
Feedback
groupverdictscore
#10
#20
Test results
testverdicttimegroup
#10.01 s1, 2details
#20.07 s2details

Compiler report

input/code.cpp: In function 'void solve(std::vector<int>&)':
input/code.cpp:9:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < v.size(); ++i) {
                  ~~^~~~~~~~~~
input/code.cpp:11:9: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   if (i == v.size() - 1) {
       ~~^~~~~~~~~~~~~~~
input/code.cpp:17:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    if (i + 2 < v.size()) {
        ~~~~~~^~~~~~~~~~
input/code.cpp:30:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for (int j = i + 2; j < v.size(); ++j) {
                        ~~^~~~~~~~~~

Code

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

using namespace std;

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 < v.size()) {
				k = i + 2;
			} else if (i > 0) {
				k = i - 1;
			} else {
				cout << -1 << endl;
				return;
			}
			swap(v[i], v[k]);
			m.push_back(make_pair(i+1, k+1));
			swap(v[i], v[i+1]);
			m.push_back(make_pair(i+1, i+2));
		} 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];
		}
		solve(v);
	}
}

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