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

#include <cstdio>
#include <stack>
#include <utility>
#include <vector>

using namespace std;

void count(stack<int> *s) {
  int find;
  int times = 0;

  if (s->empty())
    return;

  find = s->top();

  while (!s->empty() && find == s->top()) {
    times++;
    s->pop();
  }

  if (times >= 4)
    return;

  for (; times; times--)
    s->push(find);
}

int main() {
  while (1) {
    int i;
    int n;
    vector<int> characters;
    vector<pair<int, int>> var;
    int d[4];
    int save;
    int min_size;

    scanf("%d", &n);
    if (n == 0)
      return 0;

    min_size = n;

    for (i = 0; i < 4; i++)
      d[i] = -1 - i;

    for (i = 0; i < n; i++) {
      int j;
      int k;

      scanf("%d", &k);
      k--;

      characters.push_back(k);

      for (j = 0; j < 3; j++) {
        d[j] = d[j + 1];
      }

      d[3] = k;

      if (d[0] == d[1] && d[1] == d[2]) {
        if (var.empty() || var.back() != make_pair(i, d[0]))
          var.push_back(make_pair(i, d[0]));
      }

      if (d[0] == d[1] && d[1] == d[3]) {
        if (var.empty() || var.back() != make_pair(i - 1, d[1]))
          var.push_back(make_pair(i - 1, d[1]));
      }

      if (d[0] == d[2] && d[2] == d[3]) {
        if (var.empty() || var.back() != make_pair(i - 2, d[2]))
          var.push_back(make_pair(i - 2, d[2]));
      }

      if (d[1] == d[2] && d[2] == d[3]) {
        if (var.empty() || var.back() != make_pair(i - 3, d[3]))
          var.push_back(make_pair(i - 3, d[3]));
      }
    }

    for (i = 0; i < var.size(); i++) {
      int j;
      stack<int> process;
      int now_color = -1;

      save = characters[var[i].first];
      characters[var[i].first] = var[i].second;

      for (j = 0; j < characters.size(); j++) {
        if (characters[j] != now_color) {
          count(&process);
          now_color = characters[j];
        }

        process.push(characters[j]);
      }

      count(&process);

      if (min_size > process.size())
        min_size = process.size();
      characters[var[i].first] = save;
    }

    printf("%d\\n", min_size);
  }
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <stack>
#include <utility>
#include <vector>

using namespace std;

int count(stack<int> s) {
  stack<int> tmp;
  int find;
  int times = 0;

  if (s.empty())
    return 0;

  find = s.top();

  while (!s.empty() && s.top() == find) {
    tmp.push(s.top());
    times++;
    s.pop();
  }

  return times;
}

int main() {
  while (1) {
    int i;
    int n;
    vector<int> characters;
    vector<pair<int, int>> var;
    int d[4];
    int color[3];
    int save;
    int min_size;

    scanf("%d", &n);
    if (n == 0)
      return 0;

    min_size = n;

    for (i = 0; i < 3; i++)
      color[i] = 0;
    for (i = 0; i < 4; i++)
      d[i] = -1 - i;

    for (i = 0; i < n; i++) {
      int j;
      int k;

      scanf("%d", &k);
      k--;

      characters.push_back(k);

      color[k]++;
      if (d[0] >= 0)
        color[d[0]]--;

      for (j = 0; j < 3; j++) {
        d[j] = d[j + 1];
      }

      d[3] = k;

      for (j = 0; j < 3; j++) {
        if (color[j] == 3) {
          int l;
          for (l = 0; l < 4; l++) {
            if (d[l] != j) {
              if (var.empty() || var.back() != make_pair(i + l - 3, j))
                var.push_back(make_pair(i + l - 3, j));
            }
          }
        }
      }
    }

    for (i = 0; i < var.size(); i++) {
      int j;
      stack<int> process;
      int now_color = -1;

      save = characters[var[i].first];
      characters[var[i].first] = var[i].second;

      for (j = 0; j < characters.size(); j++) {
        int times;

        if (characters[j] != now_color) {
          times = count(process);
          if (times >= 4) {
            for (; times; times--)
              process.pop();
          }

          now_color = characters[j];
        }

        process.push(characters[j]);
      }

      if (min_size > process.size())
        min_size = process.size();
      characters[var[i].first] = save;
    }

    printf("%d\\n", min_size);
  }
}