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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Karol Kosinski 2022
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a),_b=(b);i<_b;++i)
#define FR_(i,a,b) for(int i=(a),_b=(b);i<=_b;++i)
#define FD_(i,b,a) for(int i=(b),_a=(a);i>=_a;--i)
#define ALL(c) (c).begin(),(c).end()
#define SIZE(c) int((c).size())
#define TIE(x...) int x;tie(x)
#define X first
#define Y second
#ifndef ENABLE_DEBUG
#define DEB(k,p,f,x...)
#else
#define DEB(k,p,f,x...) {if(k)printf("--------%4d : %s\n",__LINE__,__FUNCTION__);if(p)f(x);}
#endif
#define DEBL DEB(1,1,void,0)
#define DEBF(f,x...) DEB(1,1,f,x)
#define DEBC(p,x...) DEB(0,p,printf,x)
#define DEBUG(x...) DEB(0,1,printf,x)
using namespace std;
using LL = long long;
using ULL = unsigned long long;
using PII = pair<int, int>;
using TIII = tuple<int, int, int>;

constexpr int NX = 3'005;

int A[NX], B[NX], C[NX], S[ NX + 1 ];
bool Was[NX], Was2[NX];
vector<int> D1[15], D2[15], Tmp;

int solve(int n)
{
    if ( is_sorted( A + 1, A + n + 1 ) )
    {
        printf("0\n");
        return 0;
    }
    int p = 1;
    while (1)
    {
        bool changed = false;
        FR_(i,1,n) Was[i] = false;
        FR_(i,1,n)
        {
            if ( A[i] == i or Was[i] ) continue;
            changed = true;
            int k = i;
            Was[k] = true;
            Tmp.push_back(k);
            DEBUG("* %d ", k);
            while ( A[k] != i )
            {
                k = A[k];
                Was[k] = true;
                Tmp.push_back(k);
                DEBUG("* %d ", k);
            }
            DEBUG("\n");
            int d = ( SIZE(Tmp) + 1 ) / 2;
            D1[p].push_back( Tmp[0] );
            D2[p].push_back( Tmp[d] );
            int a1 = SIZE(Tmp) - 1, a2 = d + 1;
            while ( a1 > a2 )
            {
                D1[p].push_back( Tmp[ a1 -- ] );
                D2[p].push_back( Tmp[ a2 ++ ] );
            }
            int b1 = 1, b2 = d - 1;
            while ( b1 < b2 )
            {
                D1[p].push_back( Tmp[ b1 ++ ] );
                D2[p].push_back( Tmp[ b2 -- ] );
            }
            Tmp.clear();
        }
        FOR( j, 0, SIZE( D1[p] ) )
        {
            swap( A[ D1[p][j] ], A[ D2[p][j] ] );
        }
        if (not changed) break;
        ++ p;
    }
    printf("%d\n", -- p);
    FR_(i,1,p)
    {
        reverse( ALL( D2[i] ) );
        printf("%d\n", 2 * SIZE( D1[i] ));
        for ( int d : D1[i] ) printf("%d ", d);
        for ( int d : D2[i] ) printf("%d ", d);
        printf("\n");
    }
    return p;
}

void validate(int n, int p)
{
    int maxc = 0;
    FR_(i,1,n)
    {
        if ( Was2[i] ) continue;
        int k = i, counter = 1;
        Was2[k] = true;
        while ( C[k] != i )
        {
            k = C[k];
            Was2[k] = true;
            ++ counter;
        }
        maxc = max(counter, maxc);
    }
    if ( maxc == 1 )
    {
        if ( p != 0 )
            fprintf(stdout, "Wrong number of rounds: p=%d maxc=%d\n", p, maxc);
    }
    else if ( maxc == 2 )
    {
        if ( p != 1 )
            fprintf(stdout, "Wrong number of rounds: p=%d maxc=%d\n", p, maxc);
    }
    else if ( p != 2 )
    {
        fprintf(stdout, "Wrong number of rounds: p=%d maxc=%d\n", p, maxc);
    }
    FR_(i,1,p)
    {
        reverse( ALL( D2[i] ) );
        FOR( j, 0, SIZE( D1[i] ) )
        {
            swap( C[ D1[i][j] ], C[ D2[i][j] ] );
        }
    }
    if ( not is_sorted( C + 1, C + n + 1 ) )
    {
        fprintf(stdout, "Wrong order of items!\n");
        FR_(i,1,n)
        {
            fprintf(stdout, "%d ", C[i]);
        }
        fprintf(stdout, "\n");
    }
}

int main()
{
    int n; scanf("%d", &n);
    FR_(i,1,n)
    {
        int a; scanf("%d", &a);
        B[i] = a, S[a] = 1;
        DEBUG("%d ", B[i]);
    }
    DEBUG("\n");
    FR_(i,1,NX) S[i] += S[ i - 1 ];
    FR_(i,1,n)
    {
        C[i] = A[i] = S[ B[i] ];
        DEBUG("%d ", A[i]);
    }
    DEBUG("\n");
    int p = solve(n);
    // validate(n, p);
    return 0;
}