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
#include<iostream>
#include<vector>
#include<algorithm>
#include <queue>
#include <stack>
using namespace std;

void Swap(vector<int>& permutation, int i, int j){
    int a = permutation[i];
    permutation[i] = permutation[j];
    permutation[j] = a;
}

void Sort(int n, vector<int>& data, vector<int>& permutation){
    bool swaps = true;
    n--;
    while(swaps){
        swaps = false;
        for(int i = 0; i < n; i++){
            if(data[permutation[i]] <= data[permutation[i+1]])continue;
            Swap(permutation, i, i+1);
            swaps = true;
        }
    }
}

void Split(vector<int>& cycle, int s, int e, int depth, vector<queue<int>>& q, vector<stack<int>>& st){
    int size = e - s + 1;
    //for(int i = s; i <= e; i++){
    //    cout<<cycle[i]<<" ";
    //}cout<<'\n';
    
    if(size <= 1)return;

    queue<int> t1;
    stack<int> t2;
    while(q.size() <= depth){
        q.push_back(t1);
        st.push_back(t2);
    }

    if(size == 2){
        q[depth].push(cycle[s]);
        st[depth].push(cycle[e]);
        return;
    }
    if(size == 3){
        q[depth].push(cycle[s + 1]);
        st[depth].push(cycle[s + 2]);
        if(q.size() <= depth + 1){
            q.push_back(t1);
            st.push_back(t2);
        }
        q[depth + 1].push(cycle[s]);
        st[depth + 1].push(cycle[s + 1]);
        return;
    }

    q[depth].push(cycle[s - 1 + size / 2]);
    st[depth].push(cycle[e]);
    Split(cycle, s, s - 1 + size / 2, depth + 1, q, st);
    Split(cycle, s + size/2, e, depth + 1, q, st);
}

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

    vector<int> data(n + 1, 0);
    vector<int> permutation(n + 1, 0);
    vector<int> id(3001, 0);
    for(int i = 1; i<=n; i++){
        cin>>data[i];
        permutation[i] = i;
        id[data[i]] = i;
    }

    int j = 1;
    for(int i = 0; i < 3001; i++){
        if(id[i] == 0)continue;
        permutation[id[i]] = j;
        j++;
    }

    //Sort(n + 1, data, permutation);
    
    //vector<int> copy = permutation;
    //for(int i = 0; i <= n; i++)permutation[copy[i]] = i;;

    //Permute(permutation);
    //for(int i= 0; i <= n; i++)cout<<i<<" ";
    //cout<<'\n';

    //for(int i =0; i <= n; i++){
    //    cout<<permutation[i]<<" ";
    //}cout<<'\n';

    vector<vector<int>> cycle;
    vector<int> temp(1, 0);
    vector<bool> visited(n+1, false);
    int maxCycle = 1, next, sizeNow;

    bool identity = true;
    for(int i = 0; i <= n; i++){
        if(permutation[i] != i)identity = false;
    }

    if(identity){
        cout<< 0;
        return 0;
    }

    for(int i = 0; i <= n; i++){
        if(visited[i])continue;
        sizeNow = 1;
        temp[0] = i;
        cycle.push_back(temp);
        visited[i] = true;
        next = permutation[i];
        
        while(!visited[next]){
            cycle[cycle.size() - 1].push_back(next);
            visited[next] = true;
            sizeNow++;
            next = permutation[next];
        }
        if(sizeNow > maxCycle)maxCycle = sizeNow;
    }

    vector<queue<int>> que;
    vector<stack<int>> sta;
    for(int i = 0; i < cycle.size(); i++)
        Split(cycle[i], 0, cycle[i].size() - 1, 0, que, sta);
    
    cout<<que.size()<<'\n';
    if(que.size() == 0)return 0;
    for(int i = 0; i < que.size() - 1; i++){
        cout<<que[i].size() * 2<<'\n';
        while(!que[i].empty()){
            cout<<que[i].front() << " ";
            que[i].pop();
        }
        cout<<sta[i].top();
        sta[i].pop();
        while(!sta[i].empty()){
            cout<<" "<<sta[i].top();
            sta[i].pop();
        }
        cout<<'\n';
    }
    int i = que.size() - 1;
    cout<<que[i].size() * 2 <<'\n';
    while(!que[i].empty()){
            cout<<que[i].front() << " ";
            que[i].pop();
        }
        cout<<sta[i].top();
        sta[i].pop();
        while(!sta[i].empty()){
            cout<<" "<<sta[i].top();
            sta[i].pop();
        }

    /*int i = 0;
    int ans = maxCycle - 1;
    //stack<queue<int>> stacc;
    cout<<ans<<'\n';
    
    while(i < ans){
        queue<int> q;
        for(int j = 0; j < cycle.size(); j++){
            if(cycle[j].size() - 1 <= i)continue;
            //cout<<cycle[j][cycle[j].size() - 1 - i]<< " ";
            q.push(cycle[j][cycle[j].size() - 1 - i]);
        }

        for(int j = cycle.size() - 1; j >= 0; j--){
            if(cycle[j].size() - 1 <= i)continue;
            //cout<<cycle[j][cycle[j].size() - 1 - i - 1]<< " ";
            q.push(cycle[j][cycle[j].size() - 1 - i - 1]);
        }
        //stacc.push(q);
        cout<<q.size()<<'\n';
        cout<<q.front();
        q.pop();
        while(!q.empty()){
            cout<<" "<<q.front();
            q.pop();
        }
        cout<<'\n';
        i++;
    }*/

    /*while(!stacc.empty()){
        queue<int>& q = stacc.top();
        cout<<q.size()<<'\n';
        cout<<q.front();
        q.pop();
        while(!q.empty()){
            cout<<" "<<q.front();
            q.pop();
        }
        cout<<'\n';
        stacc.pop();
    }*/

    return 0;
}