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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define _1 first
#define _2 second
#define pb push_back
#define INF 1145141919
#define MOD 1000000007

int N;

map<vector<int>, int> bfs(vector<int> s, int depth) {
  map<vector<int>, int> dp;
  queue<vector<int>> q;
  q.push(s);
  dp[s] = 0;

  while (!q.empty()) {
    vector<int> x = q.front();
    q.pop();
    int f = dp[x];
    if (f + 1 >= depth)
      continue;

    int n = x.size();
    for (int l = 0; l < n; l++) {
      for (int r = l + 1; r < n; r++) {
        vector<int> t(x);
        rep(s, (r - l + 1) / 2) swap(t[l + s], t[r - s]);
        if (dp.find(t) == dp.end()) {
          dp[t] = f + 1;
          q.push(t);
        }
      }
    }
  }
  return dp;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N;
  vector<int> s;
  rep(i, N) {
    int a;
    cin >> a;
    a--;
    s.pb(a);
  }

  vector<int> id(N);
  rep(i, N) id[i] = i;

  int m = N - 1;
  auto lp = bfs(id, 4), rp = bfs(s, 4);
  for (auto &p : lp) {
    if (rp.find(p._1) == rp.end())
      continue;
    m = min(m, p._2 + rp[p._1]);
  }
  cout << m << "\\n";
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define _1 first
#define _2 second
#define pb push_back
#define INF 1145141919
#define MOD 1000000007

int N;

vector<int> norm(vector<int> seq) {
  int m = *min_element(all(seq));
  rep(i, seq.size()) seq[i] -= m;
  bool u[seq.size()];
  rep(i, seq.size()) u[i] = false;
  rep(i, seq.size()) {
    if (seq[i] == i)
      u[i] = true;
    else
      break;
  }
  for (int i = (int)seq.size() - 1; i >= 0; i--) {
    if (seq[i] == i)
      u[i] = true;
    else
      break;
  }
  vector<int> ret;
  rep(i, seq.size()) if (!u[i]) ret.pb(seq[i]);

  m = *min_element(all(ret));
  rep(i, ret.size()) ret[i] -= m;
  return ret;
}
vector<int> swap_range(vector<int> &seq, int l, int r) {
  vector<int> ret(seq);
  rep(s, (r - l + 1) / 2) swap(ret[l + s], ret[r - s]);
  return ret;
}

int f(vector<int> seq) {
  if (seq.empty())
    return 0;
  int first = -1, last = -1;
  rep(i, seq.size()) {
    if (seq[i] == 0)
      first = i;
    if (seq[i] == (int)seq.size() - 1)
      last = i;
  }
  assert(first != -1 && last != -1);

  vector<int> ls = swap_range(seq, 0, first);
  vector<int> rs = swap_range(seq, last, seq.size() - 1);
  // cout<<"{"; for (int x:seq)cout<<x<<",";cout<<"} -> "<<r<<"\\n";
  return 1 + min(f(norm(ls)), f(norm(rs)));
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N;
  vector<int> s;
  rep(i, N) {
    int a;
    cin >> a;
    s.pb(a);
  }
  cout << f(norm(s)) << "\\n";
  return 0;
}