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

#include <algorithm>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
#define INF 100000000
using namespace std;
typedef pair<int, int> P;
int N, ans;
vector<int> vec, vec2, tmp;
void deal(int t) {
  int fir = 0;
  bool update = true;
  while (update) {
    update = false;
    fir = max(t - 6, 0);
    int color = 0;
    for (int i = max(t - 6, 0); i < min(t + 6, (int)tmp.size()); i++) {
      if (tmp[i] == color && i != tmp.size() - 1)
        continue;
      int count = i - fir;
      if (i == tmp.size() - 1)
        count++;
      if (count >= 4) {
        while (count--)
          tmp.erase(fir + tmp.begin());
        t = fir;
        update = true;
        break;
      } else {
        fir = i;
        color = tmp[i];
      }
    }
  }

  return;
}
int main() {
  while (cin >> N && N) {
    vec.clear();
    vec2.clear();
    ans = N;
    for (int i = 0; i < N; i++) {
      int tmp;
      cin >> tmp;
      vec.push_back(tmp);
    }
    for (int i = 0; i < N; i++) {
      bool flag = false;
      if (1 <= i && i < N - 1)
        if (vec[i - 1] == vec[i + 1] && vec[i - 1] != vec[i])
          flag = true;
      if (i < N - 3 && !flag)
        if (vec[i + 1] == vec[i + 2] && vec[i + 1] == vec[i + 3])
          flag = true;
      if (3 <= i && !flag)
        if (vec[i - 1] == vec[i - 2] && vec[i - 1] == vec[i - 3])
          flag = true;
      if (flag)
        vec2.push_back(i);
    }
    if (vec2.empty()) {
      cout << ans << endl;
      continue;
    }
    for (int i = 0; i < (int)vec2.size(); i++) {
      int m = vec2[i];
      if (m != 0) {
        tmp = vec;
        tmp[m] = vec[m - 1];
        deal(m);
        ans = min(ans, (int)tmp.size());
        if (m == N - 1)
          continue;
        if (vec[m - 1] != vec[m + 1]) {
          tmp = vec;
          tmp[m] = vec[m + 1];
          deal(m);
          ans = min(ans, (int)tmp.size());
        }
      } else {
        tmp = vec;
        tmp[m] = vec[m + 1];
        deal(m);
        ans = min(ans, (int)tmp.size());
      }
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
#define INF 100000000
using namespace std;
typedef pair<int, int> P;
int N, ans;
vector<int> vec, vec2, tmp;
void deal(int t) {
  int fir = 0;
  int las = 0;
  int color = 0;
  bool update = true;
  while (update) {
    update = false;
    for (int i = max(t - 4, 0); i < (int)tmp.size(); i++) {
      if (tmp[i] == color)
        continue;
      int count = i - fir;
      if (i - fir >= 4) {
        while (count--)
          tmp.erase(fir + tmp.begin());
        t = fir;
        update = true;
        break;
      } else {
        fir = i;
        color = tmp[i];
      }
    }
  }
}
int main() {
  while (cin >> N && N) {
    vec.clear();
    vec2.clear();
    ans = N;
    for (int i = 0; i < N; i++) {
      int tmp;
      cin >> tmp;
      vec.push_back(tmp);
    }
    for (int i = 1; i < (int)vec.size() - 1; i++) {
      if (vec[i - 1] == vec[i + 1] && vec[i - 1] != vec[i])
        vec2.push_back(i);
    }
    if (vec2.empty()) {
      cout << ans << endl;
      continue;
    }
    for (int i = 0; i < (int)vec2.size(); i++) {
      tmp = vec;
      int t = vec2[i];
      tmp[t] = tmp[t - 1];
      deal(t);
      ans = min(ans, (int)tmp.size());
    }
    cout << ans << endl;
  }
}