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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <vector>

using namespace std;

int main() {
  int n = 0;
  int color[][2] = {{0, 0}, {2, 3}, {1, 3}, {1, 2}};
  while (cin >> n && n != 0) {
    vector<int> line;
    for (int i = 0; i < n; i++) {
      int tmp = 0;
      cin >> tmp;
      line.push_back(tmp);
    }

    int minch = n;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 2; j++) {
        vector<int> copyline = line;
        if (i == 0) {
          if (color[line[i]][j] != line[i + 1])
            continue;
        } else if (i == n - 1) {
          if (color[line[i]][j] != line[i - 1])
            continue;
        } else if (color[line[i]][j] != line[i + 1] &&
                   color[line[i]][j] != line[i - 1])
          continue;
        copyline[i] = color[line[i]][j];
        int count = 1;
        for (int k = 0; k < n - 1; k++) {
          if (!copyline[k])
            continue;
          int progress = 1;
          while (!copyline[k + progress] && k + progress < copyline.size())
            progress++;
          if (copyline[k] != copyline[k + progress]) {
            if (count >= 4) {
              int target = copyline[k];
              for (int l = k; copyline[l] == 0 || copyline[l] == target; l--) {
                copyline[l] = 0;
                if (!l)
                  break;
              }
              k = -1;
              count = 1;
            } else
              count = 1;
          } else
            count++;
        }

        if (count >= 4) {
          int target = copyline[n - 1];
          for (int l = n - 1; copyline[l] == 0 || copyline[l] == target; l--) {
            copyline[l] = 0;
            if (!l)
              break;
          }
        }

        int chnum = 0;
        for (int k = 0; k < n; k++) {
          if (copyline[k])
            chnum++;
        }

        minch = minch > chnum ? chnum : minch;
      }
    }

    cout << minch << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <vector>

using namespace std;

int main() {
  int n = 0;
  int color[][2] = {{0, 0}, {2, 3}, {1, 3}, {1, 2}};
  while (cin >> n && n != 0) {
    vector<int> line;
    for (int i = 0; i < n; i++) {
      int tmp = 0;
      cin >> tmp;
      line.push_back(tmp);
    }

    int minch = 1000000;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 2; j++) {
        vector<int> copyline = line;
        copyline[i] = color[line[i]][j];
        int count = 1;
        for (int k = 0; k < n - 1; k++) {
          if (!copyline[k])
            continue;
          int progress = 1;
          while (!copyline[k + progress] && k + progress < copyline.size())
            progress++;
          if (copyline[k] != copyline[k + progress]) {
            if (count >= 4) {
              int target = copyline[k];
              for (int l = k; copyline[l] == 0 || copyline[l] == target; l--) {
                copyline[l] = 0;
                if (!l)
                  break;
              }
              k = -1;
              count = 1;
            } else
              count = 1;
            continue;
          }
          count++;
        }
        int chnum = 0;
        for (int k = 0; k < n; k++) {
          if (copyline[k])
            chnum++;
        }

        if (minch > chnum)
          minch = chnum;
        // minch = minch > chnum ? chnum : minch;
      }
    }

    cout << minch << endl;
  }
  return 0;
}