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

#include <bits/stdc++.h>
#define syosu(x) fixed << setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<int, P> pip;
typedef vector<pip> vip;
const int inf = 1 << 29;
const ll INF = 1ll << 60;
const double pi = acos(-1);
const double eps = 1e-8;
const ll mod = 1e9 + 7;
const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1};

const int M = 3628800;
int n;

int g(vi a) {
  int res = 0, tmp = 1;
  for (int i = n - 2; i >= 0; i--) {
    int cnt = 0;
    for (int j = i + 1; j < n; j++)
      if (a[i] > a[j])
        cnt++;
    res += cnt * tmp;
    tmp *= n - i;
  }
  return res;
}

vi a;

vi f() {
  queue<pair<vi, int>> q;
  q.push(make_pair(a, 0));
  vi dis(M, inf);
  dis[g(a)] = 0;
  while (!q.empty()) {
    auto p = q.front();
    q.pop();
    vi b = p.first;
    int d = p.second;
    for (int i = 0; i < n; i++)
      for (int j = i + 2; j <= n; j++) {
        reverse(b.begin() + i, b.begin() + j);
        int id = g(b);
        if (dis[id] == inf) {
          dis[id] = d + 1;
          if (d + 1 < 4) {
            q.push(make_pair(b, d + 1));
          }
        }
        reverse(b.begin() + i, b.begin() + j);
      }
  }
  return dis;
}

int main() {
  cin >> n;
  a = vi(n);
  for (auto &i : a)
    cin >> i;
  vi b = f();
  sort(a.begin(), a.end());
  vi c = f();
  int res = 9;
  for (int i = 0; i < M; i++)
    res = min(res, b[i] + c[i]);
  cout << res << endl;
}


Error: MLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <typeinfo>
#include <vector>
#define syosu(x) fixed << setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<P, int> pip;
typedef vector<pip> vip;
const int inf = 1 << 28;
const ll INF = 1ll << 60;
const double pi = acos(-1);
const double eps = 1e-8;
const ll mod = 1e9 + 7;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};

int n;
vi a;

map<vi, int> m;

int main() {
  cin >> n;
  a = vi(n);
  vi b(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
    b[i] = i;
  }
  m[b] = 0;
  queue<pair<vi, int>> q;
  q.push({b, 0});
  while (!q.empty()) {
    vi p = q.front().first;
    int t = q.front().second;
    q.pop();
    for (int i = 1; i <= n; i++)
      for (int j = 0; j < i; j++) {
        vi p_ = p;
        reverse(p_.begin() + j, p_.begin() + i);
        if (m.find(p_) == m.end()) {
          m[p_] = t + 1;
          q.push({p_, t + 1});
        }
      }
  }
  cout << m[a] << endl;
}