Problem p01557 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, vector<int>> P;

#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcount

#define INF 1e16
#define mod 1000000007

int n;
vector<int> a;

ll f(const vector<int> &v) {
  ll res = 0;
  rep(i, n) {
    res *= 10LL;
    res += v[i];
  }
  return res;
}

map<ll, int> d1, d2;

int main() {
  cin >> n;
  rep(i, n) {
    int b;
    cin >> b;
    a.push_back(b);
  }
  d1[f(a)] = 0;
  queue<vector<int>> que;
  que.push(a);
  while (que.size()) {
    vector<int> crt = que.front();
    que.pop();
    if (d1[f(crt)] >= (n / 2) - 1)
      continue;
    rep(i, n) repl(j, i + 1, n) {
      vector<int> nxt = crt;
      reverse(nxt.begin() + i, nxt.begin() + j + 1);
      if (d1.find(f(nxt)) != d1.end())
        continue;
      d1[f(nxt)] = d1[f(crt)] + 1;
      que.push(nxt);
    }
  }

  vector<int> p;
  rep(i, n) p.push_back(i + 1);
  que.push(p);
  d2[f(p)] = 0;
  while (que.size()) {
    vector<int> crt = que.front();
    que.pop();
    if (d1.find(f(crt)) != d1.end()) {
      cout << d1[f(crt)] + d2[f(crt)] << endl;
      return 0;
    }
    rep(i, n) repl(j, i + 1, n) {
      vector<int> nxt = crt;
      reverse(nxt.begin() + i, nxt.begin() + j + 1);
      if (d2.find(f(nxt)) != d2.end())
        continue;
      d2[f(nxt)] = d2[f(crt)] + 1;
      que.push(nxt);
    }
  }
  return 0;
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, vector<int>> P;

#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcount

#define INF 1e16
#define mod 1000000007

int n;
vector<int> a;
map<vector<int>, int> d1, d2;

int main() {
  cin >> n;
  rep(i, n) {
    int b;
    cin >> b;
    a.push_back(b);
  }
  d1[a] = 0;
  queue<vector<int>> que;
  que.push(a);
  while (que.size()) {
    vector<int> crt = que.front();
    que.pop();
    if (d1[crt] >= (n / 2) - 1)
      continue;
    rep(i, n) repl(j, i + 1, n) {
      vector<int> nxt = crt;
      reverse(nxt.begin() + i, nxt.begin() + j + 1);
      if (d1.find(nxt) != d1.end())
        continue;
      d1[nxt] = d1[crt] + 1;
      que.push(nxt);
    }
  }

  vector<int> p;
  rep(i, n) p.push_back(i + 1);
  que.push(p);
  d2[p] = 0;
  while (que.size()) {
    vector<int> crt = que.front();
    que.pop();
    if (d1.find(crt) != d1.end()) {
      cout << d1[crt] + d2[crt] << endl;
      return 0;
    }
    rep(i, n) repl(j, i + 1, n) {
      vector<int> nxt = crt;
      reverse(nxt.begin() + i, nxt.begin() + j + 1);
      if (d2.find(nxt) != d2.end())
        continue;
      d2[nxt] = d2[crt] + 1;
      que.push(nxt);
    }
  }
  return 0;
}