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

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

// #define int long long
#define DBG 1
#define dump(o)                                                                \\
  if (DBG) {                                                                   \\
    cerr << #o << " " << (o) << " ";                                           \\
  }
#define dumpl(o)                                                               \\
  if (DBG) {                                                                   \\
    cerr << #o << " " << (o) << endl;                                          \\
  }
#define dumpc(o)                                                               \\
  if (DBG) {                                                                   \\
    cerr << #o;                                                                \\
    for (auto &e : (o))                                                        \\
      cerr << " " << e;                                                        \\
    cerr << endl;                                                              \\
  }
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(c) begin(c), end(c)
const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9 + 7);
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
int N;
bool inrange(int i) { return 0 <= i && i < N; }

signed main() {
  for (; cin >> N && N;) {
    vector<int> v(N);
    rep(i, 0, N) { cin >> v[i]; }
    int ans = INF;
    rep(i, 0, N) rep(j, 1, 4) {
      int tmp = N;
      int l = i - 1, r = i + 1;
      int color = j;
      int cnt = 1;
      while (true) {
        while (true) {
          if (!inrange(l))
            break;
          if (v[l] != color)
            break;
          l--, cnt++;
        }
        while (true) {
          if (!inrange(r))
            break;
          if (v[r] != color)
            break;
          r++, cnt++;
        }
        if (cnt >= 4) {
          tmp -= cnt;
          cnt = 0;
          if (inrange(l))
            color = v[l];
          else if (inrange(r))
            color = v[r];
          else
            break;
        } else
          break;
      }
      chmin(ans, tmp);
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

// #define int long long
#define DBG 1
#define dump(o)                                                                \\
  if (DBG) {                                                                   \\
    cerr << #o << " " << (o) << " ";                                           \\
  }
#define dumpl(o)                                                               \\
  if (DBG) {                                                                   \\
    cerr << #o << " " << (o) << endl;                                          \\
  }
#define dumpc(o)                                                               \\
  if (DBG) {                                                                   \\
    cerr << #o;                                                                \\
    for (auto &e : (o))                                                        \\
      cerr << " " << e;                                                        \\
    cerr << endl;                                                              \\
  }
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(c) begin(c), end(c)
const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9 + 7);
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

#define REMOVE(v, a) v.erase(remove(v.begin(), v.end(), a), v.end())
const int CHAIN = 4;
class Field {
public:
  vector<int> v;
  Field(const vector<int> &v) : v(v) {}
  bool clear() {
    bool cleared = false;
    vector<int> c(v.size(), 1);
    rep(i, 1, v.size()) {
      if (v[i - 1] == v[i])
        c[i] = c[i - 1] + 1;
    }
    int i = v.size() - 1;
    int cc = 0;
    while (i) {
      if (c[i] >= CHAIN)
        cc = c[i];
      if (cc) {
        v[i] = INF;
        cc--;
        cleared = true;
      }
      i--;
    }
    REMOVE(v, INF);
    return cleared;
  }
};

signed main() {
  for (int N; cin >> N && N;) {
    vector<int> v(N);
    rep(i, 0, N) { cin >> v[i]; }
    int ans = INF;
    rep(i, 0, N) rep(j, 1, 4) {
      vector<int> tmp = v;
      tmp[i] = j;
      Field F(tmp);
      while (F.clear())
        ;
      chmin(ans, (int)F.v.size());
    }
    cout << ans << endl;
  }
  return 0;
}