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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <iostream>
#include <map>
#include <vector>
#include <algorithm>

using namespace std;

void transform(std::vector<int> &vec) {
    std::vector<int> copy = vec;
    std::sort(copy.begin(), copy.end());
    
    std::map<int, int> pos;
    for (int i=0; i<copy.size(); i++) {
        pos[copy.at(i)] = i;
    }
    
    for (int i=1; i<copy.size(); i++) {
        vec[i] = pos[vec[i]];
    }
}

std::vector<std::vector<int>> cycles(std::vector<int> &vec) {
    std::vector<std::vector<int>> cycles;
    std::vector<bool> visited(vec.size(), false);
    for (int i=1; i<vec.size(); i++) {
        if (visited[i]) {
            continue;
        }
        std::vector<int> cycle;
        cycle.push_back(i);
        visited[i] = true;
        
        int next = vec[i];
        while (next != i) {
            visited[next] = true;
            cycle.push_back(next);
            next = vec[next];
        }
        
        if (cycle.size() != 1) {
            cycles.push_back(cycle);
        }
    }
    
    return cycles;
}

void break_cycles(std::vector<std::vector<int>> &cycles, std::vector<int> &vec) {
    std::vector<int> first;
    std::vector<int> second;
    for (auto cycle : cycles) {
        if (cycle.size() == 2) {
            first.push_back(cycle[0]);
            second.push_back(cycle[1]);
            continue;
        }
        
        for (int i=0; i<(cycle.size()-1)/2; i++) {
            first.push_back(cycle[i]);
            second.push_back(cycle[cycle.size()-2-i]);
        }
    }

    std::map<int, int> pos;
    for (int i=0; i<vec.size(); i++) {
        pos[vec.at(i)] = i;
    }
    
    for (int i=0; i<first.size(); i++) {
        int f = first[i];
        int s = second[i];
        vec[pos[f]] = s;
        vec[pos[s]] = f;
        //std::cout << "swap " << f << " " << s << '\n';
    }

    reverse(second.begin(), second.end());
    first.insert(first.end(), second.begin(), second.end());
    
    
    std::cout << first.size() << '\n';
    for (int v : first) {
        std::cout << pos[v] << " ";
    }
    std::cout << '\n';
}

int main() {
    ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);
    
    int n;
    std::vector<int> vec;
    
    std::cin >> n;
    
    vec.push_back(0);
    
    for (int i=0; i<n; i++) {
        int v;
        std::cin >> v;
        vec.push_back(v);
    }
    
    std::vector<int> res;
    
    transform(vec);
    // for (int v: vec) {
    //     std::cout << v << " ";
    // }
    // std::cout << '\n';
    
    std::vector<std::vector<int>> c = cycles(vec);
    // for (std::vector<int> cycle : c) {
    //     for (int v: cycle) {
    //         std::cout << v << " ";
    //     }
    //     std::cout << '\n';
    // }
    
    if (c.size() == 0) {
        std::cout << "0\n";
        return 0;
    }
    
    bool only_len_2 = true;
    for (auto v : c) {
        if (v.size() > 2) {
            only_len_2 = false;
        }
    }
    
    if (only_len_2) {
        std::cout << "1\n";
        break_cycles(c, vec);
        return 0;
    } else {
        std::cout << "2\n";
        break_cycles(c, vec);
        std::vector<std::vector<int>> c = cycles(vec);
        // for (int v: vec) {
        //     std::cout << v << " ";
        // }
        // std::cout << '\n';
        
        // for (std::vector<int> cycle : c) {
        //     std::cout << "[";
        //     for (int v: cycle) {
        //         std::cout << v << " ";
        //     }
        //     std::cout << "]\n";
        // }
        break_cycles(c, vec);
    }
    // for (int v: vec) {
    //     std::cout << v << " ";
    // }
}