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
#include <iostream>
#include <cstdio>
#include <string>
#include <sstream> 
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <ctime>
#include <cassert>
using namespace std;

#define assert_range(x,a,b) assert((a) <= (x) and (x) <= (b))
using ll = long long;
const int INF = 1e9;

void print_vec(vector<int> a) {
    for (int i = 0; i < a.size(); ++i) {
        cout << a[i]+1 << " ";
    }
    cout << endl;
}

bool verify(vector<int> a, vector<vector<int>> ans) {
    for (int i = 0; i < ans.size(); ++i) {
        set<int> ss(ans[i].begin(), ans[i].end());
        assert(ss.size() == ans[i].size());

        for (int j = 0, k = ans[i].size()-1; j < k; ++j, --k) {
            swap(a[ans[i][j]], a[ans[i][k]]);
        }
    }
    for (int i = 1; i < a.size(); ++i) {
        if (a[i-1] >= a[i]) {
            return false;
        }
    }
    return true;
}

vector<int> normalize(vector<int> a) {
    int n = a.size();
    vector<pair<int, int>> ord(n);
    for (int i = 0; i < n; ++i) {
        ord[i] = {a[i], i};
    }
    sort(ord.begin(), ord.end());
    for (int i = 0; i < n; ++i) {
        a[ord[i].second] = i;
    }
    return a;
}

vector<int> apply_swaps(vector<int> a, vector<pair<int, int>> swaps) {
    for (auto it = swaps.begin(); it != swaps.end(); ++it) {
        swap(a[it->first], a[it->second]);
    }
    return a;
}

bool is_sorted(vector<int> a) {
    for (int i = 1; i < a.size(); ++i) {
        if (a[i-1] > a[i]) return false;
    }
    return true;
}

vector<int> get_row_from_swaps(vector<pair<int, int>> swaps) {
    int n_swaps = swaps.size();
    vector<int> row(2*n_swaps);
    for (int i = 0; i < n_swaps; ++i) {
        row[i] = swaps[i].first;
        row[2*n_swaps-1-i] = swaps[i].second;
    }
    return row;
}

vector<vector<int>> solve(vector<int> a) {
    //print_vec(a);
    int n = a.size();

    vector<vector<int>> ans;

    // check is sorted
    if (is_sorted(a)) return ans;

    vector<pair<int, int>> swaps;
    vector<bool> used(n);
    vector<int> loc(n);
    for (int i = 0; i < n; ++i) {
        loc[a[i]] = i;
    }
    for (int i = 0; i < n; ++i) {
        if (used[i]) continue;
        if (a[i] == i) {
            used[i] = true;
            continue;
        }
        if (a[a[i]] == i) {
            used[i] = true;
            used[a[i]] = true;
            swaps.push_back({i, a[i]});
            swap(a[i], a[a[i]]);
            swap(loc[a[i]], loc[a[a[i]]]);
            continue;
        }
    }
    for (int i = 0; i < n; ++i) {
        if (a[i] == i || used[i]) continue;
        /*
        int run_limit = 5;
        int run = 0;
        */

        int j = i;
        // wartosc na pozycji j jest juz ustalona oraz wynosi a[j] jej lokacja nigdy sie nie zmieni
        // potrzebujemy teraz przeniesc wartosc j (znajdujaca sie na pozycji loc[j])  na pozycje a[j], co spowoduje przeniesienie wartosci a[a[j]] na pozycje loc[j]
        while (true) {
            /*
            cout << "###" << endl;
            cout << "j="<<j+1 << " a[j]="<<a[j]+1 << " loc[j]="<<loc[j]+1 << " a[a[j]]="<<a[a[j]]+1 << endl; 
            print_vec(a);
            cout << "###" << endl;
            */
            //assert(!used[j]);
            used[j] = true;
            used[loc[j]] = true;
            if (a[j] == j) break;

            int next_j = loc[j];
            if (loc[j] == a[j]) break;
            swaps.push_back({loc[j], a[j]});
            used[loc[j]] = true;
            used[a[j]] = true;
            swap(a[loc[j]], a[a[j]]);
            //swap(loc[loc[j]], loc[a[j]]);
            j = next_j;

            /*
            ++run;
            if (run == run_limit) break;
            */
        }
    }

    ans.push_back(get_row_from_swaps(swaps));
    /*
    cout << "###" << endl;
    print_vec(a);
    cout << "###" << endl;
    */

    if (is_sorted(a)) {
        return ans;
    }

    swaps.clear();
    for (int i = 0; i < n; ++i) {
        if (a[i] == i) continue;
        assert(a[a[i]] == i);
        swaps.push_back({i, a[i]});
        swap(a[i], a[a[i]]);
    }
    ans.push_back(get_row_from_swaps(swaps));

    return ans;
}

int main() {
    int t = 1;
    //scanf("%d", &t);
    for (int tid = 0; tid < t; ++tid) {
        int n;
        scanf("%d", &n);
        vector<int> a(n);

        for (int i = 0; i < n; ++i) {
            scanf("%d", &a[i]);
        }

        auto a_norm = normalize(a);

        if (t != 1) {
            printf("TEST_ID=%d/%d\n", tid+1, t);
        }
        auto ans = solve(a_norm);

        printf("%d\n", (int)ans.size());
        for (int i = 0; i < ans.size(); ++i) {
            printf("%d\n", (int)ans[i].size());
            for (int j = 0; j < ans[i].size(); ++j) {
                printf("%d ", ans[i][j]+1);
            }
            printf("\n");
        }
        assert(verify(a, ans));
    }

    return 0;
}