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

using namespace std;
using PII = pair<int, int>;

int main()
{
	int n;
	cin >> n;

	vector<int> sorted(n);
	vector<PII> height(n);

	for (int i = 0; i < n; ++i)
	{
		cin >> sorted[i];
		height[i].first = sorted[i];
	}

	sort(sorted.begin(), sorted.end());

	map<int, int> dict;

	for (int i = 0; i < n; ++i)
	{
		dict[sorted[i]] = i;
	}

	for (int i = 0; i < n; ++i)
	{
		height[i].second = dict[height[i].first];
	}

	vector<deque<int>> res;

	while (true)
	{
		deque<int> deque;
		vector<bool> used(n);

		for (int i = 0; i < n; ++i)
		{
			if (!used[i])
			{
				used[i] = true;

				vector<int> cycle;
				cycle.push_back(i);

				int next = height[i].second;

				while (!used[next])
				{
					used[next] = true;
					cycle.push_back(next);
					next = height[next].second;
				}

				if (cycle.size() == 2)
				{
					deque.emplace_front(cycle[0]);
					deque.emplace_back(cycle[1]);

					swap(height[cycle[0]], height[cycle[1]]);
				}
				else if (cycle.size() > 2)
				{
					for (int i = 1, j = cycle.size() - 1; i < j; ++i, --j)
					{
						deque.emplace_front(cycle[i]);
						deque.emplace_back(cycle[j]);

						swap(height[cycle[i]], height[cycle[j]]);
					}
				}
			}
		}

		if (deque.size() > 0)
			res.push_back(deque);
		else
			break;
	}

	cout << res.size() << endl;

	for (int i = 0; i < res.size(); ++i)
	{
		if (i != 0)
			cout << endl;

		cout << res[i].size() << endl;

		for (int j = 0; j < res[i].size(); ++j)
		{
			if (j != 0)
				cout << " ";

			cout << res[i][j] + 1;
		}
	}

	return 0;
}