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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

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

void f(vector<int> &x) {

  int r = x.size();
  if (r < 4)
    return;
  else {

    int p;
    bool b = false;
    REP(i, 0, r - 4) {
      if (x[i] == x[i + 1] && x[i] == x[i + 2] && x[i] == x[i + 3]) {
        p = i;
        b = true;
        break;
      }
    }

    if (b) {
      if ((x[p] != x[p + 4] && p + 4 < r) || p + 4 == r) {
        x.erase(x.begin() + p, x.begin() + p + 4);
        f(x);
      } else if (x[p] == x[p + 4] &&
                 ((x[p] != x[p + 5] && p + 5 < r) || p + 5 == r)) {
        x.erase(x.begin() + p, x.begin() + p + 5);
        f(x);
      } else if (x[p] == x[p + 4] && x[p] == x[p + 5] &&
                 ((x[p] != x[p + 6] && p + 6 < r) || p + 6 == r)) {
        x.erase(x.begin() + p, x.begin() + p + 6);
        f(x);
      } else if (x[p] == x[p + 4] && x[p] == x[p + 5] && x[p] == x[p + 6] &&
                 p + 6 < r) {
        x.erase(x.begin() + p, x.begin() + p + 7);
        f(x);
      }

    }

    else
      return;
  }
}

int main() {

  while (1) {

    int N, a;
    vector<int> s;

    cin >> N;
    if (N == 0)
      break;

    rep(i, N) {
      cin >> a;
      s.push_back(a);
    }

    if (N < 4) {
      cout << N << endl;
    } else {

      vector<pair<int, int>> v;

      REP(i, 0, N - 4) {
        if (s[i] != s[i + 1] && s[i + 1] == s[i + 2] && s[i + 1] == s[i + 3])
          v.push_back(make_pair(i, s[i + 1]));
      }

      REP(i, 3, N - 1) {
        if (s[i] != s[i - 1] && s[i - 1] == s[i - 2] && s[i - 1] == s[i - 3])
          v.push_back(make_pair(i, s[i - 1]));
      }

      REP(i, 1, N - 3) {
        if (s[i] != s[i - 1] && s[i - 1] == s[i + 1] && s[i - 1] == s[i + 2])
          v.push_back(make_pair(i, s[i - 1]));
      }

      REP(i, 2, N - 2) {
        if (s[i] != s[i - 1] && s[i - 1] == s[i - 2] && s[i - 1] == s[i + 1])
          v.push_back(make_pair(i, s[i - 1]));
      }

      sort(v.begin(), v.end());
      vector<pair<int, int>>::iterator it = unique(v.begin(), v.end());
      v.erase(it, v.end());

      vector<int> s1 = s;
      if (v.empty())
        cout << N << endl;
      else {
        int M = 10000;
        rep(i, v.size()) {
          s1[v[i].first] = v[i].second;
          f(s1);
          if (M > s1.size())
            M = s1.size();
          s1 = s;
        }

        cout << M << endl;
      }

      s.clear();
      v.clear();
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

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

vector<int> s;
void f(vector<int> &x) {

  int r = x.size();
  if (r < 4)
    return;
  else {

    int p;
    bool b = false;
    REP(i, 0, r - 4) {
      if (x[i] == x[i + 1] && x[i] == x[i + 2] && x[i] == x[i + 3]) {
        p = i;
        b = true;
        break;
      }
    }

    if (b) {
      if ((x[p] != x[p + 4] && p + 4 < r) || p + 4 == r) {
        x.erase(x.begin() + p, x.begin() + p + 4);
        f(x);
      } else if (x[p] == x[p + 4] &&
                 ((x[p] != x[p + 5] && p + 5 < r) || p + 5 == r)) {
        x.erase(x.begin() + p, x.begin() + p + 5);
        f(x);
      } else if (x[p] == x[p + 4] && x[p] == x[p + 5] &&
                 ((x[p] != x[p + 6] && p + 6 < r) || p + 6 == r)) {
        x.erase(x.begin() + p, x.begin() + p + 6);
        f(x);
      } else if (x[p] == x[p + 4] && x[p] == x[p + 5] && x[p] == x[p + 6] &&
                 p + 6 < r) {
        x.erase(x.begin() + p, x.begin() + p + 7);
        f(x);
      }

    }

    else
      return;
  }
}

int main() {

  while (1) {

    int N, a;

    cin >> N;
    if (N == 0)
      break;

    rep(i, N) {
      cin >> a;
      s.push_back(a);
    }

    if (N < 4) {
      cout << N << endl;
    } else {

      vector<int> s1 = s;
      int M = 10000;
      rep(i, N) {

        if ((s1[i] == 1 && s1[i + 1] != 1 && s1[i - 1] != 1 && i - 1 >= 0 &&
             i + 1 < N) ||
            s1[0] == 1 || s1[N - 1] == 1) {
          s1[i] = 2;
          f(s1);
          if (s1.size() < M)
            M = s1.size();
          s1 = s;

          s1[i] = 3;
          f(s1);
          if (s1.size() < M)
            M = s1.size();
          s1 = s;
        }

        if ((s1[i] == 2 && s1[i + 1] != 2 && s1[i - 1] != 2 && i - 1 >= 0 &&
             i + 1 < N) ||
            s1[0] == 2 || s1[N - 1] == 2) {
          s1[i] = 3;
          f(s1);
          if (s1.size() < M)
            M = s1.size();
          s1 = s;

          s1[i] = 1;
          f(s1);
          if (s1.size() < M)
            M = s1.size();
          s1 = s;
        }

        if ((s1[i] == 3 && s1[i + 1] != 3 && s1[i - 1] != 3 && i - 1 >= 0 &&
             i + 1 < N) ||
            s1[0] == 3 || s1[N - 1] == 3) {
          s1[i] = 2;
          f(s1);
          if (s1.size() < M)
            M = s1.size();
          s1 = s;

          s1[i] = 1;
          f(s1);
          if (s1.size() < M)
            M = s1.size();
          s1 = s;
        }
      }

      cout << M << endl;

      s.clear();
    }
  }

  return 0;
}