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

int heights[300003];
int sortedHeights[300003];
map<int, int> heightToNumber;
vector<vector<pair<int, int>>> listofPairsToBeSwapped;

vector<set<int>> listOfToBeSwapped;
int n;
int main()
{
    ios_base::sync_with_stdio(0);
    cin>>n;
    ////cerr<<"1"<<endl;

    for(int i=0;i<n;i++)
    {
        cin>>heights[i];
        sortedHeights[i]=heights[i];
    }
    sort(sortedHeights, sortedHeights+n);
    for(int i=1;i<=n;i++)
    {
        heightToNumber[sortedHeights[i-1]] = i;
    }
    for(int i=0;i<n;i++)
    {
        heights[i]=heightToNumber[heights[i]];
        sortedHeights[i]=heightToNumber[sortedHeights[i]];
    }
    for(int i=n;i>0;i--)
        heights[i]=heights[i-1];
    heights[0]=0;
    bool anyChanges=true;
    int numberOfRound=0;
    ////cerr<<"1"<<endl;
    ////cerr.flush();
    //for(int i=1;i<=n;i++)
        //cerr<<heights[i]<<" ";
    //cerr<<endl;
    while(anyChanges)
    {
        vector<pair<int, int>> pairsToBeSwapped;
        set<int> toBeSwapped;
        for(int i=1;i<n;i++)
        {
            if(heights[i]!=i && toBeSwapped.find(i)==toBeSwapped.end()
            && toBeSwapped.find(heights[i])==toBeSwapped.end())
            {
                bool changes2=true;
                int cycleIndex = i; 
                while(toBeSwapped.find(cycleIndex)==toBeSwapped.end()
                    && toBeSwapped.find(heights[cycleIndex])==toBeSwapped.end())
                {
                    toBeSwapped.insert(cycleIndex);
                    toBeSwapped.insert(heights[cycleIndex]);
                    pairsToBeSwapped.push_back(make_pair(cycleIndex, heights[cycleIndex]));
                    cycleIndex = heights[heights[cycleIndex]];
                }
            }    
        }
        if(pairsToBeSwapped.size()>0)
        {
            for(auto p: pairsToBeSwapped)
            {
                swap(heights[p.first],heights[p.second]);
                //cerr<<"swapping "<<p.first<<" "<<p.second<<endl;;
            }
            listofPairsToBeSwapped.push_back(pairsToBeSwapped);
            listOfToBeSwapped.push_back(toBeSwapped);
            anyChanges=true;
            numberOfRound++;
            //for(int i=1;i<=n;i++)
                //cerr<<heights[i]<<" ";
            //cerr<<endl;
        }
        else 
            anyChanges=false;
    }
    cout<<numberOfRound<<endl;
    for(int i=0;i<numberOfRound;i++)
    {
        cout<<listofPairsToBeSwapped[i].size()*2<<endl;
        for(int j=0;j<listofPairsToBeSwapped[i].size();j++)
        {
            cout<<listofPairsToBeSwapped[i][j].first<<" ";
        }
        for(int j=listofPairsToBeSwapped[i].size()-1;j>=0;j--)
        {
            cout<<listofPairsToBeSwapped[i][j].second<<" ";
        }
        cout<<endl;
    }

    return 0;
}