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
#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>
#include <map>

using namespace std;

void run(vector<pair<int, int> > &h) {
  map<int, int> target_pos_by_height;

  vector<pair<int, int> > sorted_h = h;
  sort(sorted_h.begin(), sorted_h.end());

  for (int i = 0; i < sorted_h.size(); i++) {
    target_pos_by_height[sorted_h[i].first] = i;
    // cout << sorted_h[i].first << " " << sorted_h[i].second << endl;
  } 
 
  vector<vector<int> > lists;
  vector<int> visited(h.size(), false);

  vector<int> two_element_cycle_indices;

  for (int i = 0; i < h.size(); i++) {
    // cout << "processing element " << h[i].first << endl;
    int height = h[i].first;
    if (target_pos_by_height[height] == i) {
      // cout << "it's already at the desired position" << endl;
      continue;
    }
    if (visited[i]) {
      // cout << "already part of another cycle" << endl;
      continue;
    }

    vector<int> list;
    // cout << "cycle ";
    int current = i;
    do {
      // cout << h[current].first << " ";
      list.push_back(current);
      visited[current] = true;
      current = target_pos_by_height[h[current].first];
    } while (current != list[0]);
    // cout << endl;

    lists.push_back(list);
    if (list.size() == 2) {
      two_element_cycle_indices.push_back(lists.size() - 1);
    }
  }
  if (two_element_cycle_indices.size() > 0) {
    cout << lists.size() - two_element_cycle_indices.size() + 1 << endl;
  } else {
    cout << lists.size() << endl;
  }
  int two_element_cycle_index = 0;
  for (int i = 0; i < lists.size(); i++) {
    if (two_element_cycle_index < two_element_cycle_indices.size() && i == two_element_cycle_indices[two_element_cycle_index]) {
      two_element_cycle_index++;
      continue;
    }
    cout << lists[i].size() << endl;
    for (int j = 0; j < lists[i].size(); j++) {
      cout << lists[i][j] + 1 << " ";
    }
    cout << endl;
  }

  if (two_element_cycle_indices.size() > 0) {
    cout << 2 * two_element_cycle_indices.size() << endl;
    for (int i = 0; i < two_element_cycle_indices.size(); i++) {
      cout << lists[two_element_cycle_indices[i]][0] + 1 << " ";
    }
    for (int i = two_element_cycle_indices.size() - 1; i >= 0; i--) {
      cout << lists[two_element_cycle_indices[i]][1] + 1 << " ";
    }
    cout << endl;
  }
}

int main() {
  int n;
  vector<pair<int, int> > h;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int hi;
    cin >> hi;
    h.push_back(make_pair(hi, i));
  }
  run(h);
}