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

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <utility>
#include <vector>

using namespace std;

#define dump(n) cout << "# " << #n << "=" << (n) << endl
#define debug(n)                                                               \\
  cout << __FILE__ << "," << __LINE__ << ": #" << #n << "=" << (n) << endl
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define rep(i, n) repi(i, 0, n)
#define iter(c) __typeof((c).begin())
#define foreach(i, c) for (iter(c) i = (c).begin(); i != (c).end(); i++)
#define allof(c) (c).begin(), (c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;

int simulate(const vi &a, int changedpos) {
  int li = changedpos, ri = changedpos;
  for (;;) {
    int color = a[ri];
    int li2, ri2;
    for (li2 = li - 1;; li2--) {
      if (li2 < 0 || a[li2] != color) {
        li2++;
        break;
      }
    }
    for (ri2 = ri; ri2 < a.size() && a[ri2] == color; ri2++)
      ;
    if ((ri2 - li2) - (ri - li) >= 4) {
      li = li2;
      ri = ri2;
    } else
      break;
    if (li == 0 || ri == a.size() || a[li - 1] != a[ri])
      break;
  }
  return a.size() - (ri - li);
}

int main() {
  for (int n; cin >> n, n;) {
    vi a(n);
    rep(i, n) cin >> a[i];

    int res = 1 << 29;
    rep(i, n) {
      int temp = a[i];
      repi(j, 1, 4) {
        if (j == temp)
          continue;
        a[i] = j;
        res = min(res, simulate(a, i));
      }
      a[i] = temp;
    }

    cout << res << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <utility>
#include <vector>

using namespace std;

#define dump(n) cout << "# " << #n << "=" << (n) << endl
#define debug(n)                                                               \\
  cout << __FILE__ << "," << __LINE__ << ": #" << #n << "=" << (n) << endl
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define rep(i, n) repi(i, 0, n)
#define iter(c) __typeof((c).begin())
#define foreach(i, c) for (iter(c) i = (c).begin(); i != (c).end(); i++)
#define allof(c) (c).begin(), (c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;

int simulate(vi a) {
  list<int> l(allof(a));
  for (;;) {
    // foreach(i,l)
    //	cout<<*i<<" ";
    // cout<<endl;
    bool changed = false;
    for (iter(l) i = l.begin(); i != l.end();) {
      iter(l) j = find_if(i, l.end(), not1(bind2nd(equal_to<int>(), *i)));
      if (distance(i, j) >= 4) {
        i = l.erase(i, j);
        changed = true;
      } else
        i = j;
    }
    if (!changed)
      break;
  }
  return l.size();
}

int main() {
  for (int n; cin >> n, n;) {
    vi a(n);
    rep(i, n) cin >> a[i];

    int res = 1 << 29;
    rep(i, n) {
      int temp = a[i];
      repi(j, 1, 4) {
        if (j == temp)
          continue;
        a[i] = j;
        res = min(res, simulate(a));
      }
      a[i] = temp;
    }

    cout << res << endl;
  }

  return 0;
}