CSES - Putka Open 2020 – 3/5 - Results
Submission details
Task:Vaihdot
Sender:Mahtimursu
Submission time:2020-10-16 18:20:19 +0300
Language:C++ (C++11)
Status:READY
Result:0
Feedback
groupverdictscore
#10
#20
Test results
testverdicttimegroup
#10.01 s1, 2details
#20.01 s2details

Code

#include <bits/stdc++.h>

typedef long long ll;

#define M 1000000007

using namespace std;

void test_case() {
	int n;
	cin >> n; 
    vector<int> v(n);

    for (int i = 0; i < n; ++i) {
        cin >> v[i];
    }

    vector<pair<int, int>> res;

    for (int i = 0; i < n; ++i) {
        if (v[i] == i + 1) continue; // oikea
        int id = v[i];
        if (abs(id - i - 1) == 1) continue;
        int tg = v[v[i]-1];
        v[v[i]-1] = v[i];
        v[i] = tg;
        res.push_back({i+1, id});
    }

    if (!is_sorted(v.begin(), v.end())) {
        if (n < 4) {
            cout << -1 << "\n";
            return;
        }

        for (int i = 0; i < n; ++i) {
            if (v[i] == i + 1) continue;

            // Use first
            if (i >= n - 2) {
                res.push_back({i+1, 1});
                res.push_back({i+2, 1});
                res.push_back({i+1, 1});
                v[i] = i + 1;
                v[i + 1] = i + 2;
            } else { // use last
                res.push_back({i+1, n});
                res.push_back({i+2, n});
                res.push_back({i+1, n});
                v[i] = i + 1;
                v[i + 1] = i + 2;
            }
        }
    }

    // VASTAUS
    cout << res.size() << "\n";

    for (auto val : res) {
        cout << val.first << " " << val.second << "\n";
    }
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	int t;
	cin >> t;
	for (int i = 0; i < t; ++i) {
		test_case();
		//cout << "\n";
	}

	return 0;
}

Test details

Test 1

Group: 1, 2

Verdict:

input
1000
1
1
2
1 2
...

correct output
0
0
-1
0
-1
...

user output
(empty)

Error:
munmap_chunk(): invalid pointer

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)

Error:
code: malloc.c:2401: sysmalloc: Assertion `(old_top == initial_top (av) && old_size == 0)...