alexjercan/bugnet huggingface dataset:
Problem p01557 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

int main() {
  int N;
  cin >> N;

  vector<int> a(N), b(N);

  for (int i = 0; i < N; i++) {
    cin >> a[i];
    b[i] = i + 1;
  }

  map<vector<int>, int> d, h;
  int ans = N - 1;

  // from goal
  {
    h[b] = 0;
    queue<vector<int>> q;
    q.push(b);

    while (!q.empty()) {
      vector<int> v = q.front();
      int cost = h[v];
      q.pop();

      if (v == a) {
        ans = cost;
        break;
      }
      if (cost >= 4)
        break;

      for (int i = 0; i < N; i++)
        for (int j = i + 1; j < N; j++) {
          vector<int> next = v;
          reverse(next.begin() + i, next.begin() + j + 1);
          if (!h.count(next)) {
            h[next] = cost + 1;
            q.push(next);
          }
        }
    }
  }

  // from start
  {
    d[a] = 0;
    queue<vector<int>> q;
    q.push(a);

    while (!q.empty()) {
      vector<int> v = q.front();
      int cost = d[v];
      q.pop();

      if (h.count(v)) {
        ans = cost + h[v];
        break;
      }

      if (cost >= 4)
        break;

      for (int i = 0; i < N; i++)
        for (int j = i + 1; j < N; j++) {
          vector<int> next = v;
          reverse(next.begin() + i, next.begin() + j + 1);
          if (!d.count(next)) {
            d[next] = cost + 1;
            q.push(next);
          }
        }
    }
  }
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

int main() {
  int N;

  while (cin >> N, N) {
    vector<int> a(N), b(N);

    for (int i = 0; i < N; i++) {
      cin >> a[i];
      b[i] = i + 1;
    }

    map<vector<int>, int> d, h;
    int ans = N - 1;

    // from goal
    {
      h[b] = 0;
      queue<vector<int>> q;
      q.push(b);

      while (!q.empty()) {
        vector<int> v = q.front();
        int cost = h[v];
        q.pop();

        if (v == a) {
          ans = cost;
          break;
        }
        if (cost >= 4)
          break;

        for (int i = 0; i < N; i++)
          for (int j = i + 1; j < N; j++) {
            vector<int> next = v;
            reverse(next.begin() + i, next.begin() + j + 1);
            if (!h.count(next)) {
              h[next] = cost + 1;
              q.push(next);
            }
          }
      }
    }

    // from start
    {
      d[a] = 0;
      queue<vector<int>> q;
      q.push(a);

      while (!q.empty()) {
        vector<int> v = q.front();
        int cost = d[v];
        q.pop();

        if (h.count(v)) {
          ans = cost + h[v];
          break;
        }

        if (cost >= 4)
          break;

        for (int i = 0; i < N; i++)
          for (int j = i + 1; j < N; j++) {
            vector<int> next = v;
            reverse(next.begin() + i, next.begin() + j + 1);
            if (!d.count(next)) {
              d[next] = cost + 1;
              q.push(next);
            }
          }
      }
    }
    cout << ans << endl;
  }

  return 0;
}
Problem p01557 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <map>
#include <queue>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)

#include <iostream>
typedef long long ll;

#include <cstdio>
#include <set>
inline int getInt() {
  int s;
  scanf("%d", &s);
  return s;
}

using namespace std;

struct state {
  ll v;
  state() : v(0) {}
  state(const vector<int> &v) : v(0) { REP(i, v.size()) set(i, v[i]); }
  vector<int> decode(int n) const {
    vector<int> v(n);
    REP(i, n) v[i] = get(i);
    return v;
  }

  inline void set(int pos, int val) {
    v &= ~(((1ll << ((pos + 1) * 4)) - 1) - ((1ll << (pos * 4)) - 1));
    v |= ((ll)val) << (pos * 4);
  }

  inline int get(int pos) const { return (v >> (pos * 4)) & ((1 << 4) - 1); }
};

map<ll, int> solve(int n, state s, map<ll, int> *prev = NULL) {
  map<ll, int> ret;

  queue<state> q;
  ret[s.v] = 0;
  q.push(s);

  while (q.size()) {
    const state st = q.front();
    q.pop();
    const int cnt = ret[st.v];

    if (prev != NULL && prev->count(st.v))
      throw(*prev)[st.v] + cnt;
    if (cnt == 4)
      continue;

    REP(i, n) REP(j, i) {
      vector<int> v = st.decode(n);
      reverse(v.begin() + j, v.begin() + i + 1);
      const state next(v);
      if (ret.count(next.v) == 0) {
        ret[next.v] = cnt + 1;
        q.push(next);
      }
    }
  }

  return ret;
}

int main() {
  const int n = getInt();
  state init;
  state goal;
  REP(i, n) init.set(i, getInt() - 1);
  REP(i, n) goal.set(i, i);

  if (init.v == goal.v) {
    cout << 0 << endl;
  } else {
    map<ll, int> s1 = solve(n, init);
    try {
      map<ll, int> s2 = solve(n, goal, &s1);
      cout << 9 << endl;
    } catch (int e) {
      cout << e << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <map>
#include <queue>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)

typedef long long ll;

#include <cstdio>
#include <set>

using namespace std;

inline int getInt() {
  int s;
  scanf("%d", &s);
  return s;
}

ll encode(const vector<int> &v) {
  ll ret = 0;
  const int n = v.size();
  REP(i, n) ret |= (ll)(v[i]) << 4 * i;
  return ret;
}

vector<int> decode(const ll val, int n) {
  vector<int> ret(n);
  REP(i, n) ret[i] = (val >> (4 * i)) & ((1 << 4) - 1);
  return ret;
}

int main() {
  const int n = getInt();
  ll s = 0;
  ll d = 0;
  REP(i, n) s |= (ll)(getInt() - 1) << (4 * i);
  REP(i, n) d |= (ll)(i) << (4 * i);

  map<ll, int> memo;
  queue<ll> q;

  memo[s] = 0;
  q.push(s);

  while (q.size()) {
    const ll state = q.front();
    q.pop();
    vector<int> v = decode(state, n);
    const int cnt = memo[state];

    if (state == d) {
      printf("%d\\n", cnt);
      break;
    }

    REP(i, n) for (int j = i + 1; j <= n; j++) {
      reverse(v.begin() + i, v.begin() + j);

      const ll next = encode(v);
      if (memo.count(next) == 0) {
        memo[next] = cnt + 1;
        q.push(next);
      }

      reverse(v.begin() + i, v.begin() + j);
    }
  }

  return 0;
}
Problem p01557 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define r(i, n) for (int i = 0; i < n; i++)
typedef pair<vector<int>, int> P;

int n;

map<vector<int>, int> BFS(vector<int> A) {
  map<vector<int>, int> m;
  m[A] = 0;
  queue<P> q;
  q.push(P(A, 0));
  while (!q.empty()) {
    vector<int> B = q.front().first;
    int cost = q.front().second;
    q.pop();
    if (cost == 4)
      continue;
    for (int l = 0; l < n - 1; l++) {
      for (int r = l + 1; r < n; r++) {
        vector<int> C = B;
        reverse(C.begin() + l, C.begin() + r + 1);
        if (!m.count(C)) {
          m[C] = cost + 1;
          q.push(P(C, cost + 1));
        }
      }
    }
  }
  return m;
}

int main() {
  cin >> n;
  vector<int> A(n), B(n);
  r(i, n) cin >> B[i], A[i] = i + 1;
  map<vector<int>, int> m1 = BFS(A);
  map<vector<int>, int> m2 = BFS(B);
  int ans = 9;
  do {
    if (m1.count(A) && m2.count(A)) {
      ans = min(ans, m1[A] + m2[A]);
    }
  } while (next_permutation(A.begin(), A.end()));
  cout << ans << endl;
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define r(i, n) for (int i = 0; i < n; i++)
typedef pair<vector<int>, int> P;

int n;

map<vector<int>, int> BFS(vector<int> A) {
  map<vector<int>, int> m;
  m[A] = 0;
  queue<P> q;
  q.push(P(A, 0));
  while (!q.empty()) {
    vector<int> B = q.front().first;
    int cost = q.front().second;
    q.pop();
    if (cost == 5)
      continue;
    for (int l = 0; l < n - 1; l++) {
      for (int r = l + 1; r < n; r++) {
        vector<int> C = B;
        reverse(C.begin() + l, C.begin() + r + 1);
        if (!m.count(C)) {
          m[C] = cost + 1;
          q.push(P(C, cost + 1));
        }
      }
    }
  }
  return m;
}

int main() {
  cin >> n;
  vector<int> A(n), B(n);
  r(i, n) cin >> B[i], A[i] = i + 1;
  map<vector<int>, int> m1 = BFS(A);
  map<vector<int>, int> m2 = BFS(B);
  do {
    if (m1.count(A) && m2.count(A)) {
      cout << m1[A] + m2[A] << endl;
      return 0;
    }
  } while (next_permutation(A.begin(), A.end()));
}
Problem p01557 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef long long ll;
typedef long double ld;
typedef vector<short> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/

int n;

map<vi, short> bfs(vi v) {
  map<vi, short> mp;
  queue<vi> que;
  que.push(v);
  mp[v] = 0;
  while (que.size()) {
    vi tmp = que.front();
    que.pop();
    int t = mp[tmp];
    if (t >= (n - 1) / 2)
      continue;
    REP(i, n) REP(j, i) {
      vi nx = tmp;
      reverse(nx.begin() + j, nx.begin() + i + 1);
      if (mp.find(nx) != mp.end())
        continue;
      else {
        mp[nx] = t + 1;
        que.push(nx);
      }
    }
  }
  return mp;
}

signed main() {
  cin >> n;
  vi a(n), b;
  REP(i, n) cin >> a[i];
  b = a;
  sort(ALL(b));
  auto mp1 = bfs(a);
  auto mp2 = bfs(b);
  int ans = n - 1;
  for (auto it = mp1.begin(); it != mp1.end(); it++) {
    if (mp2.find(it->first) != mp2.end())
      chmin(ans, it->second + mp2[it->first]);
  }
  cout << ans << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef long long ll;
typedef long double ld;
typedef vector<short> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/

int n;

map<vi, short> bfs(vi v) {
  map<vi, short> mp;
  queue<vi> que;
  que.push(v);
  mp[v] = 0;
  while (que.size()) {
    vi tmp = que.front();
    que.pop();
    int t = mp[tmp];
    if (t >= n / 2)
      continue;
    REP(i, n) REP(j, i) {
      vi nx = tmp;
      reverse(nx.begin() + j, nx.begin() + i + 1);
      if (mp.find(nx) != mp.end())
        continue;
      else {
        mp[nx] = t + 1;
        que.push(nx);
      }
    }
  }
  return mp;
}

signed main() {
  cin >> n;
  vi a(n), b;
  REP(i, n) cin >> a[i];
  b = a;
  sort(ALL(b));
  auto mp1 = bfs(a);
  auto mp2 = bfs(b);
  int ans = n - 1;
  for (auto it = mp1.begin(); it != mp1.end(); it++) {
    if (mp2.find(it->first) != mp2.end())
      chmin(ans, it->second + mp2[it->first]);
  }
  cout << ans << endl;
  return 0;
}
Problem p01557 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define MAX 10
#define INF 1e9
typedef vector<int> Vec;

int N, res;
map<Vec, int> dist[2];
Vec v[2];

void bfs(int p) {
  int t = !p;
  Vec next;
  queue<Vec> que;

  dist[p][v[p]] = 0;
  que.push(v[p]);

  while (!que.empty()) {
    Vec s = que.front();
    que.pop();

    if (s == v[t]) {
      res = dist[p][s];
      break;
    }

    if (dist[p][s] >= (N - 1) / 2) {
      break;
    }

    for (int i = 0; i < N - 1; i++) {
      for (int j = i + 1; j < N; j++) {
        next = s;
        reverse(next.begin() + i, next.begin() + j + 1);

        if (!dist[p].count(next)) {
          dist[p][next] = dist[p][s] + 1;
          que.push(next);
        }
      }
    }
  }
}

int main() {
  cin >> N;
  v[0].resize(N);
  for (int i = 0; i < N; i++) {
    cin >> v[0][i];
    v[1].push_back(i + 1);
  }

  res = 9;
  bfs(0);
  bfs(1);
  map<Vec, int>::iterator it, val;

  for (it = dist[0].begin(); it != dist[0].end(); ++it) {
    val = dist[1].find(it->first);
    if (val != dist[1].end()) {
      res = min(res, it->second + val->second);
    }
  }
  cout << res << endl;

  return 0;
}

Error: MLE

Fixed submission:

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

using namespace std;

#define MAX 10
#define INF 1e9
typedef vector<int> Vec;

int N;
map<Vec, int> dist[2];

void bfs(int p, Vec &start) {
  Vec st;
  queue<Vec> que;

  dist[p][start] = 0;
  que.push(start);

  while (!que.empty()) {
    Vec s = que.front();
    que.pop();

    if (dist[p][s] >= N / 2) {
      break;
    }

    for (int i = 0; i < N - 1; i++) {
      for (int j = i + 1; j < N; j++) {
        Vec next = s;
        reverse(next.begin() + i, next.begin() + j + 1);

        if (!dist[p].count(next)) {
          dist[p][next] = dist[p][s] + 1;
          que.push(next);
        }
      }
    }
  }
}

int main() {
  Vec start, goal;

  cin >> N;
  start.resize(N);
  for (int i = 0; i < N; i++) {
    cin >> start[i];
    goal.push_back(i + 1);
  }

  bfs(0, start);
  bfs(1, goal);
  map<Vec, int>::iterator it, val;

  int min = INF;
  for (it = dist[0].begin(); it != dist[0].end(); ++it) {
    val = dist[1].find(it->first);
    if (val != dist[1].end()) {
      min = std::min(min, it->second + val->second);
    }
  }
  cout << min << endl;

  return 0;
}
Problem p01557 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

const int N = 10;
const int D = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2;
const int INF = 1 << 29;

struct state {
  vector<int> v;
  int cnt;
  state(vector<int> v = vector<int>(), int cnt = 0) : v(v), cnt(cnt) {}
};

int n, d[N], wd[2][D];
vector<int> v;

int hash(const vector<int> &vec) {
  int res = 0;
  bool f[N];
  fill(f, f + n, false);

  for (int i = 0; i < n - 1; i++) {
    int cnt = 0;
    for (int j = 0; j < vec[i] - 1; j++) {
      if (f[j])
        cnt++;
    }
    res += d[n - 2 - i] * (vec[i] - cnt - 1);
    f[vec[i] - 1] = true;
  }

  return res;
}

int bfs(const vector<int> &vec, int memo[], bool is_first) {
  int res = INF;
  queue<state> q;
  state u, u2;

  fill(memo, memo + D, INF);
  memo[hash(vec)] = 0;

  for (q.push(state(vec, 0)); !q.empty();) {
    u = q.front();
    q.pop();

    if (!is_first)
      res = min(res, wd[0][hash(u.v)] + u.cnt);
    if (is_first && u.cnt >= 4 || u.cnt >= 5 || res == 9)
      continue;

    for (int i = 0; i < n - 1; i++) {
      for (int j = i + 1; j < n; j++) {
        int hs;
        u2 = state(u.v, u.cnt + 1);
        reverse(u2.v.begin() + i, u2.v.begin() + j + 1);
        hs = hash(u2.v);
        if (memo[hs] > u2.cnt) {
          memo[hs] = u2.cnt;
          q.push(u2);
        }
      }
    }
  }

  return res;
}

int solve() {
  vector<int> w = v;

  sort(w.begin(), w.end());

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (v[i] == w[j]) {
        v[i] = j + 1;
        break;
      }
    }
  }

  w = v;
  sort(w.begin(), w.end());

  bfs(w, wd[0], true);
  return bfs(v, wd[1], false);
}

int main() {
  d[0] = 1;
  for (int i = 1; i < N; i++)
    d[i] = d[i - 1] * (i + 1);

  while (cin >> n) {
    v.clear();
    for (int i = 0; i < n; i++) {
      int x;
      cin >> x;
      v.push_back(x);
    }
    cout << solve() << endl;
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <vector>
using namespace std;

typedef pair<vector<int>, int> P;

set<vector<int>> s;
vector<int> ansv;
int n, ans;

bool f;

void func(vector<int> v) {
  queue<P> Q;
  P u;
  Q.push(P(v, 1));
  while (!Q.empty()) {
    u = Q.front();
    Q.pop();
    for (int i = 0; i < n - 1; i++) {
      for (int j = i + 2; j <= n; j++) {
        vector<int> v2 = u.first;
        reverse(v2.begin() + i, v2.begin() + j);
        if (v2 == ansv) {
          ans = u.second;
          return;
        } else if (s.find(v2) == s.end()) {
          s.insert(v2);
          Q.push(P(v2, u.second + 1));
        }
      }
    }
  }
}

int main() {
  while (cin >> n) {
    f = false;
    vector<int> v(n);
    s.clear();
    ans = -1;
    for (int i = 0; i < n; i++)
      cin >> v[i];
    s.insert(v);
    ansv = v;
    sort(ansv.begin(), ansv.end());

    if (ansv != v)
      func(v);
    else
      ans = 0;

    cout << ans << endl;
  }
}
Problem p01557 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;

string rev(const string &s, int i, int j) {
  string ret = s;
  for (int k = i; k <= j; ++k)
    ret[k] = s[j - k + i];
  return ret;
}

int main() {
  int N;
  cin >> N;
  string A(N, '0');
  for (int i = 0; i < N; ++i) {
    int t;
    cin >> t;
    A[i] = '0' + t - 1;
  }
  string C(N, '0');
  for (int i = 0; i < N; ++i)
    C[i] = '0' + i;
  if (A == C) {
    cout << 0 << endl;
    return 0;
  }
  queue<string> que;
  que.push(A);
  map<string, int> M;
  M[A] = 1;
  while (!que.empty()) {
    string s = que.front();
    que.pop();
    for (int i = 0; i < N; ++i) {
      for (int j = i + 1; j < N; ++j) {
        string t = rev(s, i, j);
        if (M[t] > 0)
          continue;
        M[t] = M[s] + 1;
        if (t == C) {
          cout << M[t] - 1 << endl;
          return 0;
        }
        if (M[t] < N / 2) {
          que.push(t);
        }
      }
    }
  }
  map<string, int> M_;
  M_[C] = 1;
  que.push(C);
  while (!que.empty()) {
    string s = que.front();
    que.pop();
    for (int i = 0; i < N; ++i) {
      for (int j = i + 1; j < N; ++j) {
        string t = rev(s, i, j);
        if (M_[t] > 0)
          continue;
        M_[t] = M_[s] + 1;
        if (M[t] > 0) {
          cout << M[t] + M_[t] - 2 << endl;
          return 0;
        }
        if (M_[t] <= N / 2)
          que.push(t);
      }
    }
  }
  cout << N - 1 << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;

string rev(const string &s, int i, int j) {
  string ret = s;
  for (int k = i; k <= j; ++k)
    ret[k] = s[j - k + i];
  return ret;
}

int main() {
  int N;
  cin >> N;
  string A(N, '0');
  for (int i = 0; i < N; ++i) {
    int t;
    cin >> t;
    A[i] = '0' + t - 1;
  }
  string C(N, '0');
  for (int i = 0; i < N; ++i)
    C[i] = '0' + i;
  if (A == C) {
    cout << 0 << endl;
    return 0;
  }
  queue<string> que;
  que.push(A);
  map<string, int> M;
  M[A] = 1;
  while (!que.empty()) {
    string s = que.front();
    que.pop();
    for (int i = 0; i < N; ++i) {
      for (int j = i + 1; j < N; ++j) {
        string t = rev(s, i, j);
        if (M[t] > 0)
          continue;
        M[t] = M[s] + 1;
        if (t == C) {
          cout << M[t] - 1 << endl;
          return 0;
        }
        if (M[t] <= N / 2) {
          que.push(t);
        }
      }
    }
  }
  map<string, int> M_;
  M_[C] = 1;
  que.push(C);
  while (!que.empty()) {
    string s = que.front();
    que.pop();
    for (int i = 0; i < N; ++i) {
      for (int j = i + 1; j < N; ++j) {
        string t = rev(s, i, j);
        if (M_[t] > 0)
          continue;
        M_[t] = M_[s] + 1;
        if (M[t] > 0) {
          cout << M[t] + M_[t] - 2 << endl;
          return 0;
        }
      }
    }
  }
  cout << N - 1 << endl;
  return 0;
}
Problem p01557 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

using namespace std;
typedef long long ll;

struct S {
  int n;
  char d[10];
  S() {}
  S(int n) : n(n) {}
  ll hash() {
    ll r = 0;
    for (int i = 0; i < n; i++) {
      r *= 16;
      r += d[i];
    }
    return r;
  }
};

typedef pair<S, int> P;

int main() {
  int n;
  cin >> n;
  S s(n), r(n);
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    x--;
    s.d[i] = x;
    r.d[i] = i;
  }
  ll rh = r.hash();
  int dist = 1, rdist = 1;
  queue<P> q;
  map<ll, int> u;
  q.push(P(s, 1));
  while (!q.empty()) {
    S s(0);
    int d;
    tie(s, d) = q.front();
    q.pop();
    if (d == 6)
      break;

    ll sh = s.hash();
    if (u[sh])
      continue;
    u[sh] = d;
    dist = d;
    if (sh == rh) {
      break;
    }
    for (int i = 0; i < n; i++) {
      for (int j = i + 2; j <= n; j++) {
        S ss = s;
        reverse(ss.d + i, ss.d + j);
        q.push(P(ss, d + 1));
      }
    }
  }
  map<ll, int> ru;
  q = queue<P>();
  q.push(P(r, 1));
  while (!q.empty()) {
    S s(0);
    int d;
    tie(s, d) = q.front();
    q.pop();
    ll sh = s.hash();
    if (d == 6) {
      rdist = 6;
      break;
    }
    if (ru[sh])
      continue;
    ru[sh] = d;
    rdist = d;
    if (u[sh]) {
      break;
    }
    for (int i = 0; i < n; i++) {
      for (int j = i + 2; j <= n; j++) {
        S ss = s;
        reverse(ss.d + i, ss.d + j);
        q.push(P(ss, d + 1));
      }
    }
  }
  cout << dist + rdist - 2 << endl;
  return 0;
}

Error: MLE

Fixed submission:

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

using namespace std;
typedef long long ll;

struct S {
  int n;
  int d[10];
  S() {}
  S(int n) : n(n) {}
  ll hash() {
    ll r = 0;
    for (int i = 0; i < n; i++) {
      r *= 16;
      r += d[i];
    }
    return r;
  }
};

typedef pair<S, int> P;

int main() {
  int n;
  cin >> n;
  S s(n), r(n);
  for (int i = 0; i < n; i++) {
    cin >> s.d[i];
    s.d[i]--;
    r.d[i] = i;
  }
  ll rh = r.hash();
  int dist = 1, rdist = 1;
  queue<P> q;
  map<ll, int> u;
  q.push(P(s, 1));
  while (!q.empty()) {
    S s(0);
    int d;
    tie(s, d) = q.front();
    q.pop();
    if (d == 6)
      break;

    ll sh = s.hash();
    if (u[sh])
      continue;
    u[sh] = d;
    dist = d;
    if (sh == rh) {
      break;
    }
    for (int i = 0; i < n; i++) {
      for (int j = i + 2; j <= n; j++) {
        S ss = s;
        reverse(ss.d + i, ss.d + j);
        q.push(P(ss, d + 1));
      }
    }
  }
  map<ll, int> ru;
  q = queue<P>();
  q.push(P(r, 1));
  while (!q.empty()) {
    S s(0);
    int d;
    tie(s, d) = q.front();
    q.pop();
    ll sh = s.hash();
    if (ru[sh])
      continue;
    ru[sh] = d;
    rdist = d;
    if (u[sh]) {
      break;
    }
    for (int i = 0; i < n; i++) {
      for (int j = i + 2; j <= n; j++) {
        S ss = s;
        reverse(ss.d + i, ss.d + j);
        q.push(P(ss, d + 1));
      }
    }
  }
  cout << dist + rdist - 2 << endl;
  return 0;
}