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

#include <algorithm>
#include <climits>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

namespace _ {
struct _ {
  _() { // initialize
    cin.tie(0);
    ios::sync_with_stdio(false);
  }
} _;
} // namespace _
template <typename T, size_t size> inline size_t len(const T (&)[size]) {
  return size;
}
#define println(s) cout << s << '\\n';
#define str to_string
#define rep(i, n) for (int i = 0; i < int(n); i++)
#define all(c) (c).begin(), (c).end()

#define MAX_N 100002

bool list[MAX_N];
int n, k;

// O(n)
int eval(int pos) {
  int ret = -1;
  int cont = 0;
  for (int i = 1; i < n + 1; i++) {
    if (i == pos || list[i])
      cont++;
    else {
      ret = max(ret, cont);
      cont = 0;
    }
  }
  return max(ret, cont);
}

int main() {
  while (true) {
    cin >> n >> k;
    if (n == 0 && k == 0)
      break;

    // zero clear
    rep(i, MAX_N) list[i] = false;
    rep(i, k) {
      int x;
      cin >> x;
      // x = 0: wildcard *
      list[x] = true;
    }

    int answer = -1;
    if (!list[0])
      answer = eval(-1); // no wildcard; O(n)
    else {
      vector<int> v;
      bool prev = list[1]; // the previous number is `on`
      int empty_length = 0;
      int buf_start = 1;
      for (int i = 2; i < n + 1 + 1; i++) {
        // true
        if (list[i]) {
          if (!prev) {
            // new range
            buf_start = i;
            // and finish -1s
            if (empty_length > 1)
              v.push_back(INT_MIN);
          } else {
          } // continue
        }
        // false
        else {
          if (prev) { // fin range
            // true -> false
            // range [buf_start, i-1], size: i-buf_start
            int size = i - buf_start;
            v.push_back(size);
            empty_length = 0;
          }
          // () - () -...- () => -1
          else {
            empty_length++;
          } // continue
        }
        prev = list[i];
      }
      int *v_array = &v[0];

      for (int i = 1; i < v.size(); i++) {
        int score = v_array[i - 1] + v_array[i] + 1;
        answer = max(answer, score);
      }
      // O(n(n-k))
      // for (int i=1; i<n+1; i++) if (!list[i]) {
      // for(int j=1;j<n+1;j++) cout << " " << (i==j?"*":(list[j]? str(j) :
      // "_")); cout << "\\n"; answer = max(answer, eval(i));
      //}
    }
    cout << answer << '\\n';
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

namespace _ {
struct _ {
  _() { // initialize
    cin.tie(0);
    ios::sync_with_stdio(false);
  }
} _;
} // namespace _
template <typename T, size_t size> inline size_t len(const T (&)[size]) {
  return size;
}
#define println(s) cout << s << '\\n';
#define str to_string
#define rep(i, n) for (int i = 0; i < int(n); i++)
#define all(c) (c).begin(), (c).end()

#define MAX_N 100001

bool list[MAX_N];
int n, k;

// O(n)
int eval(int pos) {
  int ret = -1;
  int cont = 0;
  for (int i = 1; i < n + 1; i++) {
    if (i == pos || list[i])
      cont++;
    else {
      ret = max(ret, cont);
      cont = 0;
    }
  }
  return max(ret, cont);
}

int main() {
  while (true) {
    cin >> n >> k;
    if (n == 0 && k == 0)
      break;

    // zero clear
    rep(i, MAX_N) list[i] = false;
    rep(i, k) {
      int x;
      cin >> x;
      // x = 0: wildcard *
      list[x] = true;
    }

    int answer = -1;
    if (!list[0])
      answer = eval(-1); // no wildcard; O(n)
    else {               // O(n(n-k))
      for (int i = 1; i < n + 1; i++)
        if (!list[i]) {
          // for(int j=1;j<n+1;j++) cout << " " << (i==j?"*":(list[j]? str(j) :
          // "_")); cout << "\\n";
          answer = max(answer, eval(i));
        }
    }
    cout << answer << '\\n';
  }
  return 0;
}