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

using namespace std;

int n;
vector<int> t, st;
map<int,int> docel;
vector<vector<int>> cycle;
vector<bool> was;
vector<int> res,ult;
 
void find_cycle(){
    was.assign(n,0);
    cycle.clear();
    for(int i = 0; i<n; i++)if(was[i] == 0 && docel[t[i]] != i){
        was[i] = 1;
        cycle.push_back(vector<int>());
        cycle.back().push_back(i);
        int ind = docel[t[i]];
        while(ind != i){
            was[ind] = 1;
            cycle.back().push_back(ind);
            ind = docel[t[ind]];
        }
    }
}

void paruj(){
    for(int i = 0; i<cycle.size(); i++){
        for(int j = 0; j<cycle[i].size()/2; j++){
            res.push_back(cycle[i][j]);
            ult.push_back(cycle[i][cycle[i].size()-1-j]);
            swap(t[cycle[i][j]],t[cycle[i][cycle[i].size()-1-j]]);
        }
    }
}

void print_res_ult(){
    cout<<res.size()*2<<"\n";
    for(int i = 0; i<res.size(); i++)cout<<res[i]+1<<" ";
    for(int i = ult.size()-1; i>=0; i--)cout<<ult[i]+1<<" ";
    cout<<"\n";
    res.clear();
    ult.clear();
}

bool is_sorted(){
    bool is_s = 1;
    for(int i = 0; i<n; i++)if(i > 0 && t[i-1] > t[i])is_s = 0;
    return is_s;
}

int main(){
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    cin>>n;
    t.assign(n,0);
    for(int i = 0; i<n; i++){
        cin>>t[i];
    }

    if(is_sorted()){
        cout<<0<<"\n";
        return 0;
    }

    st = t;
    sort(st.begin(), st.end());
    for(int i = 0; i<n; i++){
        docel[st[i]] = i;
    }

    find_cycle();
    paruj();
    //for(int v:t)cout<<v<<" ";cout<<"\n";

    if(is_sorted()){
        cout<<1<<"\n";
        print_res_ult();
        return 0;
    }else{
        cout<<2<<"\n";
        print_res_ult();
    }

    find_cycle();
    paruj();
    //for(int v:t)cout<<v<<" ";cout<<"\n";

    print_res_ult();
    return 0;
}