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
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <bits/stdc++.h>
using namespace std;

vector<pair<int, int>> wysokosci;
vector<int> wys_odw;
vector<vector<int>> petle;
vector<vector<vector<pair<int, int>>>> res; // petla, runda, osoby
int maxR;

void rozwiazPetle(int p)
{
    int r = 0;
    int p2 = 1;
    int n = petle[p].size();
    vector<bool> ok(n, false);
    while (p2 < n)
    {
        res[p].push_back(vector<pair<int, int>>());
        for (int i = 0; i < n - p2; i += p2 * 2)
        {
            if (ok[i])
                continue;
            // cout << "p: " << petle[p][i] << ' ' << petle[p][i + p2] << '\n';
            res[p][r].push_back({petle[p][i], petle[p][i + p2]});
            // swap(petle[p][i], petle[p][i + p2]);
            ok[i + p2] = true;
        }
        r++;
        maxR = max(maxR, r);
        p2 *= 2;
    }
}

void wypisz()
{
    int np = petle.size();
    int no;
    cout << maxR << '\n';
    for (int r = maxR-1; r >= 0; --r)
    {
        int m = 0;
        for (int p = 0; p < np; ++p)
        {
            if (res[p].size() <= r)
                continue;
            m += 2 * res[p][r].size();
        }
        cout << m << '\n';
        for (int p = 0; p < np; ++p)
        {
            if (res[p].size() <= r)
                continue;
            no = res[p][r].size();
            for (int o = 0; o < no; ++o)
            {
                cout << wys_odw[res[p][r][o].first] + 1 << ' ';
            }
        }
        for (int p = np - 1; p >= 0; --p)
        {
            if (res[p].size() <= r)
                continue;
            no = res[p][r].size();
            for (int o = no - 1; o >= 0; --o)
            {
                cout << wys_odw[res[p][r][o].second] + 1 << ' ';
            }
        }
        cout << '\n';
    }
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int n;
    cin >> n;
    wysokosci.resize(n);
    for (int i = 0; i < n; ++i)
    {
        cin >> wysokosci[i].first;
        wysokosci[i].second = i;
    }
    sort(wysokosci.begin(), wysokosci.end());
    wys_odw.resize(n);
    for (int i = 0; i < n; ++i)
    {
        wys_odw[wysokosci[i].second] = i;
    }
    vector<bool> bylo(n, false);
    int petla = 0;
    for (int i = 0; i < n; ++i)
    {
        if (bylo[i])
            continue;
        int j = i;
        petle.push_back(vector<int>());
        do
        {
            petle[petla].push_back(j);
            bylo[j] = true;
            j = wysokosci[j].second;
        } while (j != i);
        petla++;
    }
    res.resize(petle.size());
    // for (auto &x : petle)
    // {
    //     for (auto &y : x)
    //     {
    //         cout << y << ' ';
    //     }
    //     cout << '\n';
    // }
    for (int i = 0; i < petle.size(); ++i)
    {
        rozwiazPetle(i);
    }
    wypisz();
}