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

typedef long long LL;

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    // -----------------------------------------------------------------------------------
    
    int n;
    cin>>n;
    vector<pair<int,int>> tab(n),srt(n);
    vector<bool> vis(n);
    for(int i=0;i<n;i++){
        cin>>tab[i].first;
        srt[i]={tab[i].first,i};
    }
    
    sort(srt.begin(),srt.end());

    for(int i=0;i<n;i++){
        tab[srt[i].second].second=i;
    }

    deque<int> q;
    vector<deque<int>> odp;

    while(1){
        for(int j=0;j<n;j++){
            int i=j;
            while(tab[i].second!=i && !vis[i] && !vis[tab[i].second]){
                q.push_back(i);
                q.push_front(tab[i].second);
                vis[i]=1;
                vis[tab[i].second]=1;
                int x=tab[tab[i].second].second;
                swap(tab[i],tab[tab[i].second]);
                i=x;
            }
        }
        if(q.empty())break;
        odp.push_back(q);
        q.clear();
        vis.clear();
        vis.resize(n);
    }
    cout<<odp.size()<<endl;
    for(int i=0;i<odp.size();i++){
        cout<<odp[i].size()<<endl;
        for(int x:odp[i])cout<<x+1<<" ";
        cout<<endl;
    }

    
    // -----------------------------------------------------------------------------------

    return 0;
}