alexjercan/bugnet huggingface dataset:
Problem p00457 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
int main() {
  while (1) {
    int N;
    int fie[10001];
    int ans = 100000;
    cin >> N;
    if (N == 0)
      break;
    for (int i = 0; i < N; i++)
      cin >> fie[i];
    for (int i = 0; i < N; i++) {
      int memo = fie[i], l = i, r = i, cou = 1;
      for (int j = 1; j <= 3; j++) {
        fie[i] = j;
        l = i, r = i, cou = 1;
        while (1) {
          // printf("[%d][%d][%d][%d][%d]\\n",i,j,l,r,cou);
          int fg = 1;
          if (l != 0) {
            if (fie[l] == fie[l - 1]) {
              l--;
              cou++;
              fg = 0;
            }
          }
          if (r != N - 1) {
            if (fie[r] == fie[r + 1]) {
              r++;
              cou++;
              fg = 0;
            }
          }
          if (fg == 1 && (cou < 4))
            break;
          if (fg == 1 && (l == 0 || r == N - 1)) {
            cou = 0;
            break;
          }
          if (fg == 1 && fie[l - 1] != fie[r + 1]) {
            cou = 0;
            break;
          }
          if (fg == 1) {
            l--, r++;
            cou = 2;
          }
        }
        // printf("[%d]\\n",(l-0)+((N-1)-r)+cou);
        ans = min(ans, (l - 0) + ((N - 1) - r) + cou);
        fie[i] = memo;
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
int main() {
  int n, oridrop[10002], workdrop[4][10002], bef, judge, ans, minans;
  while (1) {
    scanf("%d", &n);
    ans = n, minans = n;
    if (n == 0)
      break;
    for (int i = n; i > 0; i--)
      scanf("%d", &oridrop[i]);
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        workdrop[1][j] = oridrop[j];
        workdrop[2][j] = oridrop[j];
        workdrop[3][j] = oridrop[j];
      }
      for (int j = 1; j <= 3; j++) {
        workdrop[j][i] = j;
        ans = n;
        judge = 1;

        /*
        printf("\\n==========\\n[%d]wo[%d]ni\\n==========\\n",i,j);
        */

        for (int k = 1; k <= ans; k++) {

          /*
          printf("[%d]|[%d][%d]\\n",k,workdrop[j][k],ans);
          */

          if (bef == workdrop[j][k] && k != 1)
            judge++;
          else
            judge = 1;

          /*
          printf("[%d][%d]\\n",bef,judge);
          */

          bef = workdrop[j][k];
          if (4 <= judge &&
              (workdrop[j][k] != workdrop[j][k + 1] || k == ans)) {
            ans -= judge;
            if (ans < minans)
              minans = ans;
            for (int l = k - judge + 1; l <= ans; l++)
              workdrop[j][l] = workdrop[j][l + judge];

            /*
            for(int l=1;l<=8;l++){
                printf("[%d]",workdrop[j][l]);
            puts("");
            */

            workdrop[j][ans + 1] = 0;
            k = 0;
          }
        }
      }
    }
    printf("%d\\n", minans);
  }
  return 0;
}
Problem p00457 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;

int chain(string s) {
  while (true) {
    int cnt = 1;
    char c = 'a';
    bool b = false;
    for (int i = 0; i < s.length(); i++) {
      if (c == s[i])
        cnt++;
      else
        cnt = 1;
      c = s[i];
      if (cnt == 4) {
        b = true;
        int st = i - 3;
        for (; c == s[i]; i++)
          c = s[i - 1];
        s.erase(st, i - st);
      }
      if (b)
        break;
    }
    if (!b)
      return s.length();
  }
}

int main() {
  while (true) {
    int N;
    cin >> N;
    if (!N)
      return 0;
    string s = "";
    for (int i = 0; i < N; i++) {
      char c;
      cin >> c;
      s += c;
    }
    int ans = 1 << 30;
    for (int i = 0; i < s.length(); i++) {
      char tmp = s[i];
      for (char j = '1'; j <= '3'; j++) {
        if (s[i] == j)
          continue;
        s[i] = j;
        ans = min(ans, chain(s));
      }
      s[i] = tmp;
    }
    cout << ans << endl;
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;

int chain(string s) {
  while (true) {
    int cnt = 1;
    char c = 'a';
    bool b = false;
    for (int i = 0; i < s.length(); i++) {
      if (c == s[i])
        cnt++;
      else if (s[i] != 'a')
        cnt = 1;
      if (s[i] != 'a')
        c = s[i];
      if (cnt == 4) {
        b = true;
        int mm = 0;
        for (int j = 0;; j++) {
          if (s[i - j] != 'a') {
            s[i - j] = 'a';
            mm++;
          }
          if (mm == 4)
            break;
        }
      }
      if (cnt >= 4)
        s[i] = 'a';
    }
    if (!b) {
      int ans = 0;
      for (int i = 0; i < s.length(); i++)
        ans += s[i] != 'a';
      return ans;
    }
  }
}

int main() {
  while (true) {
    int N;
    cin >> N;
    if (!N)
      return 0;
    string s = "";
    for (int i = 0; i < N; i++) {
      char c;
      cin >> c;
      s += c;
    }
    int ans = 1 << 30;
    for (int i = 0; i < s.length(); i++) {
      char tmp = s[i];
      s[i] = '1';
      ans = min(ans, chain(s));
      s[i] = '2';
      ans = min(ans, chain(s));
      s[i] = '3';
      ans = min(ans, chain(s));
      s[i] = tmp;
    }
    cout << ans << endl;
  }
}
Problem p00457 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

vector<int> v;

int main() {
  int n;
  while (cin >> n, n) {
    v.clear();
    v.push_back(10);
    for (int i = 0; i < n; i++) {
      int a;
      cin >> a;
      v.push_back(a);
    }
    v.push_back(10);

    int ans = n;

    for (int i = 1; i <= n; i++) {
      int sk = i, ek = i; // erased kukan
      int lp = 0;
      while (1) {
        int l = 0, r = 0;
        for (int j = ek + 1; j < v.size(); j++) {
          if (v[j] != v[ek + 1])
            break;
          r++;
        }
        for (int j = sk - 1; j > -1; j--) {
          if (v[j] != v[sk - 1])
            break;
          l++;
        }

        if (lp) {
          if (v[ek + 1] != v[sk - 1])
            break;
          if (l + r >= 4) {
            sk -= l;
            ek += r;
          } else
            break;
        } else {
          if (v[ek + 1] != v[sk - 1]) {
            if (l >= 3) {
              sk -= l;
            } else if (r >= 3) {
              ek += l;
            } else
              break;
          } else {
            if (l + r >= 4) {
              sk -= l;
              ek += r;
            } else
              break;
          }
        }
        lp++;
      }
      if (lp)
        ans = min(ans, n - (ek - sk + 1));
    }
    cout << ans << endl;
  }
  return 0;
}

Error: -6
Fatal glibc error: malloc assertion failure in sysmalloc: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)


Fixed submission:

#include <bits/stdc++.h>
using namespace std;

vector<int> v;
int ans = 0;

int check(int n, int res, vector<int> v) {
  if (v.size() < 3)
    return res;

  for (int i = 0; i < n; i++) {
    int bf = 0, af = 0;
    int now = i;
    while (1) {
      if (now == 0)
        break;
      if (v[now] == v[i]) {
        bf++;
        now--;
      } else
        break;
    }
    now = i;

    while (1) {
      if (now == n - 1)
        break;
      if (v[now] == v[i]) {
        af++;
        now++;
      } else
        break;
    }

    vector<int> hozon;
    if (bf + af + 1 >= 4) {
      for (int j = 0; j < bf + af + 1; j++)
        hozon.push_back(i - bf + j);
    }
    v.erase(v.begin() + i - bf, v.begin() + i + af);
    ans = max(ans, res);
    check(v.size(), res + 1, v);
    for (int j = 0; j < hozon.size(); j++)
      v.insert(v.begin() + i - bf, hozon[j]);
    hozon.clear();
  }
  return 0;
}

int main() {
  int n;
  while (cin >> n, n) {
    v.clear();
    for (int i = 0; i < n; i++) {
      int a;
      cin >> a;
      v.push_back(a);
    }
    check(n, 0, v);
    cout << ans << endl;
  }
  return 0;
}
Problem p00457 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int a[10010] = {0};
int b[10010] = {0};
int ans = 1001, w;
void kotani(int i, int j, int c, int n);
int main() {
  while (1) {
    int n;
    scanf("%d", &n);
    w = n;
    ans = n;
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      scanf("%d", &a[i]);
    }
    for (int i = 0; i < n; i++) {
      for (int j = 1; j <= 3; j++) {
        memcpy(b, a, sizeof(int) * n);
        if (b[i] != j) {
          b[i] = j;
          kotani(0, 1, 1, n);
        }
      }
    }
    printf("%d\\n", ans);
  }
}
void kotani(int i, int j, int c, int n) {
  if (b[i] == b[j] && j < n) {
    // cout<<"wwww"<<endl;
    kotani(i, j + 1, c + 1, n);
  } else {
    // cout<<"ttttt"<<endl;
    if (c >= 4) {
      int u = 0;
      for (int z = j; z < n; z++) {
        // cout<<"jjjjj"<<endl;
        b[i + u] = b[z];
        u++;
      }
      ans = min(ans, n - c);
      // cout<<n-c<<endl;
      // cout<<i<<" "<<j<<" "<<c<<" "<<n<<" "<<n-c<<endl;
      kotani(0, 1, 1, n - c);
    } else if (i + 1 < n) {
      kotani(i + 1, i + 2, 1, n);
    } else {
      /*cout<<"WWWWWWW"<<endl;
      for(int u=0;u<n;u++){
              cout<<b[u];
      }
      cout<<endl;*/
    }
  }
}

Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int a[1001] = {0};
int b[1001] = {0};
int ans = 1001, w;
void kotani(int i, int j, int c, int n);
int main() {
  while (1) {
    int n;
    scanf("%d", &n);
    w = n;
    ans = n;
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      scanf("%d", &a[i]);
    }
    for (int i = 0; i < n; i++) {
      for (int j = 1; j <= 3; j++) {
        memcpy(b, a, sizeof(int) * n);
        if (b[i] != j) {
          b[i] = j;
          kotani(0, 1, 1, n);
        }
      }
    }
    printf("%d\\n", ans);
  }
}
void kotani(int i, int j, int c, int n) {
  /*for(int s=0;s<n;s++){
          cout<<b[s];
  }
  cout<<endl;*/
  if (b[i] == b[j] && j < n) {
    // cout<<"wwww"<<endl;
    kotani(i, j + 1, c + 1, n);
  } else {
    // cout<<"ttttt"<<endl;
    if (c >= 4) {
      for (int z = 0; z < c; z++) {
        // cout<<"jjjjj"<<endl;
        if ((j + z) > n)
          break;
        b[i + z] = b[j + z];
      }
      ans = min(ans, n - c);
      cout << ans << endl;
      kotani(0, 1, 1, n - c);
    } else if (i + 1 < n) {
      kotani(i + 1, i + 2, 1, n);
    }
  }
}
Problem p00457 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int a[10010] = {0};
int b[10010] = {0};
int ans = 1001, w;
void kotani(int i, int j, int c, int n);
int main() {
  while (1) {
    int n;
    scanf("%d", &n);
    w = n;
    ans = n;
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      scanf("%d", &a[i]);
    }
    for (int i = 0; i < n; i++) {
      for (int j = 1; j <= 3; j++) {
        memcpy(b, a, sizeof(int) * n);
        if (b[i] != j) {
          b[i] = j;
          kotani(0, 1, 1, n);
        }
      }
    }
    printf("%d\\n", ans);
  }
}
void kotani(int i, int j, int c, int n) {
  if (b[i] == b[j] && j < n) {
    kotani(i, j + 1, c + 1, n);
  } else {
    if (c >= 4) {
      int u = 0;
      for (int z = j; z < n; z++) {
        b[i + u] = b[z];
        u++;
      }
      ans = min(ans, n - c);
      kotani(0, 1, 1, n - c);
    } else if (i + 1 < n) {
      kotani(i + 1, i + 2, 1, n);
    }
  }
}

Error: -6
free(): invalid pointer


Fixed submission:

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int a[10010] = {0};
int b[10010] = {0};
int ans = 1001, w;
void kotani(int i, int j, int c, int n);
int main() {
  while (1) {
    int n;
    scanf("%d", &n);
    w = n;
    ans = n;
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      scanf("%d", &a[i]);
    }
    for (int i = 0; i < n; i++) {
      for (int j = 1; j <= 3; j++) {
        memcpy(b, a, sizeof(int) * n);
        if (b[i] != j) {
          b[i] = j;
          kotani(0, 1, 1, n);
        }
        free(b);
      }
    }
    printf("%d\\n", ans);
  }
}
void kotani(int i, int j, int c, int n) {
  if (b[i] == b[j] && j < n) {
    kotani(i, j + 1, c + 1, n);
  } else {
    if (c >= 4) {
      int u = 0;
      for (int z = j; z < n; z++) {
        b[i + u] = b[z];
        u++;
      }
      ans = min(ans, n - c);
      kotani(0, 1, 1, n - c);
    } else if (i + 1 < n) {
      kotani(i + 1, i + 2, 1, n);
    }
  }
}
Problem p00457 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstddef>
#include <cstdio>
#include <vector>

#pragma warning(disable : 4996)

using namespace std;

int main() {
  while (true) {
    int N, A;
    size_t MAX = 15000;

    vector<char> D;

    scanf("%d", &N);

    if (N == 0) {
      break;
    }

    for (int i = 0; i < N; i++) {
      scanf("%d", &A);

      D.push_back(A);
    }

    for (int i = 0; i < N; i++) {
      for (char j = 1; j <= 3; j++) {
        if (D[i] != j) {
          vector<char> E(D.begin(), D.end());

          E[i] = j;

          for (int k = 0; k < E.size() - 3; k++) {

          loop:;

            if (E.size() <= 3) {
              break;
            }

            if (E[k] == E[k + 1] && E[k + 1] == E[k + 2] &&
                E[k + 2] == E[k + 3]) {
              for (char l = 0; l < 3; l++) {
                E.erase(E.begin() + k);
              }

              while (k < E.size() - 1 && E[k] == E[k + 1]) {
                E.erase(E.begin() + k);
              }

              if (k < E.size()) {
                E.erase(E.begin() + k);
              }

              k = 0;

              goto loop;
            }
          }

          MAX = min(E.size(), MAX);
        }
      }
    }

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

  return 0;
}

// Time : O( 3n ^ 3 )

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstddef>
#include <cstdio>
#include <vector>

#pragma warning(disable : 4996)

using namespace std;

int main() {
  while (true) {
    int N, A;
    size_t MAX = 15000;

    vector<int> D;

    scanf("%d", &N);

    if (N == 0) {
      break;
    }

    for (int i = 0; i < N; i++) {
      scanf("%d", &A);

      D.push_back(A);
    }

    for (int i = 0; i < N; i++) {
      for (int j = 1; j <= 3; j++) {
        if (D[i] != j) {
          vector<int> E(D.begin(), D.end());

          E[i] = j;

          for (int k = 0; k < E.size() - 3; k++) {

          loop:;

            if (E.size() <= 3) {
              break;
            }

            if (E[k] == E[k + 1] && E[k + 1] == E[k + 2] &&
                E[k + 2] == E[k + 3]) {
              for (int l = 0; l < 3; l++) {
                E.erase(E.begin() + k);
              }

              while (k < E.size() - 1 && E[k] == E[k + 1]) {
                E.erase(E.begin() + k);
              }

              if (k < E.size()) {
                E.erase(E.begin() + k);
              }

              k = 0;

              goto loop;
            }
          }

          MAX = min(E.size(), MAX);
        }
      }
    }

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

  return 0;
}

// Time : O( 3n ^ 3 )
Problem p00457 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;

int n;
int chain[10002];

int ccount(int a, int b, int cnt) {
  if (chain[a] != chain[b])
    return cnt;
  int len = 0;
  int i, j;
  for (i = a; i >= 0 && chain[a] == chain[i]; i--, len++)
    ;
  for (j = b; j < n && chain[b] == chain[j]; j++, len++)
    ;
  if (len < 4)
    return cnt;
  if (i == -1 || j == n)
    return cnt + len;
  return ccount(i, j, cnt + len);
}

int main() {
  while (true) {
    scanf("%d", &n);
    if (!n)
      return 0;
    int ans = 0;
    for (int i = 0; i < n; i++)
      scanf("%d", &chain[i]);
    for (int i = 0; i < n; i++) {
      char def = chain[i];
      for (int j = 1; j <= 3; j++) {
        chain[i] = j;
        int s, g, cnt = 1;
        for (s = i - 1; s >= 0 && chain[s] == chain[i]; s--, cnt++)
          ;
        for (g = i + 1; g < n && chain[g] == chain[i]; g++, cnt++)
          ;
        if (cnt >= 4) {
          if (s == -1 || g == n)
            ans = max(cnt, ans);
          else
            ans = max(ccount(s, g, cnt), ans);
        }
      }
      chain[i] = def;
    }
    printf("%d\\n", n - ans);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;

int n;
char chain[10000];

int ccount(int a, int b, int cnt) {
  if (chain[a] != chain[b])
    return cnt;
  int len = 0;
  int i, j;
  for (i = a; i >= 0 && chain[a] == chain[i]; i--, len++)
    ;
  for (j = b; j < n && chain[b] == chain[j]; j++, len++)
    ;
  if (len < 4)
    return cnt;
  if (i == -1 || j == n)
    return cnt + len;
  return ccount(i, j, cnt + len);
}

int main() {
  while (true) {
    scanf("%d", &n);
    if (!n)
      return 0;
    int ans = 0;
    for (int i = 0; i < n; i++)
      scanf("%d", &chain[i]);
    for (int i = 0; i < n; i++) {
      char def = chain[i];
      for (int j = 1; j <= 3; j++) {
        chain[i] = j;
        int s, g, cnt = 1;
        for (s = i - 1; s >= 0 && chain[s] == chain[i]; s--, cnt++)
          ;
        for (g = i + 1; g < n && chain[g] == chain[i]; g++, cnt++)
          ;
        if (cnt >= 4) {
          if (s == -1 || g == n)
            ans = max(cnt, ans);
          else
            ans = max(ccount(s, g, cnt), ans);
        }
      }
      chain[i] = def;
    }
    printf("%d\\n", n - ans);
  }
}
Problem p00457 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#define N_MAX 10000
using namespace std;

int N;
int M[N_MAX];

int del(int m[]) {
  int delNum = 0;
  int cnt[4];
  int bfr = -1;
  int pos[4];
  for (int i = 0; i < N; i++) {
    if (m[i] == 0)
      continue;
    if (m[i] != bfr) {
      cnt[0] = cnt[1] = cnt[2] = cnt[3] = 0;
    }
    if (cnt[m[i]] < 4)
      pos[cnt[m[i]]] = i;
    cnt[m[i]]++;
    bfr = m[i];

    if (cnt[bfr] > 4) {
      m[i] = 0;
      delNum++;
    }
    if (cnt[bfr] == 4) {
      for (int j = 0; j < 4; j++) {
        m[pos[j]] = 0;
        delNum++;
      }
    }
  }
  return delNum;
}

int high(int m[]) {
  int h = 0;
  for (int i = N - 1; i >= 0; i--) {
    if (m[i] != 0)
      h++;
  }
  return h;
}

void cp(int m[]) {
  for (int i = 0; i < N; i++) {
    m[i] = M[i];
  }
}

int solve() {
  int mini = N;
  int m[N_MAX];

  int qsize = 0;
  int qp[N_MAX];
  int qc[N_MAX];

  int cnt[4];
  int bfr = -1;
  int pos[4];
  for (int i = 0; i < N; i++) {
    if (M[i] != bfr) {
      cnt[0] = cnt[1] = cnt[2] = cnt[3] = 0;
    }
    if (cnt[M[i]] < 3)
      pos[cnt[M[i]]] = i;
    cnt[M[i]]++;
    bfr = M[i];
    if (cnt[bfr] == 3) {
      if (pos[0] - 1 >= 0) {
        qp[qsize] = pos[0] - 1;
        qc[qsize] = bfr;
        qsize++;
      }
      if (pos[3] + 1 < N) {
        qp[qsize] = pos[2] + 1;
        qc[qsize] = bfr;
        qsize++;
      }
    }
  }

  for (int i = 0; i < qsize; i++) {
    cp(m);
    m[qp[i]] = qc[i];
    while (del(m))
      ;
    mini = min(mini, high(m));
  }
  return mini;
}

int main() {
  while ((cin >> N) && N) {
    for (int i = 0; i < N; i++) {
      cin >> M[i];
    }
    cout << solve() << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#define N_MAX 10000
using namespace std;

int N;
int M[N_MAX];

int del(int m[]) {
  int delNum = 0;
  int cnt[4];
  int bfr = -1;
  int pos[4];
  for (int i = 0; i < N; i++) {
    if (m[i] == 0)
      continue;
    if (m[i] != bfr) {
      cnt[0] = cnt[1] = cnt[2] = cnt[3] = 0;
    }
    if (cnt[m[i]] < 4)
      pos[cnt[m[i]]] = i;
    cnt[m[i]]++;
    bfr = m[i];

    if (cnt[bfr] > 4) {
      m[i] = 0;
      delNum++;
    }
    if (cnt[bfr] == 4) {
      for (int j = 0; j < 4; j++) {
        m[pos[j]] = 0;
        delNum++;
      }
    }
  }
  return delNum;
}

int high(int m[]) {
  int h = 0;
  for (int i = N - 1; i >= 0; i--) {
    if (m[i] != 0)
      h++;
  }
  return h;
}

void cp(int m[]) {
  for (int i = 0; i < N; i++) {
    m[i] = M[i];
  }
}

int solve() {
  int mini = N;
  int m[N_MAX];
  for (int i = 0; i < N; i++) {
    for (int j = 1; j <= 3; j++) {
      if (M[i] == j)
        continue;
      cp(m);
      m[i] = j;
      while (del(m))
        ;
      mini = min(mini, high(m));
    }
  }
  return mini;
}

int main() {
  while ((cin >> N) && N) {
    for (int i = 0; i < N; i++) {
      cin >> M[i];
    }
    cout << solve() << endl;
  }
  return 0;
}
Problem p00457 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

int N;
vector<int> G;
vector<pair<int, int>> V;

int erase(int s) {
  int res = N;
  while (res) {
    int a, b;
    for (a = s + 1; a <= N; ++a) {
      if (a == N || G[s] != G[a])
        break;
    }
    --a;
    for (b = s - 1; b >= -1; --b) {
      if (b == -1 || G[s] != G[b])
        break;
    }
    ++b;
    if (a - b + 1 >= 4) {
      s = min(N - 1, a + 1);
      res -= a - b + 1;
      for (int i = b; i <= a; ++i)
        G[i] = 0;
      for (int i = N - 1, j = N - 1; i >= 0; --i) {
        if (G[i] != 0) {
          int tmp = G[i];
          G[i] = 0;
          G[j--] = tmp;
        }
      }
    } else {
      break;
    }
  }
  return res;
}

int main() {
  while (cin >> N && N) {
    G.resize(N);
    for (int i = 0; i < N; ++i) {
      cin >> G[i];
    }
    int res = N;
    for (int i = 0; i < N; ++i) {
      for (int j = 1; j <= 3; ++j) {
        vector<int> tmp = G;
        G[i] = j;
        res = min(res, erase(i));
        G = tmp;
      }
    }
    cout << res << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int N;
vector<int> G;

int erase() {
  int res = N;
  bool update = true;
  do {
    bool f = G[5] == 2;
    update = false;
    for (int i = 0, k = 0; i <= N; ++i) {
      if (i == N || i && G[i] != G[i - 1]) {
        if (k >= 4 && G[i - 1]) {
          update = true;
          int ni = i;
          while (k--) {
            G[--ni] = 0;
            --res;
          }
        }
        k = 1;
      } else {
        ++k;
      }
    }
    for (int i = N - 1, j = N - 1; i >= 0; --i) {
      if (G[i] != 0) {
        int tmp = G[i];
        G[i] = 0;
        G[j--] = tmp;
      }
    }
  } while (update);
  return res;
}

int main() {
  while (cin >> N && N) {
    G.resize(N);
    for (int i = 0; i < N; ++i) {
      cin >> G[i];
    }
    int res = N;
    for (int i = 0; i < N; ++i) {
      for (int j = 1; j <= 3; ++j) {
        vector<int> tmp = G;
        G[i] = j;
        res = min(res, erase());
        G = tmp;
      }
    }
    cout << res << endl;
  }
  return 0;
}