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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
const double EPS = 1e-10;
const int inf = 1e8;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
int n, m, out;
vi test, co;
int in[10][10] = {0};
void f(int a) {
  // rep(i,10){rep(j,10)cout<<" "<<in[i][j];cout<<endl;}cout<<endl;
  if (a == 0) {
    vi tes(n);
    rep(i, n) tes[i] = i;
    rep(i, 8) {
      rep(j, n) if (in[i][j]) { swap(tes[j], tes[j + 1]); }
    }
    //    rep(i,10){rep(j,10)cout<<" "<<in[i][j];cout<<endl;}cout<<endl;
    bool h = true;
    rep(i, n) if (tes[i] != test[i]) h = false;
    if (h) {
      int ma = 0;
      rep(i, 8) rep(j, n) if (in[i][j]) ma = max(ma, i);
      out = min(out, ma);
    }
  } else {
    rep(i, n - 1) if (co[i]) {
      co[i]--;
      rep(j, 8) {
        if (in[j][i])
          continue;
        if (i && in[j][i - 1])
          continue;
        in[j][i] = true;
        f(a - 1);
        in[j][i] = false;
      }
      co[i]++;
      break;
    }
  }
}
int main() {
  out = inf;
  cin >> n >> m;
  test = co = vi(n, 0);
  rep(i, n) test[i] = i;
  rep(i, m) {
    int a;
    cin >> a;
    a--;
    swap(test[a], test[a + 1]);
    co[a]++;
  }
  f(m);
  cout << out + 1 << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
const double EPS = 1e-10;
const int inf = 1e8;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
int n, m, out;
vi test, co;
int in[10][10] = {0};
void f(int a) {
  // rep(i,10){rep(j,10)cout<<" "<<in[i][j];cout<<endl;}cout<<endl;
  if (a == 0) {
    vi tes(10);
    rep(i, n) tes[i] = i;
    rep(i, 10) {
      rep(j, 9) if (in[i][j]) { swap(tes[j], tes[j + 1]); }
    }
    //    rep(i,10){rep(j,10)cout<<" "<<in[i][j];cout<<endl;}cout<<endl;
    bool h = true;
    rep(i, n) if (tes[i] != test[i]) h = false;
    if (h) {
      int ma = 0;
      rep(i, 10) rep(j, 10) if (in[i][j]) ma = max(ma, i);
      out = min(out, ma);
    }
  } else {
    rep(i, n - 1) if (co[i]) {
      co[i]--;
      rep(j, 10) {
        if (in[j][i])
          continue;
        if (i && in[j][i - 1])
          continue;
        in[j][i] = true;
        f(a - 1);
        in[j][i] = false;
      }
      co[i]++;
      break;
    }
  }
}
int main() {
  out = inf;
  cin >> n >> m;
  test = co = vi(10, 0);
  rep(i, n) test[i] = i;
  rep(i, m) {
    int a;
    cin >> a;
    a--;
    swap(test[a], test[a + 1]);
    co[a]++;
  }
  f(m);
  cout << out + 1 << endl;
}