1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;

vector<int> swaps_to_sequence(const vector<pair<int,int>>& Sw) {
    int n = Sw.size();
    vector<int>Seq(2 * n);

    for (int i = 0; i < n; i++) {
        Seq[i] = Sw[i].first;
        Seq[2 * n - 1 - i] = Sw[i].second;
    }

    return Seq;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;
    vector<int>H(n);
    for (int &x : H)
        cin >> x;
    if (is_sorted(H.begin(), H.end())) {
        cout << "0\n";
        return 0;
    }

    vector<int>sH(H);
    sort(sH.begin(), sH.end());
    map<int, int> Mp; // value to position in sorted
    for (int i = 0; i < n; i++)
        Mp[sH[i]] = i;

    // try in one round
    auto try_in_one_round = [&]() -> bool {
        vector<pair<int,int>> Swaps;
        for (int i = 0; i < n; i++)
            if (Mp[H[i]] > i) {
                if (Mp[H[Mp[H[i]]]] == i)
                    Swaps.push_back({i, Mp[H[i]]});
                else
                    return false;
            }

        auto Seq = swaps_to_sequence(Swaps);
        cout << "1\n";
        cout << Seq.size() << "\n";
        for (int x : Seq)
            cout << x + 1 << " ";
        cout << "\n";
        return true;
    };
    if (try_in_one_round())
        return 0;

    // do it in two rounds
    vector<bool>Used(n);
    vector<pair<int,int>>Swaps1, Swaps2;

    for (int i = 0; i < n; i++)
        if (!Used[i]) {
            Used[i] = true;
            vector<int>Cycle{i};
            for (int j = Mp[H[i]]; j != i; j = Mp[H[j]]) {
                Cycle.push_back(j);
                Used[j] = true;
            }

            int c = Cycle.size();
            for (int l = 0, r = c - 2; l < r; l++, r--)
                Swaps1.push_back({Cycle[l], Cycle[r]});
            for (int l = 0, r = c - 1; l < r; l++, r--)
                Swaps2.push_back({Cycle[l], Cycle[r]});
        }

    cout << "2\n";
    auto Seq1 = swaps_to_sequence(Swaps1);
    cout << Seq1.size() << "\n";
    for (int x : Seq1)
        cout << x + 1 << " ";
    cout << "\n";
    auto Seq2 = swaps_to_sequence(Swaps2);
    cout << Seq2.size() << "\n";
    for (int x : Seq2)
        cout << x + 1 << " ";
    cout << "\n";
    
    return 0;
}