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
// Wiktor Cupiał
// TU Delft
 
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
 
#define mp make_pair
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define eb emplace_back
#define st first
#define nd second
#define vt vector
#define VAR(__var)  #__var << ": " << __var << " "
#define PAIR(__var) #__var << ": " << __var.first << ", " << __var.second << " "
#define FOR(__var, __start, __end)  for(int __var=__start; __var<__end; ++__var)
#define FORB(__var, __start, __end) for(int __var=__start; __var>__end; --__var)
#define maxi(__x, __y) __x = (__x>__y?__x:__y)
#define mini(__x, __y) __x = (__x<__y?__x:__y)
#define all(__var)     (__var).begin(),(__var).end()
#define rall(__var)    (__var).rbegin(),(__var).rend()
#define sz(__var)      (int)(__var).size()
#define satori         int __test; cin>>__test; while(__test--)
 
#ifndef DEBUG
#define DEBUG 0
#endif
#define debug if(DEBUG)
 
using namespace std;
 
using namespace __gnu_pbds;
template <typename T>
using ord_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
 
// templeos <3
typedef     int16_t i16;
typedef     int32_t i32;
typedef     int64_t i64;
typedef        void  u0;
typedef        bool  u1;
typedef        char  u8;
typedef    uint16_t u16;
typedef    uint32_t u32;
typedef    uint64_t u64;
typedef       float f32;
typedef      double f64;
typedef long double f86;
 
typedef       long long ll;
typedef     long double ld;
typedef   pair<ll, ll> pll;
typedef pair<int, int> pii;
 
const int INF = 1e9+2137;

int32_t main() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

	int n;
	cin >> n;

	vt<int> a(n), gdzie(n), tmp(n);
	FOR(i, 0, n) {
		cin >> a[i];
		tmp[i] = a[i];
	}
	
	unordered_map<int, int> conv;
	sort(all(tmp));
	FOR(i, 0, n)
		conv[tmp[i]] = i;

	FOR(i, 0, n)
		a[i] = conv[a[i]];
	
	vt<bool> vis(n, 0);
	deque<int> dq;
	vt<vt<int>> res;

	FOR(i, 0, n)
		gdzie[a[i]] = i;

	auto add_pair = [&](int a, int b) {
		vis[a] = 1;
		vis[b] = 1;
		dq.pf(a);
		dq.pb(b);
	};

	while(!is_sorted(all(a))) {
		fill(all(vis), 0);
		FOR(i, 0, n) {
			if(a[i] == i || vis[i])
				continue;
			if(gdzie[i] == a[i]) {
				if(vis[a[i]])
					continue;
				add_pair(i, a[i]);
				swap(a[i], a[a[i]]);
				swap(gdzie[i], gdzie[gdzie[i]]);
				continue;
			}
			int now = i;
			bool ok = 1;
			while(ok) {
				int x = gdzie[now];
				int xx = gdzie[x];
				int y = a[now];
				int yy = a[y];
				add_pair(x, y);
				swap(a[x], a[y]);
				if(xx == y && yy == x) {
					gdzie[x] = x;
					gdzie[now] = y;
					ok = 0;
				}
				else {
					gdzie[yy] = x;
					gdzie[now] = y;
					now = x;
				}
				if(xx == yy)
					ok = 0;
			}
		}

		vt<int> now;
		FOR(i, 0, sz(dq)) 
			now.pb(dq[i]);
		res.pb(now);
		dq.clear();

		/*
		cout << "---\n";
		FOR(i, 0, n)
			cout << a[i] << ' ';
		cout << '\n';
		FOR(i, 0, sz(now))
			cout << now[i] << ' ';
		cout << '\n';
		cout << "---\n";
		*/
	}

	cout << sz(res) << '\n';
	for(auto &x : res) {
		cout << sz(x) << '\n';
		for(auto &y : x)
			cout << y+1 << ' ';
		cout << '\n';
	}

	return 0;
}