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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef long long int64;
const int inf = (int)1e9;

int calc(int it1, int it2, vector<pair<int, int>> &b, vector<int> &sum, int n) {
  while (it1 >= 0 && it2 < b.size()) {
    if (b[it1].second <= 0) {
      it1 -= 1;
      continue;
    }
    if (b[it2].second <= 0) {
      it2 += 1;
      continue;
    }
    if (b[it1].first != b[it2].first || b[it1].second + b[it2].second < 4)
      break;
    it1 -= 1, it2 += 1;
  }

  int score = n - (sum[it2 - 1] - sum[it1]);
  return score;
}

int main() {
  while (true) {

    int n;
    vector<pair<int, int>> b;
    vector<int> sum;
    int prev = -1;

    scanf("%d", &n);
    if (n == 0)
      break;
    b.reserve(n);
    for (int i = 0; i < n; ++i) {
      int t;
      scanf("%d", &t);
      if (prev != t)
        b.push_back(make_pair(t, 1));
      else
        b[b.size() - 1].second += 1;
      prev = t;
    }
    sum.resize(b.size(), 0);
    sum[0] = b[0].second;
    for (int i = 1; i < sum.size(); ++i)
      sum[i] = sum[i - 1] + b[i].second;

    int ans = n;

    for (int start = 0; start < b.size(); ++start) {

      b[start].second -= 1;

      if (start > 0 && b[start - 1].second >= 3) {
        int it1 = start - 2, it2 = start;
        int score = calc(it1, it2, b, sum, n);
        ans = min(score, ans);
      }

      if (start < b.size() - 1 && b[start + 1].second >= 3) {
        int it1 = start, it2 = start + 2;
        int score = calc(it1, it2, b, sum, n);
        ans = min(score, ans);
      }

      if (start > 0 && start < b.size() - 1 && b[start].second == 0 &&
          b[start - 1].first == b[start + 1].first &&
          b[start - 1].second + b[start + 1].second >= 3) {
        int it1 = start - 2, it2 = start + 2;
        int score = calc(it1, it2, b, sum, n);
        ans = min(score, ans);
      }

      b[start].second += 1;
    }

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

  return 0;
}

/* ハラスメントに負けず */
/* 0完太陽にも負けず */
/* はやく人権を獲得したい */
/* nullmineralが書きましたが */
/* 責任はまったくとりません */

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef long long int64;
const int inf = (int)1e9;

int calc(int it1, int it2, vector<pair<int, int>> &b, vector<int> &sum, int n) {
  while (it1 >= 0 && it2 < b.size()) {
    if (b[it1].second <= 0) {
      it1 -= 1;
      continue;
    }
    if (b[it2].second <= 0) {
      it1 += 1;
      continue;
    }
    if (b[it1].first != b[it2].first || b[it1].second + b[it2].second < 4)
      break;
    it1 -= 1, it2 += 1;
  }

  int score = n - (sum[it2 - 1] - sum[it1]);
  return score;
}

int main() {
  while (true) {

    int n;
    vector<pair<int, int>> b;
    vector<int> sum;
    int prev = -1;

    scanf("%d", &n);
    if (n == 0)
      break;
    b.reserve(n);
    for (int i = 0; i < n; ++i) {
      int t;
      scanf("%d", &t);
      if (prev != t)
        b.push_back(make_pair(t, 1));
      else
        b[b.size() - 1].second += 1;
      prev = t;
    }
    sum.resize(b.size(), 0);
    sum[0] = b[0].second;
    for (int i = 1; i < sum.size(); ++i)
      sum[i] = sum[i - 1] + b[i].second;

    int ans = n;

    for (int start = 0; start < b.size(); ++start) {

      b[start].second -= 1;

      if (start > 0 && b[start - 1].second >= 3) {
        int it1 = start - 2, it2 = start;
        int score = calc(it1, it2, b, sum, n);
        ans = min(score, ans);
      }

      if (start < b.size() - 1 && b[start + 1].second >= 3) {
        int it1 = start, it2 = start + 2;
        int score = calc(it1, it2, b, sum, n);
        ans = min(score, ans);
      }

      if (start > 0 && start < b.size() - 1 && b[start].second == 0 &&
          b[start - 1].first == b[start + 1].first &&
          b[start - 1].second + b[start + 1].second >= 3) {
        int it1 = start - 2, it2 = start + 2;
        int score = calc(it1, it2, b, sum, n);
        ans = min(score, ans);
      }

      b[start].second += 1;
    }

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

  return 0;
}

/* ハラスメントに負けず */
/* 0完太陽にも負けず */
/* はやく人権を獲得したい */
/* nullmineralが書きましたが */
/* 責任はまったくとりません */