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

#define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;

static const double EPS = 1e-8;

static const int tx[] = {0, 1, 0, -1};
static const int ty[] = {-1, 0, 1, 0};

int compute_remaining_characters(const vector<int> &characters, int idx,
                                 int color) {
  int prev_lhs = idx;
  int prev_rhs = idx - 1;
  int lhs = idx;
  int rhs = idx;

  for (int round = 0; round <= characters.size(); round++) {
    color = characters[lhs];
    for (; lhs >= 0; lhs--) {
      if (characters[lhs] != color) {
        lhs++;
        break;
      }
    }
    for (; rhs < characters.size(); rhs++) {
      if (characters[rhs] != color) {
        rhs--;
        break;
      }
    }
    if (lhs < 0)
      lhs++;
    if (rhs >= characters.size())
      rhs--;

    if ((rhs - lhs + 1) - (prev_rhs - prev_lhs + 1) < 4) {
      break;
    }
    prev_lhs = lhs;
    prev_rhs = rhs;

    // next pos
    lhs--;
    rhs++;
  }
  return characters.size() - (prev_rhs - prev_lhs + 1);
}

int main() {
  int n;
  while (~scanf("%d", &n)) {
    if (n == 0)
      break;

    vector<int> characters;
    for (int i = 0; i < n; i++) {
      int color;
      scanf("%d", &color);
      characters.push_back(color);
    }

    int res = n;
    for (int idx = 0; idx < characters.size(); idx++) {
      for (int color = 1; color <= 3; color++) {
        vector<int> store = characters;
        characters[idx] = color;
        res = min(res, compute_remaining_characters(characters, idx, color));
        characters = store;
      }
    }
    printf("%d\\n", res);
  }
}

Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;

static const double EPS = 1e-8;

static const int tx[] = {0, 1, 0, -1};
static const int ty[] = {-1, 0, 1, 0};

class Block {
public:
  int color;
  int size;
  Block(int _c, int _s) : color(_c), size(_s) {}
};

int check_stage(deque<Block> &stage, int change_idx) {

  int res = 0;
  int max_round = stage.size();
  for (int round = 0; round < max_round; round++) {
    bool update = false;

    for (int i = change_idx - 3; i < change_idx + 3 && i + 1 < stage.size();
         i++) {
      if (i < 0)
        continue;
      if (stage[i].color == stage[i + 1].color) {
        Block b(stage[i].color, stage[i].size + stage[i + 1].size);
        stage.erase(stage.begin() + i, stage.begin() + i + 2);
        stage.insert(stage.begin() + i, b);
        update = true;
      }
    }

    res = 0;
    for (int i = 0; i < stage.size(); i++) {
      res += stage[i].size;
      if (stage[i].size >= 4) {
        stage.erase(stage.begin() + i);
        change_idx = i;
        update = true;
      }
    }
    if (!update)
      break;
  }
  return res;
}

int main() {
  int n;
  while (~scanf("%d", &n)) {
    if (n == 0)
      break;

    deque<Block> stage;
    queue<int> que;
    int prev = -1;
    for (int i = 0; i < n; i++) {
      int color;
      scanf("%d", &color);

      if (i == 0 || color == prev) {
        que.push(color);
      } else {
        stage.push_back(Block(prev, que.size()));
        while (!que.empty())
          que.pop();
        que.push(color);
      }
      prev = color;
    }

    if (!que.empty()) {
      stage.push_back(Block(prev, que.size()));
    }

    int res = n;

    deque<Block> tmp_stage1;
    deque<Block> tmp_stage2;

    for (int i = 0; i < stage.size(); i++) {
      for (int color = 1; color <= 3; color++) {
        if (stage[i].color == color)
          continue;

        tmp_stage1 = stage;
        tmp_stage2 = stage;
        int prev_color = stage[i].color;
        int prev_size = stage[i].size;

        tmp_stage1[i].color = color;
        tmp_stage1[i].size = 1;

        if (prev_size > 1) {
          Block b(prev_color, prev_size - 1);
          tmp_stage1.insert(tmp_stage1.begin() + i + 1, b);
          tmp_stage2.insert(tmp_stage2.begin() + i, b);
          res = min(res, check_stage(tmp_stage1, i));
          res = min(res, check_stage(tmp_stage2, i));
        } else {
          res = min(res, check_stage(tmp_stage1, i));
        }
      }
    }

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