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

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)

int N;
vector<int> A, B;

map<vector<int>, int> bfs(vector<int> start) {
  map<vector<int>, int> memo;
  queue<vector<int>> Q;
  Q.push(start);
  memo[start] = 0;

  while (!Q.empty()) {
    vector<int> vi = Q.front();
    Q.pop();

    int cost = memo[vi];

    if (cost >= (N - 1) / 2)
      continue;

    for (int i = 0; i < N; i++) {
      for (int j = i + 2; j <= N; j++) {
        vector<int> tmp = vi;
        reverse(tmp.begin() + i, tmp.begin() + j);
        if (memo.find(tmp) == memo.end()) {
          memo[tmp] = cost + 1;
          Q.push(tmp);
        }
      }
    }
  }

  return memo;
}

int solve() {
  int ans = N - 1;
  map<vector<int>, int> memo_A = bfs(A);
  map<vector<int>, int> memo_B = bfs(B);
  if (memo_A.find(B) != memo_A.end())
    ans = memo_A[B];
  else {
    do {
      if (memo_A.find(B) == memo_A.end())
        continue;
      if (memo_B.find(B) == memo_B.end())
        continue;
      ans = min(ans, memo_A[B] + memo_B[B]);
    } while (next_permutation(B.begin(), B.end()));
  }
  return ans;
}

int main() {
  cin >> N;
  rep(i, N) {
    int a;
    cin >> a;
    A.push_back(a);
  }
  B = A;
  sort(B.begin(), B.end());
  cout << solve() << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <vector>

#define cost first
#define state second
#define all(c) (c).begin(), (c).end()

using namespace std;

typedef unsigned long long ull;
typedef pair<int, ull> p2;

map<ull, int> dist;

uint encode(vector<int> vec) {
  uint res = 0;
  for (int i = 0; i < vec.size(); i++)
    res = (res << 4) + vec[i];
  return res;
}

vector<int> decode(ull a) {
  vector<int> res;
  for (; a > 0; a >>= 4)
    res.push_back(a & 15);
  reverse(res.begin(), res.end());
  return res;
}

int n;
queue<p2> que;

void bfs(vector<int> start) {

  que.push(p2(0, encode(start)));
  dist[encode(start)] = 0;
  while (!que.empty()) {
    p2 now = que.front();
    que.pop();

    if (dist.find(now.state) != dist.end() && dist[now.state] < now.cost)
      continue;
    dist[now.state] = now.cost;
    if (now.cost >= (n - 1) / 2)
      return;

    vector<int> t = decode(now.state);

    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        reverse(t.begin() + i, t.begin() + j + 1);
        bool fg = dist.find(encode(t)) == dist.end();
        if (fg || (!fg && dist[encode(t)] > now.cost + 1))
          que.push(p2(now.cost + 1, encode(t)));
        reverse(t.begin() + i, t.begin() + j + 1);
      }
    }
  }
}

int bfs2(vector<int> start) {

  queue<p2> que;
  que.push(p2(0, encode(start)));

  while (!que.empty()) {
    p2 now = que.front();
    que.pop();

    if (dist.find(now.state) != dist.end())
      return now.cost + dist[now.state];
    if (now.cost > (n - 1) / 2)
      return -1;

    vector<int> t = decode(now.state);

    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        reverse(t.begin() + i, t.begin() + j + 1);
        que.push(p2(now.cost + 1, encode(t)));
        reverse(t.begin() + i, t.begin() + j + 1);
      }
    }
  }
  return -1;
}

int main(void) {

  cin >> n;

  vector<int> v(n), g(n);
  for (int i = 0; i < n; i++)
    cin >> v[i], g[i] = i + 1;

  bfs(g);

  cout << bfs2(v) << endl;

  return 0;
}