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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define loop(__x, __start, __end) for (int __x = __start; __x < __end; __x++)
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (int i = 0; i < (int)v.size(); ++i) {
    os << v[i];
    if (i != (int)v.size() - 1)
      os << ' ';
  }
  return os;
}
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

/*
 * 働ける日だが働かない日をiとして、i日目無しでk日達成できるかを確認する.
 * 以下i) ~ ii)のいずれかを満たす場合k日達成できる.
 * i) i-c+1以前とi+1以後でk日達成している
 * ii) i+c-1以後とi-1以前だけでk日達成している
 */
int main() {
  string s;
  int n, k, c;
  cin >> n >> k >> c >> s;
  if (n == k and k == 1) {
    cout << 1 << endl;
    return 0;
  }

  vector<int> accl(n, 0), accr(n, 0);
  loop(i, 0, n) {
    if (i == 0)
      accl[i] = s[i] == 'o';
    else if (i - c - 1 < 0)
      accl[i] = max(accl[i - 1], (int)(s[i] == 'o'));
    else {
      chmax(accl[i], accl[i - c - 1] + (int)(s[i] == 'o'));
      chmax(accl[i], accl[i - 1]);
    }
  }

  for (int i = n - 1; i >= 0; i--) {
    if (i == n - 1)
      accr[i] = s[i] == 'o';
    else if (i + c + 1 >= n)
      accr[i] = max(accr[i + 1], (int)(s[i] == 'o'));
    else {
      chmax(accr[i], accr[i + c + 1] + (int)(s[i] == 'o'));
      chmax(accr[i], accr[i + 1]);
    }
  }
  // cout << accl << endl;
  // cout << accr << endl;

  loop(i, 0, n) {
    if (s[i] == 'x')
      continue;
    bool ok = false;
    {
      int num = 0;
      if (i - 1 >= 0)
        num += accl[i - 1];
      if (i + 1 < n)
        num += accr[i + 1];
      ok |= num >= k;
    }
    {
      int num = 0;
      if (i + 1 < n)
        num += accr[i + 1];
      if (i - 1 >= 0)
        num += accl[i - 1];
      ok |= num >= k;
    }
    if (!ok) {
      cout << i + 1 << endl;
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

/*
 * atcoder/abc161/e.cpp
 */

// C++ 14
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstring> // memset
#include <iostream>
#include <vector>

using namespace std;

#define ll long long

#define loop(__x, __start, __end) for (int __x = __start; __x < __end; __x++)
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

const int MAX_N = 200000;
ll N, K, C;
bool ok[MAX_N] = {0};

int work[MAX_N] = {0};

void input() {
  string S;
  cin >> N >> K >> C >> S;
  loop(n, 0, N) { ok[n] = S[n] == 'o'; }
}

ll rec(int n, int backoff, vector<int> days) {
  if (n == N) {
    if (days.size() == K) {
      for (auto &&d : days) {
        work[d]++;
      }
    }
    return days.size() == K;
  }
  // cant work
  if (!ok[n] || backoff > 0)
    return rec(n + 1, max(0, backoff - 1), days);

  ll ways = 0;
  // not work
  ways += rec(n + 1, 0, days);
  // work
  days.push_back(n);
  ways += rec(n + 1, C, days);
  return ways;
}

ll memoRec() {
  vector<int> days;
  return rec(0, 0, days);
}

void solve() {
  ll ways = memoRec();
  ll count = 0;
  for (int n = 0; n < N; n++) {
    if (work[n] == ways)
      cout << n + 1 << endl;
  }
}

int main() {
  // cout.precision(15);
  input();
  solve();
  return 0;
}