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
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>

using namespace std;
const int MaxN = 3000, MaxA = 3000;

int tab[MaxN + 5];
int sortedTab[MaxN + 5];
int ind[MaxA + 5];
int vis[MaxN + 5];

vector<deque<int>> W;

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int n;
	cin >> n;
	for (int i = 1; i <= n; ++i)
	{
		cin >> tab[i];
		sortedTab[i] = tab[i];
	}
	sort(sortedTab + 1, sortedTab + n + 1);
	for (int i = 1; i <= n; ++i)
	{
		ind[sortedTab[i]] = i;
	}

	bool sorted = 0;
	int t = 1;
	while (!sorted)
	{
		sorted = 1;
		deque<int> swaps;
		for (int i = 1; i <= n; ++i)
		{
			if (ind[tab[i]] != i && vis[i] != t)
			{
				sorted = 0;
				vis[i] = t;
				int v = i, l = 0;
				while (vis[ind[tab[v]]] != t)
				{
					if (l % 2 == 0)
					{
						swaps.push_front(v);
						swaps.push_back(ind[tab[v]]);
					}
					vis[v] = t;
					v = ind[tab[v]];
					++l;
				}
				vis[v] = t;
			}
		}
		if (!swaps.empty()) W.push_back(swaps);
		while (!swaps.empty())
		{
			swap(tab[swaps.front()], tab[swaps.back()]);
			swaps.pop_front();
			swaps.pop_back();
		}
		++t;
	}
	cout << W.size() << "\n";
	for (int i = 0; i < W.size(); ++i)
	{
		cout << W[i].size() << "\n";
		while (!W[i].empty())
		{
			cout << W[i].front() << " ";
			W[i].pop_front();
		}
		cout << "\n";
	}
}