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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include <cstdio>
#include <algorithm>
#include <vector>

#define MAX_N 4000
#define INF 100

using namespace std;

int full[MAX_N];
int side[MAX_N];
int perm[MAX_N];
int ile[MAX_N];

int bestInnerCount[MAX_N], bestFullCount[MAX_N];
int useInnerBig[MAX_N], useFullBig[MAX_N];

bool inCircle[MAX_N];

vector<vector<int>> circles;
vector<vector<pair< int, int>>> allChanges;

vector<pair<int, int> > sortingHelper;


/*
5
1670
2011
1560
1232
1447

5
3 4 2 0 1

1
5
2 1 3 4 5

-------------------


6
1556
1449
1863
2014
1333
1220

6
3 2 4 5 1 0

2
5
5 6 1 4 3
4
1 2 3 4

*/

void preprocess() {

    full[0] = 0;
    full[1] = 0;

    side[0] = 0;
    side[1] = 1;

    for (int i = 2; i < MAX_N; i++) {
        full[i] = INF;
        side[i] = INF;
        //printf("%d:\n", i);

        for (int count = 1; count < i; count++) {
            // FULL
            int smallSize = i / count;
            int bigSize = smallSize + 1;
            int rest = i % smallSize;

            if (smallSize < 2) continue;
            //printf("   - %d (%d, %d)\n", count, smallSize, rest);

            int solveFullSmall = max(full[count + rest] + 1, side[smallSize - 2]);
            int solveFullBig = max(full[count] + 1, side[bigSize - 2]);
            if (full[i] > solveFullBig) {
                full[i] = solveFullBig;
                useFullBig[i] = true;
                bestFullCount[i] = count;
                //printf("  - %d full big better: %d\n", count, solveFullBig);
            }
             if (full[i] > solveFullSmall) {
                full[i] = solveFullSmall;
                useFullBig[i] = false;
                bestFullCount[i] = count;
                //printf("  - %d full small better: %d\n", count, solveFullSmall);
            }

            // SIDE[i] -> 1 const strzałka + i nieconst strzałek
            int solveSideSmall = max(side[count + rest] + 1, side[smallSize - 2]);
            int solveSideBig = max(side[count] + 1, side[bigSize - 2]);
            if (side[i] > solveSideBig) {
                side[i] = solveSideBig;
                useInnerBig[i] = true;
                bestInnerCount[i] = count;
                //printf("  - %d side big better: %d\n", count, solveSideBig);
            }
             if (side[i] > solveSideSmall) {
                side[i] = solveSideSmall;
                useInnerBig[i] = false;
                bestInnerCount[i] = count;
                //printf("  - %d side small better: %d\n", count, solveSideSmall);
            }
        }
        //printf("  full: %d side: %d\n", full[i], side[i]);
    }
}

void newHandleCircle(vector<int> positions, vector<pair<int, int> >& out) {
    for (int i = 0; i < positions.size() / 2; i++) {
        out.emplace_back(positions[i], positions[positions.size() - 1 - i]);
    }
}

void handleCircle(vector<int> positions, vector<pair<int, int> >& out, bool isInner) {
    int n = positions.size();

    if (n < 2) return; 

    bool useBig = isInner ? useInnerBig[n] : useFullBig[n];
    int count = isInner ? bestInnerCount[n] : bestFullCount[n];

    int smallSize = n / count;
    int rest = n % smallSize;



    int pos = 0;
    for (int i = 0 ; i < count; i++) {
        int nextPos = pos + smallSize;
        if (useBig && rest > 0) {
            rest--;
            nextPos++;
        }
        out.emplace_back(positions[pos], positions[nextPos - 1]);
        vector<int> inner;
        for (int j = pos + 1; j < nextPos - 1; j++) {
            inner.push_back(positions[j]);
        }
        handleCircle(inner, out, true);
        pos = nextPos;
    }
}

void makeCircles(int n) {
    for (int i = 0; i < n; i++) inCircle[i] = false;

    for (int i = 0; i < n; i++) {
        if (inCircle[i]) continue;

        vector<int> circle;
        int pos = i;
        do {
            inCircle[pos] = true;
            circle.push_back(pos);
            pos = perm[pos];
        } while (pos != i);
        circles.push_back(circle);
    }
}

int main() {
    preprocess();

    int n;

    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        //scanf("%d", &perm[i]);
        scanf("%d", &ile[i]);
        sortingHelper.emplace_back(ile[i], i);
    }

    sort(sortingHelper.begin(), sortingHelper.end());
    for (int i = 0; i < n; i++) {
        perm[sortingHelper[i].second] = i;
    }

    while (true) {
        vector<pair<int, int>> changes;
        
        circles.clear();
        makeCircles(n);

        //printf ("-------------------\n");
        //printf("Perm in: \n");
        //for (int i = 0; i < n; i++) printf("%d ", perm[i]);
        
        //printf("\nCircles:\n");
        for (auto circle : circles) {
            //for (auto a : circle) printf("%d ", a);
            //printf("\n");
            newHandleCircle(circle, changes);
        }
        //printf("Changes:\n");
        for (auto change : changes) {
            //printf("(%d, %d) ", change.first, change.second);
            int x = perm[change.first];
            perm[change.first] = perm[change.second];
            perm[change.second] = x;
        }
        //printf("\n");
        if (changes.empty()) break;
        allChanges.push_back(changes);
    }

    printf("%lu\n", allChanges.size());
    for (auto changes : allChanges) {
        printf("%lu\n", 2 * changes.size());
        for (int i = 0; i < changes.size(); i++) {
            printf("%d ", changes[i].first + 1);
        }
        for (int i = changes.size() - 1; i >= 0; i--) {
            printf("%d ", changes[i].second + 1);
        }
        printf("\n");
    }
}