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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
int main(void) {
  for (;;) {
    int ans = 10000;
    int n;
    cin >> n;

    if (n == 0)
      break;
    int puyo[11000] = {0};
    for (int i = 0; i < n; i++) {
      cin >> puyo[i];
    }
    // in
    int sub;
    for (int a = 0; a < n; a++) {
      for (int b = 1; b <= 3; b++) {
        sub = puyo[a];
        puyo[a] = b;
        int mem = -1, cnt = 1;
        int meml = 0;
        int left = n;
        // つながり検出
        for (int i = 1; i < n; i++) {
          if (puyo[i] == puyo[i - 1]) {
            cnt++;
          } else
            cnt = 1;
          if (cnt >= 4) {
            meml = max(meml, cnt);
            mem = i - meml;
            // memは連鎖するぷよの一つ上
            // memeは連鎖するぷよの一つ下
          }
        }
        left -= meml;

        int rem = meml;
        // つながり検出
        // 連鎖計算
        // cout<<"-"<<meml<<endl;

        for (;;) {
          int meme = mem + meml + 1;
          int kem = mem;
          rem = 1;
          // cout<<mem<<" "<<meme<<endl;
          for (int i = mem - 1; i >= 0; i--) {
            if (puyo[i] == puyo[kem]) {
              rem++;
              //        cout<<rem<<"-"<<i<<endl;

            } else {
              mem = i;
              break;
            }
          }
          for (int j = meme; j < n; j++) {
            if (puyo[j] == puyo[kem]) {
              rem++;
              //       cout<<rem<<"+"<<j<<endl;

            } else {
              meme = j;
              break;
            }
          }
          if (rem >= 4) {
            //     cout<<"0";
          } else
            break;
          left -= rem;
          meml += rem;
        }
        ans = min(ans, left);
        puyo[a] = sub;
      }
    }
    // 連鎖計算
    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;
int main(void) {

  for (;;) {
    int n;
    int a[10005] = {0};
    int b[10005] = {0};
    int z[10005] = {0};
    int y = 0;
    int k = 0;
    int w, q;
    int count = 0, m;
    cin >> n;
    if (n == 0)
      break;
    m = n;
    int j = 0, flag = 0, flag2 = 0;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
      b[i] = a[i];
    }

    for (w = 1; w <= 3; w++) {
      for (q = 1; q < n - 1; q++) {
        for (int c = 0; c < n; c++) {
          a[c] = b[c];
        }
        a[q] = w;
        // cout<<a[q]<<"+"<<q<<endl;
        for (int p = 0; p < n; p++) {
          if (a[p + 1] != 0 && a[p] != 0 && a[p] == a[p + 1]) {
            for (k = 0; k < n - p; k++) {
              // cout<<k<<"!"<<p<<endl;
              if (a[p + k] != a[p + k + 1])
                break;
            }

            if (k >= 3) {

              for (;;) {
                flag = 0;
                j = 0;
                for (int i = 0; i < n; i++) {
                  if (a[i + 1] != 0 && a[i] != 0 && a[i] == a[i + 1]) {

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

                      if (a[i + j] != a[i + j + 1]) {

                        // cout<<i<<"."<<j<<endl;
                        break;
                      }
                    }
                    if (j >= 3) {
                      flag = 1;
                      for (int g = 0; g < j + 1; g++) {
                        if (i + g < n) {
                          a[i + g] = -1;
                          // cout<<"oo"<<i+g<<endl;
                        }
                      }
                      for (int s = 0; s < n; s++) {
                        if (a[s] == -1) {
                          for (int t = s; t < n; t++) {
                            //     cout<<i<<"+++"<<j<<endl;
                            a[t] = a[t + j + 1];
                          }
                        }
                      }
                    }
                  }
                }
                if (flag == 0)
                  break;
              }
            }
          }
        }

        count = 0;
        for (int i = 0; i < n; i++) {
          if (a[i] != 0)
            count++;
        }
        // cout<<m<<"#"<<count<<"--a["<<q<<"]="<<w<<endl;
        m = min(count, m);
      }
    }

    cout << m << endl;
  }

  return 0;
}