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
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
string out="";
int licz=0;
void f(vector<long long>&v,long long n){
    licz++;
    vector<long long>op,ok;
    vector<bool>cb(n,false);
    vector<long long>nv(n);
    for(long long i=0;i<n;i++){
        if(!cb[i]){
            //cout<<i<<" ";
            if(i==v[i]){nv[i]=v[i];cb[i]=true;continue;}
            int a=i;
            while(true){
                if(cb[a]){break;}
                if(cb[v[a]]){nv[a]=v[a];break;}
                if(!cb[v[a]]){
                    op.push_back(a+1);
                    ok.push_back(v[a]+1);
                    cb[v[a]]=true;
                    cb[a]=true;
                    nv[a]=v[v[a]];
                    nv[v[a]]=v[a];
                    a=v[v[a]];
                }
            }
            /*else if(!cb[v[i]]){
                op.push_back(i+1);
                ok.push_back(v[i]+1);
                cb[v[i]]=true;
                cb[i]=true;
                nv[i]=v[v[i]];
                nv[v[i]]=v[i];
            }
            else{nv[i]=v[i];}*/
        }
    }
    //cout<<" :i\n";
    //cout<<op.size()*2<<endl;
    out=out+to_string(op.size()*2);
    out+='\n';
    for(auto i:op){
        out=out+to_string(i);
        out+=' ';
    }
    for(long long i=ok.size()-1;i>=0;i--){
        out=out+to_string(ok[i]);
        out+=' ';
    }
    out+='\n';
    /*for(auto i:nv){cout<<i<<" ";}
    cout<<endl;*/
    v=nv;
    bool c=true;
    for(long long i=0;i<n;i++){
        if(i!=nv[i]){c=false;break;}
    }
    //return;
    if(c){return;}
    f(v,n);
}
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    long long n;
    cin >> n;
    //cout<<n<<endl;
    vector<long long>v(n);
    for(long long i=0;i<n;i++){cin>>v[i];/*cout<<v[i]<<" ";*/}
    //cout<<endl;
    //skalujemy
    vector<long long>kv=v;
    sort(kv.begin(),kv.end());
    vector<long long>rkv(3001);
    for(long long i=0;i<n;i++){
        rkv[kv[i]]=i;
    }
    vector<long long>nv(n);
    for(long long i=0;i<n;i++){
        nv[i]=rkv[v[i]];
    }
    //for(auto i:nv){cout<<i<<" ";}
    //cout<<endl;
    /*long long mxc=0;
    vector<bool>cb(n,false);
    for(long long i=0;i<n;i++){
        if(!cb[i]){
            long long d=0;
            long long a=i;
            while(true){
                cb[a]=true;
                if(cb[nv[a]]==true){break;}
                a=nv[a];d++;
            }
            mxc=max(mxc,d);
        }
    }
    long long log=1;
    long long p2=2;
    while(true){
        if(p2>mxc){break;}
        p2*=2;
        log++;
    }*/
    bool c=true;
    for(long long i=0;i<n;i++){
        if(i!=nv[i]){c=false;break;}
    }
    if(c){cout<<0<<endl;return 0;}
    //cout<<log<<endl;
    f(nv,n);
    cout<<licz<<endl;
    cout<<out;
}