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
#include <bits/stdc++.h>
using namespace std;
int kolejne[3008], w, n, x, wynik;
bool visited[3008];
vector<pair<int, int> > indeksy;
deque<int> nr_ruchu[3008];

int main() {
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> x;
        indeksy.push_back({x, i});
    }
    sort(indeksy.begin(), indeksy.end());
    for (int i = 0; i < indeksy.size(); i++) {
        kolejne[indeksy[i].second] = i+1;
    }
    
    for (int i = 1; i <= n; i++) {
        if(visited[i]) continue;
        if(i == kolejne[i]) {
            w++;
        }
        else if(i == kolejne[kolejne[i]]) {
            w += 2;
            nr_ruchu[1].push_front(i);
            nr_ruchu[1].push_back(kolejne[i]);
            visited[i] = true;
            visited[kolejne[i]] = true;
        }
        else {
            nr_ruchu[2].push_front(kolejne[i]);
            nr_ruchu[2].push_back(kolejne[kolejne[i]]);
            
            nr_ruchu[1].push_front(i);
            nr_ruchu[1].push_back(kolejne[i]);
            
            visited[i] = true;
            visited[kolejne[i]] = true;
            visited[kolejne[kolejne[i]]] = true;
        }
    }
    
    if (w == n || nr_ruchu[2].size() == 0) wynik = 1;
    else wynik = 2;
    cout << wynik << "\n";
    for (int i = wynik; i >= 1; i--) {
        cout << nr_ruchu[i].size() << "\n";
        for (int j = 0; j < nr_ruchu[i].size(); j++) {
            cout << nr_ruchu[i][j] << " ";
        }
        cout << "\n";
    }
}