Problem p01557 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <set>
#include <vector>

#define show(x) cerr << #x << " = " << x << endl;

using namespace std;
using ll = long long;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "sz:" << v.size() << "\\n[";
  for (const auto &p : v) {
    os << p << ",";
  }
  os << "]\\n";
  return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &v) {
  os << "sz:" << v.size() << "\\n{";
  for (const auto &p : v) {
    os << p << ",";
  }
  os << "}\\n";
  return os;
}

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  vector<int> S(N);
  for (int i = 0; i < N; i++) {
    S[i] = i + 1;
  }

  constexpr int MAX = 10;
  constexpr int HALF = (MAX - 1) / 2;

  vector<set<vector<int>>> former(HALF + 1);
  vector<set<vector<int>>> latter(HALF + 1);
  former[0].insert(A);
  latter[0].insert(S);

  for (int i = 0; i < HALF; i++) {
    for (const auto &V : former[i]) {
      for (int l = 0; l < N - 1; l++) {
        for (int r = l + 1; r < N; r++) {
          auto v = V;
          for (int k = 0; k < (r - l + 1) / 2; k++) {
            swap(v[l + k], v[r - k]);
          }
          former[i + 1].insert(v);
        }
      }
    }
    for (const auto &V : latter[i]) {
      for (int l = 0; l < N - 1; l++) {
        for (int r = l + 1; r < N; r++) {
          auto v = V;
          for (int k = 0; k < (r - l + 1) / 2; k++) {
            swap(v[l + k], v[r - k]);
          }
          latter[i + 1].insert(v);
        }
      }
    }
  }

  //    show(former);

  for (int i = 0; i < N - 1; i++) {
    if (i <= HALF) {
      if (former[i].find(S) != former[i].end()) {
        cout << i << endl;
        return 0;
      }
    } else {
      const int res = i - HALF;
      for (const auto &v : latter[res]) {
        if (former[HALF].find(v) != former[HALF].end()) {
          cout << i << endl;
          return 0;
        }
      }
    }
  }
  cout << N - 1 << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;
using ll = long long;

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  vector<int> POS(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    A[i]--;
    POS[A[i]] = i;
  }
  const int maximum = 1 << N;
  int mini = N;
  for (int i = 0; i < maximum; i++) {
    vector<int> a = A;
    vector<int> pos = POS;
    int cnt = 0;
    int head = 0;
    int tail = N - 1;
    for (int j = 0; j < N; j++) {
      if (i & (1 << j)) {
        const int p = pos[head];
        if (p != head) {
          for (int k = 0; k < (p - head + 1) / 2; k++) {
            const int h = a[head + k];
            const int t = a[p - k];
            pos[h] = p - k;
            pos[t] = head + k;
            swap(a[head + k], a[p - k]);
          }
          cnt++;
        }
        head++;
      } else {
        const int p = pos[tail];
        if (p != tail) {
          for (int k = 0; k < (tail - p + 1) / 2; k++) {
            const int t = a[tail - k];
            const int h = a[p + k];
            pos[t] = p + k;
            pos[h] = head - k;
            swap(a[tail - k], a[p + k]);
          }
          cnt++;
        }
        tail--;
      }
    }
    mini = min(mini, cnt);
  }
  cout << mini << endl;

  return 0;
}