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

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define REP(i, k, n) for (int i = k; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1 << 30
#define pb push_back
#define mp make_pair

using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int func(vector<int> &v) {
  stack<P> st;
  int n = v.size(), ret = 0;

  rep(i, n) {
    int len = 1, c = v[i];
    REP(j, i + 1, n) {
      if (c == v[j]) {
        i++;
        len++;
        continue;
      } else
        break;
    }

    if (len >= 4)
      continue;
    else {
      if (st.size() == 0) {
        st.push(mp(c, len));
        ret += len;
      } else {
        P p = st.top();
        if (p.first == c) {
          if (p.second + len < 4) {
            st.top().second += len;
            ret += len;
          } else {
            st.pop();
            ret -= p.second;
          }
        } else {
          st.push(mp(c, len));
          ret += len;
        }
      }
    }

    // prints(st);
  }

  // int ret = 0;
  // while(st.size()) {
  // 	ret += st.top().second;
  // 	st.pop();
  // }
  return ret;
}

int main() {
  int n;
  while (cin >> n && n) {

    vector<int> v(n);
    rep(i, n) cin >> v[i];

    int ans = n + 1;
    rep(i, n) {
      int t = v[i];

      v[i] = 1;
      if (i == 0) {
        v[i] = v[i + 1];
        ans = min(ans, func(v));
      } else if (i == n - 1) {
        v[i] = v[i - 1];
        ans = min(ans, func(v));
      } else {
        if (v[i + 1] == v[i - 1]) {
          v[i] = v[i + 1];
          ans = min(ans, func(v));
        } else {
          v[i] = v[i - 1];
          ans = min(ans, func(v));

          v[i] = v[i + 1];
          ans = min(ans, func(v));
        }
      }

      v[i] = t;
    }

    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define REP(i, k, n) for (int i = k; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1 << 30
#define pb push_back
#define mp make_pair

using namespace std;
typedef long long ll;
typedef pair<int, int> P;

bool used[10005];

void prints(stack<P> st) {
  while (st.size()) {
    P p = st.top();
    st.pop();
    cout << "(" << p.first << "," << p.second << ") ";
  }
  cout << endl;
}

int func(vector<int> &v) {
  stack<P> st;
  int n = v.size();

  rep(i, n) {
    int len = 1, c = v[i];
    REP(j, i + 1, n) {
      if (c == v[j]) {
        i++;
        len++;
        continue;
      } else
        break;
    }

    if (len >= 4)
      continue;
    else {
      if (st.size() == 0) {
        st.push(mp(c, len));
      } else {
        P p = st.top();
        if (p.first == c) {
          if (p.second + len < 4) {
            st.top().second += len;
          } else {
            st.pop();
          }
        } else {
          st.push(mp(c, len));
        }
      }
    }

    // prints(st);
  }

  int ret = 0;
  while (st.size()) {
    ret += st.top().second;
    st.pop();
  }
  return ret;
}

int main() {
  int n;
  while (cin >> n && n) {

    vector<int> v(n);
    rep(i, n) cin >> v[i];

    int ans = n + 1;
    rep(i, n) {
      int t = v[i];

      v[i] = 1;
      if (i == 0) {
        v[i] = v[i + 1];
        ans = min(ans, func(v));
      } else if (i == n - 1) {
        v[i] = v[i - 1];
        ans = min(ans, func(v));
      } else {
        if (v[i + 1] == v[i - 1]) {
          v[i] = v[i + 1];
          ans = min(ans, func(v));
        } else {
          v[i] = v[i - 1];
          ans = min(ans, func(v));

          v[i] = v[i + 1];
          ans = min(ans, func(v));
        }
      }

      v[i] = t;
    }

    cout << ans << endl;
  }

  return 0;
}