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
// Mateusz Bogusławski

#include <functional>
#include <iostream>
#include <vector>
#include <algorithm>
#include <unordered_map>

using namespace std;

int main() {
    int n, h[3000] = {}, s[3000] = {};
    cin >> n;

    for (int i = 0; i < n; i++) {
        cin >> h[i];
        s[i] = h[i];
    }

    sort(s, s + sizeof(s)/sizeof(s[0]), greater<int>());

    unordered_map<int, int> n2pos;
    for (int i = 0; i < n; i++) {
        n2pos[s[i]] = n-1-i;
    }

    vector< vector<int> > m;

    bool f = true;
    int c = -1;
    while(f) {
        c++;
        m.resize(c+1);
        f = false;
        bool b[3000] = {};

        for (int i = 0; i < n; i++) {
            if ( b[i] || b[n2pos[h[i]]] ) { continue; }
            if (i != n2pos[h[i]]) {
                b[i] = b[n2pos[h[i]]] = true;
                m[c].insert(m[c].begin(), i);
                m[c].push_back(n2pos[h[i]]);
                int tmp = h[i];
                h[i] = h[n2pos[h[i]]];
                h[n2pos[tmp]] = tmp;
                f = true;
            }
        }
    }

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