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
#include <bits/stdc++.h>

using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int n;
  cin >> n;

  int a;
  vector<pair<int, int>> v;

  for (int i = 0; i < n; i++) {
    cin >> a;
    v.push_back({a, i});
  }

  sort(v.begin(), v.end());

  vector<int> w(n);

  for (int i = 0; i < n; i++) {
    w[v[i].second] = i;
  }

  vector<vector<int>> wyn;

  while (!is_sorted(w.begin(), w.end())) {
    unordered_set<int> s;

    vector<pair<int, int>> toSwap;

    for (int i = 0; i < n; i++) {
      if (s.find(i) == s.end() && s.find(w[i]) == s.end() && i != w[i]) {
        if (w[w[i]] == i) {
          toSwap.push_back({i, w[i]});
          s.insert(i);
          s.insert(w[i]);
        } else {
          int p = w[i];
          vector<int> cycle = {i};
          s.insert(i);
          while (p != i) {
            s.insert(p);
            cycle.push_back(p);
            p = w[p];
          }

          int a = 1;
          int b = cycle.size() - 1;

          while (a < b) {
            toSwap.push_back({cycle[a], cycle[b]});
            a++;
            b--;
          }
        }
      }
    }

    vector<int> row;

    for (int i = 0; i < toSwap.size(); i++) {
      row.push_back(toSwap[i].first + 1);
    }

    for (int i = toSwap.size() - 1; i >= 0; i--) {
      row.push_back(toSwap[i].second + 1);
    }

    wyn.push_back(row);

    for (auto p : toSwap) {
      swap(w[p.first], w[p.second]);
    }
  }

  cout << wyn.size() << "\n";
  for (auto v : wyn) {
    cout << v.size() << "\n";
    for (auto i : v) {
      cout << i << " ";
    }
    cout << "\n";
  }

  return 0;
}