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

#include <cstdio>
#include <vector>

#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1 << 29;
const double EPS = 1e-9;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
vector<int> ball;
vector<int> tmp;
int n;
int ans;
void check() {
  int cnt = 0;
  int first;
  bool flag = false;
  for (int i = 0; i < ball.size();) {
    if (ball[i] == ball[i + 1]) { // 同じ物が続く
      cnt++;
      if (!flag) {
        first = i;
        flag = true;
      }
      i++;
      continue;
    } else if (cnt >= 3) { // 四連続以上
      int lenght = first + cnt + 1;
      vector<int>::iterator it =
          ball.erase(ball.begin() + first, ball.begin() + lenght);
      flag = false;
      cnt = 0;
      i = 0;
      continue;
    }
    flag = false;
    cnt = 0;
    i++;
  }
  int sp = ball.size();
  /*cout <<"change = "<<n<<endl;
  rep(i,ball.size()){
          cout <<"ball = "<<ball[i]<<endl;
  }
  cout <<endl;
   */
  ans = min(ans, sp);
}

int main() {
  while (1) {
    scanf("%d", &n);
    if (n == 0)
      break;
    ball.clear();
    ans = INF;
    int x;
    rep(i, n) {
      scanf("%d", &x);
      ball.push_back(x);
    }
    tmp = ball;
    for (int i = 0; i < n; i++) {
      if (ball[i] == 1) {
        ball[i] = 2;
        check();
        ball = tmp;
        ball[i] = 3;
        check();
        ball = tmp;
      } else if (ball[i] == 2) {
        ball[i] = 1;
        check();
        ball = tmp;
        ball[i] = 3;
        check();
        ball = tmp;
      } else if (ball[i] == 3) {
        ball[i] = 2;
        check();
        ball = tmp;
        ball[i] = 1;
        check();
        ball = tmp;
      }
    }
    printf("%d\\n", ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1 << 29;
const double EPS = 1e-9;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
vector<int> ball;
vector<int> tmp;
int n;
int ans;
void check(int n) {
  int cnt = 0;
  int first;
  bool flag = false;
  for (int i = 0; i < ball.size();) {
    if (ball[i] == ball[i + 1]) { // 同じ物が続く
      cnt++;
      if (!flag) {
        first = i;
        flag = true;
      }
      i++;
      continue;
    } else if (cnt >= 3) { // 四連続以上
      vector<int>::iterator it =
          ball.erase(ball.begin() + first, ball.begin() + first + cnt + 1);
      flag = false;
      cnt = 0;
      i = 0;
      continue;
    }
    flag = false;
    cnt = 0;
    i++;
  }
  int sp = ball.size();
  /*cout <<"change = "<<n<<endl;
  rep(i,ball.size()){
          cout <<"ball = "<<ball[i]<<endl;
  }
  cout <<endl;
   */
  ans = min(ans, sp);
}

int main() {
  while (cin >> n, n) {
    ball.clear();
    tmp.clear();
    ans = INF;
    int x;
    rep(i, n) {
      scanf("%d", &x);
      ball.push_back(x);
    }
    tmp = ball;
    for (int i = 0; i < n; i++) {
      if (ball[i] == 1) {
        ball[i] = 2;
        check(i);
        ball = tmp;
        ball[i] = 3;
        check(i);
        ball = tmp;
      } else if (ball[i] == 2) {
        ball[i] = 1;
        check(i);
        ball = tmp;
        ball[i] = 3;
        check(i);
        ball = tmp;
      } else if (ball[i] == 3) {
        ball[i] = 2;
        check(i);
        ball = tmp;
        ball[i] = 1;
        check(i);
        ball = tmp;
      }
    }
    printf("%d\\n", ans);
  }
  return 0;
}