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

using namespace std;

const int MAX=3005;

int current[MAX];
int currentprim[MAX];
int taken[MAX];
int order[MAX];
deque <int> wyn[MAX];

int BinS(int p, int k, int x)
{
    int mid=(p+k)/2;
    if(x==order[mid]) return mid;
    if(order[mid]<x) return BinS(mid+1, k, x);
    else return BinS(p, mid-1, x);
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n, it=1;
    bool zmiana=1;
    cin >>n;
    for(int i=1; i<=n; i++)
    {
        cin >>order[i];
        current[i]=order[i];
    }
    sort(order+1, order+n+1);
    for(int i=1; i<=n; i++)
    {
        current[i]=BinS(1, n, current[i]);
        currentprim[current[i]]=i;
    }
    while(zmiana)
    {
        zmiana=0;
        for(int i=1; i<=n; i++)
        {
            if(currentprim[i]!=i && taken[i]!=it && taken[currentprim[i]]!=it)
            {
                zmiana=1;
                wyn[it].push_front(i);
                wyn[it].push_back(currentprim[i]);
                taken[i]++;
                taken[currentprim[i]]++;
                currentprim[current[i]]=currentprim[i];
                current[currentprim[i]]=current[i];
                current[i]=i;
                currentprim[currentprim[i]]=currentprim[i];
                currentprim[i]=i;
            }
        }
        it++;
    }
    it--;
    cout <<it-1 <<"\n";
    for(int i=1; i<it; i++)
    {
        cout <<wyn[i].size() <<"\n";
        for(int j=0; j<wyn[i].size(); j++) cout <<wyn[i][j] <<" ";
        cout <<"\n";
    }
    return 0;
}