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

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < j; i++)
#define FOR(i, j, k) for (int i = j; i < k; i++)
#define P pair<int, int>
const int INF = INT_MAX / 2;

int n;

vector<P> makeVecP(vector<int> v) {
  vector<P> ret;
  int i = 0, j = 0;
  while (i < n) {
    while (j < n && v[i] == v[j])
      j++;
    ret.push_back(P(v[i], j - i));
    i = j;
  }
  return ret;
}

int cntBall(vector<P> v, int left, int right) {
  int cnt = 0;
  while (left >= 0 && right < v.size() && v[left].first == v[right].first &&
         v[left].second + v[right].second >= 4) {
    cnt +=
        ((left == right) ? v[left].second : (v[left].second + v[right].second));
    left--;
    right++;
    if (v[left].second == 0)
      left--;
    if (v[right].second == 0)
      right++;
  }
  return n - cnt;
}

int main() {
  while (cin >> n && n) {
    vector<int> tmp(n);
    REP(i, n) cin >> tmp[i];
    vector<P> v = makeVecP(tmp);

    int ans = n;
    REP(i, v.size()) {
      if (v[i].second >= 3) {
        v[i].second++;
        if (i - 1 >= 0) {
          v[i - 1].second--;
          ans = min(ans, cntBall(v, i, i));
          v[i - 1].second++;
        }
        if (i + 1 < v.size()) {
          v[i + 1].second--;
          ans = min(ans, cntBall(v, i, i));
          v[i + 1].second++;
        }
        v[i].second--;
      } else if (i - 1 >= 0 && i + 1 < v.size() &&
                 v[i - 1].first == v[i + 1].first &&
                 v[i - 1].second + v[i + 1].second >= 3) {
        v[i].second--;
        v[i - 1].second++;
        ans = min(ans, cntBall(v, i - 1, i + 1));
        v[i - 1].second--;
        v[i].second++;
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < j; i++)
#define FOR(i, j, k) for (int i = j; i < k; i++)
#define P pair<int, int>
const int INF = INT_MAX / 2;
const int N = 10000;

int n;

bool isOK(int left, int right) { return true; }

int check(vector<int> c, int left, int right) {
  int ret = 0;
  while (left >= 0 || right < n) {
    // cout <<"! " <<left <<", " <<right <<endl;
    if (c[left] != c[right] && left >= 0 && right < n) {
      ret += 2;
      left--;
      right++;
      continue;
    }

    int cnt = 0;
    int fir_c = c[left];
    while (c[left] == fir_c && left >= 0) {
      left--;
      cnt++;
    };
    while (c[right] == fir_c && right < n) {
      right++;
      cnt++;
    };
    // cout <<"cnt = " <<cnt <<endl;
    if (cnt < 4)
      ret += cnt;
    // cout <<"@ " <<cnt <<", left = " <<left <<", right = " <<right <<endl;
  }
  return ret;
}

int main() {
  while (cin >> n && n) {
    vector<int> c(n);
    REP(i, n) cin >> c[i];
    int ans = n;

    REP(i, n) {
      FOR(j, 1, 4) {
        if (c[i] == j)
          continue;
        int tmp = c[i];
        c[i] = j;

        // debug
        // REP(z, n) cout <<c[z] <<", "; cout <<endl;

        int left = i, right = i;
        while (c[left] == c[i] && left >= 0)
          left--;
        while (c[right] == c[i] && right < n)
          right++;
        // debug
        // cout <<"now = " <<i <<", left = " <<left <<", right = " <<right
        // <<endl;
        if (right - left - 1 >= 4)
          ans = min(ans, check(c, left, right));
        // debug
        // cout <<ans <<endl <<endl;
        c[i] = tmp;
      }
    }
    cout << ans << endl;
  }
  return 0;
}