Submission #1100772

#TimeUsernameProblemLanguageResultExecution timeMemory
1100772Kirill22Cat (info1cup19_cat)C++17
100 / 100
322 ms13860 KiB
#include "bits/stdc++.h"

using namespace std;

void solve() {
    int n;
    cin >> n;
    vector<int> a(n), pos(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        a[i]--;
        pos[a[i]] = i;
    }
    for (int i = 0; i < n; i++) {
        if (n - pos[i] - 1 != pos[n - i - 1]) {
            cout << -1 << '\n';
            return;
        }
    }
    vector<pair<int, int>> ans;
    auto Swap = [&] (int i, int j) {
        swap(a[i], a[j]);
        pos[a[i]] = i;
        pos[a[j]] = j;
    };
    auto make = [&] (int i, int j) {
        ans.push_back({i, j});
        Swap(i, j);
        Swap(n - i - 1, n - j - 1);
    };
    bool optimal = true;
    if (optimal) {
        vector<int> todo;
        for (int it = 0; it < n / 2; it++) {
            if (a[it] != it && a[it] != n - it - 1) {
                make(it, pos[it]);
            }
            if (a[it] == it) {
                continue;
            }
            if (a[it] == n - it - 1) {
                todo.push_back(it);
                continue;
            }
        }
        if ((int) todo.size() % 2) {
            cout << -1 << '\n';
            return;
        }
        for (int i = 0; i < (int) todo.size(); i += 2) {
            make(pos[todo[i]], pos[n - todo[i + 1] - 1]);
            make(pos[todo[i]], pos[todo[i + 1]]);
        }
    }
    for (int l = 0; l < n - 1 - l && !optimal; l++) {
        int r = n - l - 1;
        if (a[l] == l && a[r] == r) {
            continue;
        }
        if (a[l] == l || a[r] == r) {
            assert(false);
            if (r - l + 1 <= 5) {
                cout << -1 << '\n';
                return;
            }
            if (a[l] == l) {
                int ps = l + 1;
                while (a[ps] == r || a[n - ps - 1] == r) ps++;
                make(l, ps);
            } else {
                int ps = l + 1;
                while (a[ps] == l || a[n - ps - 1] == l) ps++;
                make(r, ps);
            }
        }
        assert(a[l] != l && a[r] != r);
        if (pos[l] != n - 1 - pos[r]) {
            assert(false);
        }
        if (a[l] == r) {
            assert(a[r] == l);
            if (l + 1 == r) {
                cout << -1 << '\n';
                return;
            }
            make(l, l + 1);
        }
        make(l, pos[l]);
        assert(a[l] == l && a[r] == r);
    }
    cout << ans.size() << " " << ans.size() << '\n';
    for (auto [i, j] : ans) {
        cout << i + 1 << " " << j + 1 << '\n';
    }
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    cin >> t;
    while (t--) {
        solve();
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...