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

#include <bits/stdc++.h>
using namespace std;
#define int long long
int n;
int po[11], a[11];
void print(int x) {
  for (int i = 0; i < n; i++)
    cout << x % 11 << " ", x /= 11;
  cout << endl;
}
void sw(int &x, int i, int j) {
  int s = (x % po[i + 1]) / po[i] * (po[j] - po[i]);
  int t = (x % po[j + 1]) / po[j] * (po[i] - po[j]);
  x += s + t;
}
signed main() {
  po[0] = 1;
  for (int i = 0; i < 10; i++)
    po[i + 1] = po[i] * 11;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int p[2] = {};
  for (int i = 0; i < n; i++)
    p[0] = p[0] * 11 + a[i];
  for (int i = 0; i < n; i++)
    p[1] = p[1] * 11 + (i + 1);
  if (p[0] == p[1]) {
    cout << 0 << endl;
    return 0;
  }
  // cout<<p<<endl<<ans<<endl;
  // print(p);print(ans);
  queue<int> q[2];
  unordered_map<int, int> m[2];
  int ans = n - 1;
  for (int j = 0; j < 2; j++) {
    q[j].push(p[j]);
    m[j][p[j]] = 0;
    while (!q[j].empty()) {
      int t = q[j].front();
      q[j].pop();
      int d = m[j][t];
      if (d >= (n - 1) / 2)
        break;
      // cout<<d<<":";print(p);
      for (int i = 1; i < n - 1; i++) {
        int r = t;
        for (int k = 1; i - k >= 0 && i + k < n; k++) {
          sw(r, i - k, i + k);
          if (!m[j].count(r)) {
            q[j].push(r);
            m[j][r] = d + 1;
            if (j && m[0].count(r))
              ans = min(ans, m[0][r] + m[1][r]);
          }
        }
      }
      for (int i = 0; i < n - 1; i++) {
        int r = t;
        for (int k = 0; i - k >= 0 && i + 1 + k < n; k++) {
          sw(r, i - k, i + 1 + k);
          if (!m[j].count(r)) {
            q[j].push(r);
            m[j][r] = d + 1;
            if (j && m[0].count(r))
              ans = min(ans, m[0][r] + m[1][r]);
          }
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
int n;
int po[11], a[11];
void print(int x) {
  for (int i = 0; i < n; i++)
    cout << x % 11 << " ", x /= 11;
  cout << endl;
}
void sw(int &x, int i, int j) {
  int s = (x % po[i + 1]) / po[i] * (po[j] - po[i]);
  int t = (x % po[j + 1]) / po[j] * (po[i] - po[j]);
  x += s + t;
}
signed main() {
  po[0] = 1;
  for (int i = 0; i < 10; i++)
    po[i + 1] = po[i] * 11;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int p[2] = {};
  for (int i = 0; i < n; i++)
    p[0] = p[0] * 11 + a[i];
  for (int i = 0; i < n; i++)
    p[1] = p[1] * 11 + (i + 1);
  if (p[0] == p[1]) {
    cout << 0 << endl;
    return 0;
  }
  // cout<<p<<endl<<ans<<endl;
  // print(p);print(ans);
  queue<int> q[2];
  unordered_map<int, int> m[2];
  int ans = n - 1;
  for (int j = 0; j < 2; j++) {
    q[j].push(p[j]);
    m[j][p[j]] = 0;
    while (!q[j].empty()) {
      int t = q[j].front();
      q[j].pop();
      int d = m[j][t];
      if (d >= n / 2)
        break;
      // cout<<d<<":";print(p);
      for (int i = 1; i < n - 1; i++) {
        int r = t;
        for (int k = 1; i - k >= 0 && i + k < n; k++) {
          sw(r, i - k, i + k);
          if (!m[j].count(r)) {
            q[j].push(r);
            m[j][r] = d + 1;
            if (j && m[0].count(r))
              ans = min(ans, m[0][r] + m[1][r]);
          }
        }
      }
      for (int i = 0; i < n - 1; i++) {
        int r = t;
        for (int k = 0; i - k >= 0 && i + 1 + k < n; k++) {
          sw(r, i - k, i + 1 + k);
          if (!m[j].count(r)) {
            q[j].push(r);
            m[j][r] = d + 1;
            if (j && m[0].count(r))
              ans = min(ans, m[0][r] + m[1][r]);
          }
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}