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

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

#define dump(n) cout << "# " << #n << '=' << (n) << endl
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define peri(i, a, b) for (int i = int(b); i-- > int(a);)
#define rep(i, n) repi(i, 0, n)
#define per(i, n) peri(i, 0, n)
#define all(c) begin(c), end(c)
#define mp make_pair
#define mt make_tuple

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;

const int INF = 1e9;
const int MOD = 1e9 + 7;
const double EPS = 1e-9;

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  return os << '(' << p.first << ',' << p.second << ')';
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &a) {
  os << '[';
  rep(i, a.size()) os << (i ? " " : "") << a[i];
  return os << ']';
}

ll encode(const vi &a) {
  ll x = 0;
  rep(i, a.size()) x |= ll(a[i]) << i * 4;
  return x;
}
vi decode(ll x) {
  vi a;
  for (; x; x >>= 4)
    a.push_back(x & 15);
  return a;
}

// vvi f;
//
// inline int encode(const vi& a)
//{
//	return lower_bound(all(f),a)-begin(f);
// }
// inline vi decode(int x)
//{
//	return f[x];
// }

int main() {
  for (int n; cin >> n && n;) {
    vi a(n);
    rep(i, n) cin >> a[i];

    vi b(n);
    iota(all(b), 1);
    if (a == b) {
      cout << 0 << endl;
      continue;
    }

    queue<pair<ll, int>> q[2];
    unordered_map<ll, int> m[2];
    q[0].emplace(encode(a), 0);
    m[0].insert(mp(encode(a), 0));
    q[1].emplace(encode(b), 0);
    m[1].insert(mp(encode(b), 0));

    int res = INF;
    for (int k = 0;; k ^= 1) {
      // dump(q[k].size());
      queue<pair<ll, int>> tmp;
      while (q[k].size()) {
        ll x;
        int t;
        tie(x, t) = q[k].front();
        q[k].pop();
        if (m[k ^ 1].count(x)) {
          res = t + m[k ^ 1][x];
          goto end;
        }
        vi a = decode(x);
        rep(i, n) repi(j, i, n) {
          reverse(begin(a) + i, begin(a) + j + 1);
          ll x = encode(a);
          if (!m[k].count(x)) {
            tmp.emplace(encode(a), t + 1);
            m[k][x] = t + 1;
          }
          reverse(begin(a) + i, begin(a) + j + 1);
        }
      }
      swap(q[k], tmp);
    }
  end:
    cout << res << endl;
  }
}

Error: MLE

Fixed submission:

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

#define dump(n) cout << "# " << #n << '=' << (n) << endl
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define peri(i, a, b) for (int i = int(b); i-- > int(a);)
#define rep(i, n) repi(i, 0, n)
#define per(i, n) peri(i, 0, n)
#define all(c) begin(c), end(c)
#define mp make_pair
#define mt make_tuple

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;

const int INF = 1e9;
const int MOD = 1e9 + 7;
const double EPS = 1e-9;

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  return os << '(' << p.first << ',' << p.second << ')';
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &a) {
  os << '[';
  rep(i, a.size()) os << (i ? " " : "") << a[i];
  return os << ']';
}

// ll encode(const vi& a)
//{
//	ll x=0;
//	rep(i,a.size()) x|=ll(a[i])<<i*4;
//	return x;
// }
// vi decode(ll x)
//{
//	vi a;
//	for(;x;x>>=4) a.push_back(x&15);
//	return a;
// }

vvi f;

inline int encode(const vi &a) { return lower_bound(all(f), a) - begin(f); }
inline vi decode(int x) { return f[x]; }

int main() {
  for (int n; cin >> n && n;) {
    {
      f.clear();
      vi t(n);
      iota(all(t), 1);
      do {
        f.push_back(t);
      } while (next_permutation(all(t)));
    }

    vi a(n);
    rep(i, n) cin >> a[i];

    vi b(n);
    iota(all(b), 1);
    if (a == b) {
      cout << 0 << endl;
      continue;
    }

    queue<pii> q[2];
    vvi m(2, vi(f.size(), INF));
    q[0].emplace(encode(a), 0);
    m[0][encode(a)] = 0;
    q[1].emplace(encode(b), 0);
    m[1][encode(b)] = 0;

    int res = INF;
    for (int k = 0;; k ^= 1) {
      queue<pii> tmp;
      while (q[k].size()) {
        int x;
        int t;
        tie(x, t) = q[k].front();
        q[k].pop();
        if (m[k ^ 1][x] != INF) {
          res = t + m[k ^ 1][x];
          goto end;
        }
        vi a = decode(x);
        rep(i, n) repi(j, i, n) {
          reverse(begin(a) + i, begin(a) + j + 1);
          int x = encode(a);
          if (m[k][x] == INF) {
            tmp.emplace(encode(a), t + 1);
            m[k][x] = t + 1;
          }
          reverse(begin(a) + i, begin(a) + j + 1);
        }
      }
      swap(q[k], tmp);
    }
  end:
    cout << res << endl;
  }
}