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
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <ranges>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

typedef pair<int, int> PII;

const int INF = 1e9 + 1;

vector<deque<int>> solve(vector<int> p) {
    int n = (int)p.size();
    vector<deque<int>> res;

    while (true) {
        res.push_back({});
        vector<int> used(n, 0);
        for (int i = 0; i < n; i++) {
            if (used[i]) continue;
            deque<int> cycle = {i};
            while (p[cycle.back()] != cycle.front()) {
                cycle.push_back(p[cycle.back()]);
            }
            for (auto x : cycle) used[x] = true;

            while (cycle.size() > 1) {
                res.back().push_front(cycle.front());
                res.back().push_back(cycle.back());
                swap(p[cycle.front()], p[cycle.back()]);
                cycle.pop_back();
                cycle.pop_front();
            }
        }
        if (res.back().empty()) {
            res.pop_back();
            break;
        }
    }

    for (int i = 0; i < n; i++) assert(p[i] == i);
    assert(res.size() <= 2);
    return res;
}

void print(vector<deque<int>> res) {
    cout << res.size() << "\n";
    for (auto &x : res) {
        cout << x.size() << "\n";
        for (auto y : x) cout << y + 1 << " ";
        cout << "\n";
    }
}

void solve2() {
    int n;
    cin >> n;
    vector<int> h(n);
    for (auto &x : h) cin >> x;

    vector<int> rev_p(n);
    for (int i = 0; i < n; i++) rev_p[i] = i;
    sort(all(rev_p), [&](int i, int j) { return h[i] < h[j]; });
    vector<int> p(n);
    for (int i = 0; i < n; i++) p[rev_p[i]] = i;

    auto res = solve(p);
    print(res);
}

int main() {
    ios::sync_with_stdio(false);
    cin.exceptions(cin.failbit);
    cin.tie(0);
    solve2();
}