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
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 6e3+7;
bitset<MAXN> vis;
vector<int> adj[MAXN], A, B,gdzie;
deque<int> tab[MAXN];
int n, poz[MAXN], cnt = 0;

void dfs(int v, int p) {
	vis[v] = 1;
	cnt++;
	for(int s : adj[v]) {
		if(s == p || vis[s]) continue;
		dfs(s, v);
	}
}
bitset<MAXN> ogr, zro;
void gen(int v, deque<int> &T, int p) {
	
	vis[v] = 1;
	for(int s : adj[v]) {
		if(s == p || vis[s]) continue;
		if(!ogr[v] && !zro[s] && !zro[poz[A[v]]]) {
			T.push_back(poz[A[v]]);
			T.push_front(s);
			int tutaj = poz[A[v]];
			zro[tutaj] = 1;
			poz[A[v]] = s;
			poz[B[s]] = tutaj;
			swap(B[tutaj], B[s]);
			ogr[v] = 1;
			zro[s] = 1;

		}
		gen(s,T,v);
	}
}
int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> n;
	for(int i = 0; i < n; ++i) {
		int a;
		cin >> a;
		A.push_back(a);
		poz[a] = i;
	}
	B = A;
	sort(B.begin(), B.end());
	for(int i = 0; i < n; ++i) {
		if(poz[B[i]] != i) {

			adj[poz[B[i]]].push_back(i);
			
		}
	}
	B = A;
	int res = 0;
	for(int i = 0; i < n; ++i) {
		cnt = 0;
		if(!vis[i]) dfs(i, -1);
		int ile = (int)__lg(cnt);
		int moz = ile;
		if(cnt > (1 << moz)) moz++;
		res = max(res, moz);
	}
	//cout << res << '\n';
	int ile = 0, ostCnt = 0;;
	while(true) {
		ile++;
		deque<int> T;
		for(int i = 0; i < n; ++i) {
			vis[i] = 0;
			zro[i] = 0;
		}
		for(int i = 0; i < n; ++i) {
			if(!ogr[i]) {	
				gen(i, T, -1);
			}
		}
		tab[ile-1] = T;
		int cnt = 0;
		for(int i = 0; i < n; ++i) {
			if(!ogr[i]) cnt++;
		}
		//cout << cnt << '\n';
		if(cnt == ostCnt) break;
		ostCnt = cnt;
	}
	while(!tab[ile-1].size()) --ile;
	deque<int> ost = tab[ile-1];
	if(ost.front() == ost.back()) ile--;
	cout << ile << '\n';
	for(int i = 0; i < ile; ++i) {
		cout << tab[i].size() << '\n';
		for(int s : tab[i]) {
			cout << s+1 << ' ';
		}
		cout << '\n';
	}
}