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

using namespace std;

struct TestCase {
  size_t N;
  vector<size_t> values;
};

TestCase read_test_case();
void solve_test_case(const TestCase&);

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

TestCase read_test_case() {
  TestCase tc;
  cin >> tc.N;
  tc.values.resize(tc.N);
  for (auto& v : tc.values) cin >> v;
  return tc;
}

using Transposition = std::pair<size_t, size_t>;

vector<vector<size_t>> decompose_into_cycles(const vector<size_t>& perm);
vector<vector<Transposition>> solve_cycle(vector<size_t> cycle);
void print_solution(const vector<vector<Transposition>>& solution);

void solve_test_case(const TestCase& tc) {
  vector<pair<size_t, size_t>> indexed_values(tc.N);
  for (size_t i = 0; i < tc.N; i++) indexed_values[i] = {tc.values[i], i};
  sort(indexed_values.begin(), indexed_values.end());

  vector<size_t> perm;
  for (auto [_, i] : indexed_values) perm.push_back(i);

  vector<size_t> original_position(tc.N);
  for (size_t i = 0; i < indexed_values.size(); i++)
    original_position[i] = indexed_values[i].second;

  auto cycles = decompose_into_cycles(perm);

  vector<vector<Transposition>> solution;

  for (const auto& cycle : cycles) {
    vector<size_t> cycle_positions;
    for (auto c : cycle) cycle_positions.push_back(original_position[c]);

    auto cycle_solution = solve_cycle(cycle_positions);
    for (size_t i = 0; i < cycle_solution.size(); i++) {
      if (i == solution.size())
        solution.push_back(cycle_solution[i]);
      else {
        for (auto c : cycle_solution[i]) solution[i].push_back(c);
      }
    }
  }

  print_solution(solution);
}

vector<vector<size_t>> decompose_into_cycles(const vector<size_t>& perm) {
  vector<bool> used(perm.size(), false);
  vector<vector<size_t>> cycles;
  for (auto i : perm) {
    if (used[i]) continue;

    vector<size_t> cycle;
    auto current = i;
    do {
      cycle.push_back(current);
      used[current] = true;
      current = perm[current];
    } while (current != i);
    cycles.push_back(cycle);
  }

  return cycles;
}

vector<Transposition> make_reverse(vector<size_t>& cycle, size_t first,
                                   size_t last);

vector<vector<Transposition>> solve_cycle(vector<size_t> cycle) {
  vector<vector<Transposition>> solution;

  if (cycle.size() == 1)
    return {};
  else if (cycle.size() == 2)
    solution.push_back(make_reverse(cycle, 0, 1));
  else {
    solution.push_back(make_reverse(cycle, 0, cycle.size() - 1));
    solution.push_back(make_reverse(cycle, 1, cycle.size() - 1));
  }
  return solution;
}

vector<Transposition> make_reverse(vector<size_t>& cycle, size_t first,
                                   size_t last) {
  vector<Transposition> result;

  while (first < last) {
    result.push_back({cycle[first], cycle[last]});
    swap(cycle[first], cycle[last]);
    first++;
    last--;
  }

  return result;
}

void print_solution(const vector<vector<Transposition>>& solution) {
  cout << solution.size() << "\n";
  for (const auto& T : solution) {
    vector<size_t> left;
    stack<size_t> right;
    for (auto [l, r] : T) {
      left.push_back(l);
      right.push(r);
    }

    while (!right.empty()) {
      left.push_back(right.top());
      right.pop();
    }

    cout << left.size() << "\n";
    for (auto l : left) cout << l + 1 << " ";
    cout << "\n";
  }
}