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

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

short N;

struct Array {
  short a[10], d;
  Array() {}
  bool operator==(const Array &obj) const {
    for (short i = 0; i < N; i++) {
      if (a[i] != obj.a[i])
        return false;
    }
    return true;
  }
  bool operator<(const Array &obj) const {
    for (short i = 0; i < N; i++) {
      if (a[i] < obj.a[i])
        return true;
      if (a[i] > obj.a[i])
        return false;
    }
    return d < obj.d;
  }
};

Array A, I;
vector<Array> vec, vec2;

void dfs(Array arr, short dep, vector<Array> &vec) {
  vec.push_back(arr);
  if (dep == 4)
    return;

  Array tmp;
  for (short i = 0; i < N; i++) {
    for (short j = 0; j < N; j++) {
      if (i >= j)
        continue;
      for (short k = 0; k < N; k++)
        tmp.a[k] = arr.a[k];
      for (short k = i; k <= j; k++)
        tmp.a[k] = arr.a[j - (k - i)];
      tmp.d = arr.d + 1;
      dfs(tmp, dep + 1, vec);
    }
  }
}

int main(void) {
  cin >> N;
  for (short i = 0; i < N; i++)
    cin >> A.a[i];
  for (short i = 0; i < N; i++)
    I.a[i] = i + 1;
  A.d = I.d = 0;

  dfs(A, 0, vec);
  dfs(I, 0, vec2);

  sort(vec.begin(), vec.end());
  vec.erase(unique(vec.begin(), vec.end()), vec.end());

  sort(vec2.begin(), vec2.end());
  vec2.erase(unique(vec2.begin(), vec2.end()), vec2.end());

  short ans = N - 1;
  Array tmp;
  for (short i = 0; i < vec.size(); i++) {
    tmp = vec[i];
    tmp.d = 0;
    auto p = lower_bound(vec2.begin(), vec2.end(), tmp);
    if (*p == vec[i]) {
      ans = min(ans, (short)(p->d + vec[i].d));
    }
    if (vec[i] == I)
      ans = min(ans, vec[i].d);
  }

  cout << ans << endl;

  return 0;
}


Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int N;

struct Array {
  int a[10], d;
  Array() {}
  Array(int b[], int x) {
    for (int i = 0; i < N; i++)
      a[i] = b[i];
    d = x;
  }
  bool operator==(const Array &obj) const {
    for (int i = 0; i < N; i++) {
      if (a[i] != obj.a[i])
        return false;
    }
    return true;
  }
  bool operator<(const Array &obj) const {
    for (int i = 0; i < N; i++) {
      if (a[i] < obj.a[i])
        return true;
      if (a[i] > obj.a[i])
        return false;
    }
    return false;
  }
};

Array A, I;
vector<Array> vec, vec2;

void dfs(Array arr, int dep, vector<Array> &vec) {
  vec.push_back(arr);
  if (dep == 4)
    return;

  Array tmp;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      if (i >= j)
        continue;
      for (int k = 0; k < N; k++)
        tmp.a[k] = arr.a[k];
      for (int k = i; k <= j; k++)
        tmp.a[k] = arr.a[j - (k - i)];
      tmp.d = arr.d + 1;
      dfs(tmp, dep + 1, vec);
    }
  }
}

int main(void) {
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> A.a[i];
  for (int i = 0; i < N; i++)
    I.a[i] = i + 1;
  A.d = I.d = 0;

  dfs(A, 0, vec);
  dfs(I, 0, vec2);
  sort(vec2.begin(), vec2.end());

  int ans = N - 1;
  for (int i = 0; i < vec.size(); i++) {
    auto p = lower_bound(vec2.begin(), vec2.end(), vec[i]);
    if (*p == vec[i]) {
      ans = min(ans, p->d + vec[i].d);
    }
    if (vec[i] == I)
      ans = min(ans, vec[i].d);
  }
  cout << ans << endl;

  return 0;
}