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

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

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define print(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define print(x)
#endif

const int inf = 1e9;
const int64_t inf64 = 1e18;
const double eps = 1e-9;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

void solve() {
  int n;
  cin >> n;
  int64_t s = 0;
  rep(i, 0, n) {
    int64_t a;
    cin >> a;
    --a;
    s = s * 10 + a;
  }

  int64_t sorted = 0;
  rep(i, 0, n) sorted = sorted * 10 + i;

  int64_t exp[10];
  exp[0] = 1;
  rep(i, 1, 10) exp[i] = exp[i - 1] * 10;
  auto reverse = [&](int64_t x, int l, int r) {
    rep(i, 0, (r - l + 1) / 2) {
      int64_t p = x / exp[l + i] % 10, q = x / exp[r - i] % 10;
      x -= p * exp[l + i] + q * exp[r - i];
      x += q * exp[l + i] + p * exp[r - i];
    }
    return x;
  };

  unordered_map<int64_t, int> num1;
  {
    queue<int64_t> queues[5];
    num1[s] = 0;
    queues[0].push(s);
    rep(k, 0, 5) {
      auto &que = queues[k];
      while (!que.empty()) {
        auto x = que.front();
        que.pop();
        if (x == sorted) {
          cout << k << endl;
          return;
        }
        int l, r;
        rep(i, 0, n) {
          if (x / exp[i] % 10 != n - i - 1) {
            l = i;
            break;
          }
        }
        for (int i = n - 1; i >= 0; --i) {
          if (x / exp[i] % 10 != n - i - 1) {
            r = i;
            break;
          }
        }
        rep(i, l, r) {
          rep(j, i + 1, r + 1) {
            int64_t y = reverse(x, i, j);
            if (num1.find(y) != num1.end())
              continue;
            if (y == sorted) {
              cout << k + 1 << endl;
              return;
            }
            num1[y] = k + 1;
            if (k + 1 < 5)
              queues[k + 1].push(y);
          }
        }
      }
    }
  }

  unordered_map<int64_t, int> num2;
  {
    queue<int64_t> queues[6];
    num2[sorted] = 0;
    queues[0].push(sorted);
    rep(k, 0, 6) {
      auto &que = queues[k];
      while (!que.empty()) {
        auto x = que.front();
        que.pop();
        rep(i, 0, n) {
          rep(j, i + 1, n) {
            int64_t y = reverse(x, i, j);
            if (num2.find(y) != num2.end())
              continue;
            if (num1.find(y) != num1.end()) {
              cout << num1[y] + k + 1 << endl;
              return;
            }
            num2[y] = k + 1;
            if (k + 1 < 6)
              queues[k + 1].push(y);
          }
        }
      }
    }
  }
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  cout.setf(ios::fixed);
  cout.precision(10);
  solve();
  return 0;
}

Error: TLE

Fixed submission:

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

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define print(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define print(x)
#endif

const int inf = 1e9;
const int64_t inf64 = 1e18;
const double eps = 1e-9;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

void solve() {
  int n;
  cin >> n;
  int64_t s = 0;
  rep(i, 0, n) {
    int64_t a;
    cin >> a;
    --a;
    s = s * 10 + a;
  }

  int64_t sorted = 0;
  rep(i, 0, n) sorted = sorted * 10 + i;

  int64_t exp[10];
  exp[0] = 1;
  rep(i, 1, 10) exp[i] = exp[i - 1] * 10;
  auto reverse = [&](int64_t x, int l, int r) {
    rep(i, 0, (r - l + 1) / 2) {
      int64_t p = x / exp[l + i] % 10, q = x / exp[r - i] % 10;
      x -= p * exp[l + i] + q * exp[r - i];
      x += q * exp[l + i] + p * exp[r - i];
    }
    return x;
  };

  unordered_map<int64_t, bool> done;
  queue<int64_t> queues[10];
  done[s] = true;
  queues[0].push(s);
  rep(k, 0, 10) {
    print(k);
    auto &que = queues[k];
    while (!que.empty()) {
      auto x = que.front();
      que.pop();
      if (x == sorted) {
        cout << k << endl;
        return;
      }
      if (k == 9)
        continue;
      rep(i, 0, n) {
        rep(j, i + 1, n) {
          int64_t y = reverse(x, i, j);
          if (done[y])
            continue;
          done[y] = true;
          queues[k + 1].push(y);
        }
      }
    }
  }
  cout << 10 << endl;
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  cout.setf(ios::fixed);
  cout.precision(10);
  solve();
  return 0;
}