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
#include <bits/stdc++.h>

using namespace std;

constexpr int S = 5e5 + 3;

int n, act = 0, x;
int tab[S];
pair<int, int> temp[S];
int mozna[S];
vector<vector<int>> v;
vector<vector<int>> v2;
vector<int> stos;

bool check()
{
    for(int i = 1; i <= n; i++)
    {
        if(tab[i] != i)
            return true;
    }
    return false;
}

int main()
{
    cin >> n;
    for(int i = 1; i <= n; i++)
    {
        cin >> temp[i].first;
        temp[i].second = i;
    }
    sort(temp + 1, temp + n + 1);
    for(int i = 1; i <= n; i++)
    {
        tab[temp[i].second] = i;
    }
    while(check())
    {
        v.push_back({});
        v2.push_back({});
        act++;
        stos.reserve(n);
        for(int i = 1; i <= n; i++)
        {
            if(mozna[i] != act && tab[i] != i)
            {
                x = i;
                while(mozna[x] != act)
                {
                    mozna[x] = act;
                    stos.push_back(x);
                    x = tab[x];
                }
                for(int j = 0; j * 2 < (int)stos.size(); j++)
                {
                    if(stos[j] == stos[(int)stos.size() - j - 1])
                        break;
                    v[v.size() - 1].push_back(stos[j]);
                    v2[v2.size() - 1].push_back(stos[(int)stos.size() - j - 1]);
                    swap(tab[stos[j]], tab[stos[(int)stos.size() - j - 1]]);
                }
                stos.clear();
            }
        }
    }
    cout << v.size();
    for(int i = 0; i < (int)v.size(); i++)
    {
        cout << '\n' << v[i].size() * 2 << '\n';
        for(int j = 0; j < (int)v[i].size(); j++)
        {
            cout << v[i][j] << ' ';
        }
        for(int j = (int)v2[i].size() - 1; j >= 0; j--)
        {
            cout << v2[i][j] << ' ';
        }
    }
    return 0;
}