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

#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
int x[9][9];
int N, M, A[9], P[9], cx, pows[9][9];
int power2(int a, int b) {
  int res = 1;
  for (int i = 0; i < b; i++) {
    res *= a;
  }
  return res;
}
int main() {
  for (int i = 0; i < 9; i++) {
    for (int j = 0; j < 9; j++) {
      pows[i][j] = power2(i, j);
    }
  }
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    cin >> A[i];
    x[i + 1][A[i]] = 1;
  }
  for (int i = 1; i <= N; i++) {
    cx = i;
    for (int j = 1; j <= M; j++) {
      if (x[j][cx] == 1) {
        cx++;
      } else if (x[j][cx - 1] == 1) {
        cx--;
      }
    }
    P[i] = cx;
  }
  int Q = M, maxv, S, cy;
  int D[9], F[9], y[9][9];
  for (int i = 0; i < 9; i++)
    for (int j = 0; j < 9; j++)
      y[i][j] = 0;
  for (int i = 0; i < pows[M][M] * 3 / 4; i++) {
    if (i >= 1) {
      for (int j = 0; j < M; j++)
        y[D[j] + 1][A[j]] = 0;
    }
    for (int j = 0; j < M; j++) {
      D[j] = (i / pows[M][j]) % M;
      y[D[j] + 1][A[j]]++;
    }
    for (int j = 0; j < M; j++) {
      if (y[D[j] + 1][A[j]] >= 2 || y[D[j] + 1][A[j] + 1] >= 1)
        goto E;
    }
    for (int j = 1; j <= N; j++) {
      cy = j;
      for (int k = 1; k <= M; k++) {
        if (y[k][cy] == 1)
          cy++;
        else if (y[k][cy - 1] == 1)
          cy--;
      }
      F[j] = cy;
    }
    for (int j = 1; j <= N; j++) {
      if (P[j] != F[j])
        goto E;
    }
    maxv = 0;
    for (int j = 1; j <= M; j++) {
      S = (i / pows[M][j - 1]) % M;
      maxv = max(maxv, S + 1);
    }
    if (Q > maxv)
      Q = maxv;
  E:;
  }
  cout << Q << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
int x[9][9];
int N, M, A[9], P[9], cx, pows[9][9];
int power2(int a, int b) {
  int res = 1;
  for (int i = 0; i < b; i++) {
    res *= a;
  }
  return res;
}
int main() {
  for (int i = 0; i < 9; i++) {
    for (int j = 0; j < 9; j++) {
      pows[i][j] = power2(i, j);
    }
  }
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    cin >> A[i];
    x[i + 1][A[i]] = 1;
  }
  for (int i = 1; i <= N; i++) {
    cx = i;
    for (int j = 1; j <= M; j++) {
      if (x[j][cx] == 1) {
        cx++;
      } else if (x[j][cx - 1] == 1) {
        cx--;
      }
    }
    P[i] = cx;
  }
  int Q = M, maxv, S, cy;
  int D[9], F[9], y[9][9];
  for (int i = 0; i < 9; i++)
    for (int j = 0; j < 9; j++)
      y[i][j] = 0;
  for (int i = 0; i < pows[M][M] * 9 / 10; i++) {
    if (i >= 1) {
      for (int j = 0; j < M; j++)
        y[D[j] + 1][A[j]] = 0;
    }
    for (int j = 0; j < M; j++) {
      D[j] = (i / pows[M][j]) % M;
      y[D[j] + 1][A[j]]++;
    }
    for (int j = 0; j < M; j++) {
      if (y[D[j] + 1][A[j]] >= 2 || y[D[j] + 1][A[j] + 1] >= 1)
        goto E;
    }
    for (int j = 1; j <= N; j++) {
      cy = j;
      for (int k = 1; k <= M; k++) {
        if (y[k][cy] == 1)
          cy++;
        else if (y[k][cy - 1] == 1)
          cy--;
      }
      F[j] = cy;
    }
    for (int j = 1; j <= N; j++) {
      if (P[j] != F[j])
        goto E;
    }
    maxv = 0;
    for (int j = 1; j <= M; j++) {
      S = (i / pows[M][j - 1]) % M;
      maxv = max(maxv, S + 1);
    }
    if (Q > maxv)
      Q = maxv;
  E:;
  }
  cout << Q << endl;
  return 0;
}