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

#include <algorithm>
#include <cassert>
#include <climits>
#include <cstdio>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <vector>

#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define inf (1 << 29)

using namespace std;

struct P {
  vector<int> A;
  int cost;
  P(vector<int> A2 = vector<int>(), int cost = inf) : cost(cost) { A = A2; }

  bool operator<(const P &a) const { return cost > a.cost; }
};

int N;
map<vector<int>, int> mincost;
map<vector<int>, int> rmincost;
vector<int> initial;
bool found;

void computeR(int step) {
  priority_queue<P> Q;
  {
    initial.resize(N);
    rep(i, N) initial[i] = i + 1;
    Q.push(P(initial, 0));
    rmincost[initial] = 0;
  }

  // clock_t st,ed;
  // st = clock();
  while (!Q.empty()) {
    P p = Q.top();
    Q.pop();
    if (p.cost >= step)
      continue;

    // ed = clock();
    // if((double)(ed-st)/(double)CLOCKS_PER_SEC >= 1.0)return;

    rep(s, N) {
      REP(t, s + 2, N + 1) {
        vector<int> next = p.A;
        reverse(next.begin() + s, next.begin() + t);
        if (rmincost.find(next) == rmincost.end()) {
          rmincost[next] = p.cost + 1;
          Q.push(P(next, p.cost + 1));
        }
      }
    }
  }
}

void compute(vector<int> &vec) {
  mincost[vec] = 0;
  priority_queue<P> Q;
  Q.push(P(vec, 0));

  int ans = inf;
  found = false;
  while (!Q.empty()) {
    P p = Q.top();
    Q.pop();

    // if(p.cost >= ans)continue;

    if (rmincost.find(p.A) != rmincost.end()) {
      found = true;
      cout << p.cost + rmincost[p.A] << endl;
      return;
      // ans = min(ans,p.cost + rmincost[p.A]);
      continue;
    }

    if (p.cost >= 4)
      continue;

    rep(s, N) {
      REP(t, s + 2, N + 1) {
        vector<int> next = p.A;
        reverse(next.begin() + s, next.begin() + t);
        if (mincost.find(next) == mincost.end()) {
          mincost[next] = p.cost + 1;
          Q.push(P(next, p.cost + 1));
        }
      }
    }
  }
  found = true;
  cout << N - 1 << endl;
}

int main() {

  int a = scanf("%d", &N);

  computeR(4);
  // cout << "---------" << endl;
  vector<int> vec(N);
  rep(i, N) cin >> vec[i];

  compute(vec);

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <climits>
#include <cstdio>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <vector>

#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define inf (1 << 29)

using namespace std;

struct P {
  vector<int> A;
  int cost;
  P(vector<int> A2 = vector<int>(), int cost = inf) : cost(cost) { A = A2; }

  bool operator<(const P &a) const { return cost > a.cost; }
};

int N;
map<vector<int>, int> mincost;
map<vector<int>, int> rmincost;
vector<int> initial;

void computeR(int step) {
  priority_queue<P> Q;
  {
    initial.resize(N);
    rep(i, N) initial[i] = i + 1;
    Q.push(P(initial, 0));
    rmincost[initial] = 0;
  }

  clock_t st, ed;
  st = clock();
  while (!Q.empty()) {
    P p = Q.top();
    Q.pop();
    if (p.cost > step)
      continue;

    ed = clock();
    if ((double)(ed - st) / (double)CLOCKS_PER_SEC >= 3.0)
      return;

    rep(s, N) {
      REP(t, s + 2, N + 1) {
        vector<int> next = p.A;
        reverse(next.begin() + s, next.begin() + t);
        if (rmincost.find(next) == rmincost.end()) {
          rmincost[next] = p.cost + 1;
          Q.push(P(next, p.cost + 1));
        }
      }
    }
  }
}

void compute(vector<int> &vec) {
  mincost[vec] = 0;
  priority_queue<P> Q;
  Q.push(P(vec, 0));

  initial.resize(N);
  rep(i, N) initial[i] = i + 1;

  int ans = inf;

  while (!Q.empty()) {
    P p = Q.top();
    Q.pop();

    // if(p.cost >= ans)continue;

    if (p.A == initial) {
      cout << p.cost << endl;
      return;
    }

    // if(rmincost.find(p.A) != rmincost.end())
    //{
    // ans = min(ans,p.cost + rmincost[p.A]);
    // continue;
    // }

    rep(s, N) {
      REP(t, s + 2, N + 1) {
        vector<int> next = p.A;
        reverse(next.begin() + s, next.begin() + t);
        if (mincost.find(next) == mincost.end()) {
          mincost[next] = p.cost + 1;
          Q.push(P(next, p.cost + 1));
        }
      }
    }
  }
  // cout << ans << endl;
  // assert(ans != inf);
}

int main() {

  scanf("%d", &N);

  // computeR((int)(N/2));
  // cout << "---------" << endl;
  vector<int> vec(N);
  rep(i, N) cin >> vec[i];

  compute(vec);

  return 0;
}