CSES - Putka Open 2020 – 3/5 - Results
Submission details
Task:Vaihdot
Sender:hltk
Submission time:2020-10-16 20:20:42 +0300
Language:C++17
Status:READY
Result:0
Feedback
groupverdictscore
#10
#20
Test results
testverdicttimegroup
#10.01 s1, 2details
#20.03 s2details

Code

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        vector<int> v(n), pos(n);
        for (int i = 0; i < n; ++i) {
            cin >> v[i];
            v[i]--;
            pos[v[i]] = i;
        }
        bool ok = true;
        vector<pair<int, int>> swaps;
        auto do_swap = [&](int a, int b) {
            swap(v[a], v[b]);
            swap(pos[v[a]], pos[v[b]]);
            swaps.emplace_back(a, b);
        };
        for (int i = 0; i < n; ++i) {
            if (pos[i] == i)
                continue;
            if (abs(pos[i] - i) > 1) {
                do_swap(pos[i], i);
            } else {
                bool f = false;
                for (int j = 0; j < n; ++j) {
                    if (abs(i - j) > 1 && abs(pos[i] - j) > 1) {
                        f = true;
                        do_swap(pos[i], j);
                        do_swap(i, j);
                        do_swap(pos[i], j);
                        break;
                    }
                }
                if (!f)
                    ok = false;
            }
        }
        if (!ok)
            cout << -1 << '\n';
        else {
            cout << swaps.size() << '\n';
            for (auto& [l, r] : swaps)
                cout << l+1 << ' ' << r+1 << '\n';
        }
    }
}

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
...

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