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
#include <algorithm>
#include <iostream>
#include <vector>

int main() {
    uint64_t n;
    std::cin >> n;

    std::vector<uint64_t> h(n);
    std::vector<std::pair<uint64_t, uint64_t>> p(n);
    for (uint64_t i = 0; i < n; ++i) {
        std::cin >> h[i];
        p[i] = std::make_pair(h[i], i);
    }

    std::sort(p.begin(), p.end());
    for (uint64_t i = 0; i < n; ++i) {
        h[p[i].second] = i;
    }

    std::vector<std::vector<std::pair<uint64_t, uint64_t>>> swaps;
    for (;;) {
        std::vector<bool> used(n, false);
        swaps.push_back({});

        for (uint64_t i = 0; i < n - 1; ++i) {
            if (h[i] != i && !used[i] && !used[h[i]]) {
                uint64_t other = h[i];
                swaps.back().push_back({ i, other });
                used[i] = used[other] = true;
                std::swap(h[i], h[other]);
            }
        }

        if (swaps.back().size() == 0) {
            swaps.pop_back();
            break;
        }
    }

    std::cout << swaps.size() << "\n";
    for (auto &one: swaps) {
        std::cout << one.size() * 2 << "\n";
        for (int64_t i = 0; i < one.size(); ++i) {
            std::cout << one[i].first + 1 << " ";
        }
        for (int64_t i = one.size() - 1; i >= 0; --i) {
            std::cout << one[i].second + 1 << " ";
        }
        std::cout << "\n";
    }
}