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
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <iostream>
#include <list>
#include <vector>
#include <cmath>

#define MAX_HEIGHT 3000

using namespace std;

vector<int> simplifyCycles(list<vector<int>> &cycles, int *heights, int *rev_perm)
{
    list<pair<int, int>> changes;
    for (auto cycle : cycles)
    {
        if (cycle.size() == 2)
        {
            changes.push_back({rev_perm[cycle[0]], rev_perm[cycle[1]]});
            swap(heights[rev_perm[cycle[0]]], heights[rev_perm[cycle[1]]]);
            continue;
        }

        for (int i = 1; i <= (cycle.size() - 1) / 2; ++i)
        {
            changes.push_back({rev_perm[cycle[i]], rev_perm[cycle[cycle.size() - i]]});
            swap(heights[rev_perm[cycle[i]]], heights[rev_perm[cycle[cycle.size() - i]]]);
        }
    }

    vector<int> result;
    for (auto change : changes)
    {
        result.push_back(change.first + 1);
    }
    for (auto it = changes.crbegin(); it != changes.crend(); ++it)
    {
        result.push_back(it->second + 1);
    }
    return result;
}

int numberOfOperations(list<vector<int>> &cycles)
{
    size_t maxLength = 0;
    for (auto cycle : cycles)
    {
        maxLength = max(maxLength, cycle.size());
    }
    return static_cast<int>(ceil(log2(maxLength)));
}

void constructCycles(int *heights, int n, int *permutation, int *rev_perm, list<vector<int>> &cycles)
{
    int heightPresent[MAX_HEIGHT] = {0};

    for (int i = 0; i < n; ++i)
    {
        heightPresent[heights[i] - 1] = i + 1;
    }

    int perm_idx = 0;
    for (int i = 0; i < MAX_HEIGHT; ++i)
    {
        if (heightPresent[i] != 0)
        {
            permutation[heightPresent[i] - 1] = perm_idx++;
            rev_perm[perm_idx - 1] = heightPresent[i] - 1;
        }
    }

    for (int i = 0; i < n; ++i)
    {
        if (permutation[i] == i || permutation[i] == -1)
        {
            continue;
        }

        int j = i;
        vector<int> cycle;
        while (permutation[j] != -1)
        {
            cycle.push_back(permutation[j]);
            const int perm_j = permutation[j];
            permutation[j] = -1;
            j = perm_j;
        }
        cycles.push_back(cycle);
    }
}

void printSwaps(list<vector<int>> &swaps)
{
    cout << swaps.size() << endl;
    for (auto swap : swaps)
    {
        cout << swap.size() << endl;
        for (int num : swap)
        {
            cout << num << " ";
        }
        cout << endl;
    }
}

int main()
{
    int n;
    cin >> n;
    int *heights = new int[n];
    int *permutation = new int[n];
    int *rev_perm = new int[n];

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

    list<vector<int>> cycles;

    constructCycles(heights, n, permutation, rev_perm, cycles);

    list<vector<int>> swaps;

    while (!cycles.empty())
    {
        swaps.push_back(simplifyCycles(cycles, heights, rev_perm));
        cycles.clear();
        constructCycles(heights, n, permutation, rev_perm, cycles);
    }

    printSwaps(swaps);

    delete[] permutation;
    delete[] rev_perm;
    delete[] heights;
    return 0;
}