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
#include <bits/stdc++.h>

using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin >> n;
    vector<int> h(n);
    for (int i = 0; i < n; i++) {
        cin >> h[i];
    }
    vector<int> s(h);
    sort(s.begin(), s.end());
    map<int, int> pos;
    for (int i = 0; i < n; i++) {
        pos[s[i]] = i;
    }
    vector<vector<int>> res;

    while (true) {
        vector<bool> used(n);
        queue<int> beg;
        stack<int> end;
        bool OK = true;
        for (int i = 0; i < n; i++) {
            if (i != pos[h[i]]) {
                OK = false;
                if (!used[i] && !used[pos[h[i]]]) {
                    beg.push(i);
                    end.push(pos[h[i]]);
                    used[i] = true;
                    used[pos[h[i]]] = true;
                    swap(h[i], h[pos[h[i]]]);
                }
            }
        }
        if (OK) break;
        vector<int> tmp;
        while (!beg.empty()) {
            tmp.push_back(beg.front());
            beg.pop();
        }
        while (!end.empty()) {
            tmp.push_back(end.top());
            end.pop();
        }
        res.push_back(tmp);
        // cout << tmp.size() << endl;
        // for (int j = 0; j < tmp.size(); j++) {
        //     cout << tmp[j] + 1 << " ";
        // }
        // cout << endl;
    }

    cout << res.size() << endl;
    for (int i = 0; i < res.size(); i++) {
        cout << res[i].size() << endl;
        for (int j = 0; j < res[i].size(); j++) {
            cout << res[i][j] + 1 << " ";
        }
        cout << endl;
    }
}