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

#define _CRT_SECURE_NO_WARNINGS
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
#define all(c) (c).begin(), (c).end()
#define loop(i, a, b) for (ll i = a; i < ll(b); i++)
#define rep(i, b) loop(i, 0, b)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
template <class T> ostream &operator<<(ostream &os, vector<T> const &);
template <int n, class... T>
typename enable_if<(n >= sizeof...(T))>::type _ot(ostream &,
                                                  tuple<T...> const &) {}
template <int n, class... T>
typename enable_if<(n < sizeof...(T))>::type _ot(ostream &os,
                                                 tuple<T...> const &t) {
  os << (n == 0 ? "" : " ") << get<n>(t);
  _ot<n + 1>(os, t);
}
template <class... T> ostream &operator<<(ostream &os, tuple<T...> const &t) {
  _ot<0>(os, t);
  return os;
}
template <class T, class U>
ostream &operator<<(ostream &os, pair<T, U> const &p) {
  return os << "(" << p.first << ", " << p.second << ") ";
}
template <class T> ostream &operator<<(ostream &os, vector<T> const &v) {
  rep(i, v.size()) os << v[i] << (i + 1 == (int)v.size() ? "" : " ");
  return os;
}
#ifdef DEBUG
#define dump(...)                                                              \\
  (cerr << #__VA_ARGS__ << " = " << mt(__VA_ARGS__) << " [" << __LINE__ << "]" \\
        << endl)
#else
#define dump(...)
#endif

int N;

ll encode(int *v) {
  ll res = 0;
  rep(i, N) res = res * 12 + v[i];
  return res;
}
void decode(ll v, int *a) {
  rep(i, N) {
    a[N - i - 1] = v % 12;
    v /= 12;
  }
}

int main() {
  cin >> N;
  int init[15], goal[15];
  rep(i, N) cin >> init[i];
  rep(i, N) goal[i] = i + 1;
  map<ll, int> x, y;
  ll ans = -1;
  {
    queue<pair<ll, int>> q;
    q.emplace(encode(init), 0);
    x[encode(init)] = 0;
    while (q.size()) {
      ll c, d;
      tie(c, d) = q.front();
      q.pop();
      int e[15];
      decode(c, e);
      if (x[c] < d)
        continue;
      if (d == 4)
        continue;
      rep(i, N + 1) rep(j, i) {
        reverse(e + j, e + i);
        ll w = encode(e);
        if (x.count(w) == 0) {
          q.emplace(w, d + 1);
          x[w] = d + 1;
        }
        reverse(e + j, e + i);
      }
    }
  }
  {
    queue<pair<ll, int>> q;
    q.emplace(encode(goal), 0);
    y[encode(goal)] = 0;
    while (q.size()) {
      ll c, d;
      tie(c, d) = q.front();
      q.pop();
      int e[15];
      decode(c, e);
      if (y[c] < d)
        continue;
      if (d == 4)
        continue;
      if (x.count(c)) {
        ans = d + x[c];
        break;
      }
      rep(i, N + 1) rep(j, i) {
        reverse(e + j, e + i);
        ll w = encode(e);
        if (y.count(w) == 0) {
          q.emplace(w, d + 1);
          y[w] = d + 1;
        }
        reverse(e + j, e + i);
      }
    }
  }
  if (ans == -1)
    ans = 9;
  cout << ans << endl;
}

Error: 0

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
#define all(c) (c).begin(), (c).end()
#define loop(i, a, b) for (ll i = a; i < ll(b); i++)
#define rep(i, b) loop(i, 0, b)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
template <class T> ostream &operator<<(ostream &os, vector<T> const &);
template <int n, class... T>
typename enable_if<(n >= sizeof...(T))>::type _ot(ostream &,
                                                  tuple<T...> const &) {}
template <int n, class... T>
typename enable_if<(n < sizeof...(T))>::type _ot(ostream &os,
                                                 tuple<T...> const &t) {
  os << (n == 0 ? "" : " ") << get<n>(t);
  _ot<n + 1>(os, t);
}
template <class... T> ostream &operator<<(ostream &os, tuple<T...> const &t) {
  _ot<0>(os, t);
  return os;
}
template <class T, class U>
ostream &operator<<(ostream &os, pair<T, U> const &p) {
  return os << "(" << p.first << ", " << p.second << ") ";
}
template <class T> ostream &operator<<(ostream &os, vector<T> const &v) {
  rep(i, v.size()) os << v[i] << (i + 1 == (int)v.size() ? "" : " ");
  return os;
}
#ifdef DEBUG
#define dump(...)                                                              \\
  (cerr << #__VA_ARGS__ << " = " << mt(__VA_ARGS__) << " [" << __LINE__ << "]" \\
        << endl)
#else
#define dump(...)
#endif

int n;
ll encode(vi const &v) {
  ll res = 0;
  rep(i, n) res = res * 11 + v[i];
  return res;
}
vi decode(ll x) {
  vi v(n);
  rep(i, n) {
    v[n - i - 1] = x % 11;
    x /= 11;
  }
  return v;
}

int main() {
  cin >> n;
  vi v(n);
  rep(i, n) cin >> v[i];
  set<ll> dp;
  queue<pair<int, ll>> q;
  dp.insert(encode(v));
  q.emplace(0, encode(v));
  int ans = -1;
  while (q.size()) {
    int d;
    ll x;
    tie(d, x) = q.front();
    q.pop();
    vi u = decode(x);
    bool ok = true;
    rep(i, n - 1) ok &= u[i] + 1 == u[i + 1];
    if (ok) {
      ans = d;
      break;
    }
    rep(i, n + 1) rep(j, i) {
      reverse(u.begin() + j, u.begin() + i);
      if (dp.count(encode(u)) == 0)
        q.emplace(d + 1, encode(u));
      reverse(u.begin() + j, u.begin() + i);
    }
  }
  cout << ans << endl;
}