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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

const int MAX_H = 3000;

void print_pairs(vector<short>& v) {
    cout << v.size() << "\n";
    for(int i = 0; i < v.size(); i += 2) {
        cout << v[i] + 1 << " ";
    }
    for (int i = 1; i < v.size(); i += 2) {
        cout << v[v.size() - i] + 1 << " \n"[i == v.size() - 1];
    }
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(0);
    int k, l, n;
    cin >> n;
    vector<short> v(n), u, pos(MAX_H + 1), exp(MAX_H + 1), r1, r2;
    vector<bool> vis(n);
    for(int i = 0; i < n; i++) {
        cin >> v[i];
        pos[v[i]] = i;
    }

    if(is_sorted(v.begin(), v.end()))
        cout << "0\n";
    else {
        u = v;
        sort(u.begin(), u.end());
        for (int i = 0; i < n; i++)
            exp[u[i]] = i;
        for(int i = 0; i < n; i++) {
            if(!vis[i] && exp[v[i]] != i) {
                if(exp[v[i]] == pos[u[i]] && !vis[pos[u[i]]]) {
                    vis[i] = vis[pos[u[i]]] = true;
                    r1.push_back(i);
                    r1.push_back(pos[u[i]]);
                    swap(v[i], v[pos[u[i]]]);
                }
                else {
                    k = exp[v[i]];
                    l = pos[u[i]];
                    while(k != l && !vis[k] && !vis[l] && k != exp[v[k]] && l != exp[v[l]]) {
                        vis[k] = vis[l] = true;
                        r1.push_back(k);
                        r1.push_back(l);
                        swap(v[l], v[k]);
                        k = exp[v[l]];
                        l = pos[u[l]];
                    }
                    vis[k] = vis[l] = true;
                }
            }
        }
        if(is_sorted(v.begin(), v.end())) {
            cout << "1\n";
            print_pairs(r1);
        }
        else {
            for(int i = 0; i < n; i++) {
                if(i != exp[v[i]]) {
                    r2.push_back(i);
                    r2.push_back(exp[v[i]]);
                    swap(v[i], v[exp[v[i]]]);
                }
            }
            cout << "2\n";
            print_pairs(r1);
            print_pairs(r2);
        }
    }
    return 21/37;
}