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
#include <iostream>
#include <queue>
#include <stack>
#include <cmath>
using namespace std;
priority_queue <pair <int, int> > q1;
int t[3004];
int vis[3003][3004];
int vis2[3004];
stack <int> s;
queue <int> q;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n, a;
    cin >> n;
    int licz = n;
     for (int i = 1; i <= n; i++)
    {
        cin >> a;
        q1.push({a, i});
    }
    while(!q1.empty()){
        auto p = q1.top();
        q1.pop();
        t[p.second] = licz;
        licz--;
    }
    int longest = 1;
    //for (int i =1; i <=n; i++) cout << t[i]<<" ";
    for (int i = 1; i <= n; i++)
    {
        if (!vis2[i]){
            int l = 1;
            int j = t[i];
            vis2[i] = true;
            while(!vis2[j]){
                vis2[j] = true;
                j = t[j];
                l++;
            }
            if (l > longest) longest=l;
        }
    }
    int lg = log2(longest);
    if ((1 << lg) != longest) lg++;
    cout << lg << "\n";
    for (licz = 0; licz < lg; licz++)
    {
        for (int i = 1; i <= n; i++)
        {
            if (t[i] != i && !vis[licz][i]){
                if (!vis[licz][t[i]]){
                    int j = i;
                    while(!vis[licz][j] && !vis[licz][t[j]]){
                        q.push(j);
                        s.push(t[j]);
                        vis[licz][j] = true;
                        vis[licz][t[j]] = true;
                        swap(t[j], t[t[j]]);
                        j = t[j];
                    }
                    
                }
            }
        }
        cout << q.size() + s.size() << "\n";
        while (!q.empty()){
            cout << q.front() << " ";
            q.pop();
        }
        while (!s.empty()){
            cout << s.top() << " ";
            s.pop();
        }
        cout << "\n";
    }

    return 0;
}