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

#include <bits/stdc++.h>

using namespace std;
using ll = int64_t;
using pii = pair<int, int>;
using pli = pair<ll, int>;
using mii = map<int, int>;
using vi = vector<int>;
using vii = vector<vi>;
using vl = vector<ll>;
using vll = vector<vl>;
using tpi = tuple<int, int, int>;

using vqi = vector<queue<int>>;
using vpi = vector<pii>;
using bs = bitset<60>;
using vbi = vector<bs>;

ll gcd(ll a, ll b) {
  if (a < b) {
    swap(a, b);
  }

  /* ユークリッドの互除法 */
  ll r = a % b;
  while (r != 0) {
    a = b;
    b = r;
    r = a % b;
  }
  return b;
}

ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

constexpr int64_t __pow_mod(int64_t n, int64_t p, int64_t mod) noexcept {
  if (p == 0) {
    return 1;
  }
  if (p % 2 == 0) {
    int64_t t = __pow_mod(n, p / 2, mod);
    return (t * t) % mod;
  }
  return (n * (__pow_mod(n, p - 1, mod) % mod)) % mod;
}

constexpr int64_t pow_mod(int64_t n, int64_t p, int64_t mod) noexcept {
  int64_t res = __pow_mod(n, p, mod);
  if (res == 0) {
    res = mod;
  }
  return res;
}

constexpr int64_t nCr_mod(int64_t n, int64_t r, int64_t mod) noexcept {
  int64_t count = 1;
  auto a = r;
  for (auto i = n; i > n - r; i--) {
    count *= i;
    if (count % mod != 0) {
      count %= mod;
    }
    int64_t inv = pow_mod(a, mod - 2, mod);
    count *= inv;
    if (count % mod != 0) {
      count %= mod;
    }
    a--;
  }
  return count;
}

void solve(long long N, long long K, long long C, std::string S) {
  vi L(K), R(K);
  size_t k = 0;
  for (ll i = 0; i < N; i++) {
    if (S[i] == 'o') {
      L[k] = i + 1;
      k++;
      i += C;
    }
    if (k == K) {
      break;
    }
  }
  k = 0;
  for (ll i = N - 1; i >= 0; i--) {
    if (S[i] == 'o') {
      R[k] = i + 1;
      k++;
      i -= C;
    }
    if (k == K) {
      break;
    }
  }

  /*
  for (auto item : L) {
      cout << item << " ";
  }
  cout << endl;
  for (auto item : R) {
      cout << item << " ";
  }
  cout << endl;
   */

  auto l = L.cbegin();
  auto r = R.crbegin();

  for (auto i = 0; i < K; i++) {
    if (*l == *r) {
      cout << *l << endl;
    }
    l++;
    r++;
  }
}

int main() {
  long long N;
  std::cin >> N;
  long long K;
  std::cin >> K;
  long long C;
  std::cin >> C;
  std::string S;
  std::cin >> S;
  solve(N, K, C, S);
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = int64_t;
using pii = pair<int, int>;
using pli = pair<ll, int>;
using mii = map<int, int>;
using vi = vector<int>;
using vii = vector<vi>;
using vl = vector<ll>;
using vll = vector<vl>;
using tpi = tuple<int, int, int>;

using vqi = vector<queue<int>>;
using vpi = vector<pii>;
using bs = bitset<60>;
using vbi = vector<bs>;

ll gcd(ll a, ll b) {
  if (a < b) {
    swap(a, b);
  }

  /* ユークリッドの互除法 */
  ll r = a % b;
  while (r != 0) {
    a = b;
    b = r;
    r = a % b;
  }
  return b;
}

ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

constexpr int64_t __pow_mod(int64_t n, int64_t p, int64_t mod) noexcept {
  if (p == 0) {
    return 1;
  }
  if (p % 2 == 0) {
    int64_t t = __pow_mod(n, p / 2, mod);
    return (t * t) % mod;
  }
  return (n * (__pow_mod(n, p - 1, mod) % mod)) % mod;
}

constexpr int64_t pow_mod(int64_t n, int64_t p, int64_t mod) noexcept {
  int64_t res = __pow_mod(n, p, mod);
  if (res == 0) {
    res = mod;
  }
  return res;
}

constexpr int64_t nCr_mod(int64_t n, int64_t r, int64_t mod) noexcept {
  int64_t count = 1;
  auto a = r;
  for (auto i = n; i > n - r; i--) {
    count *= i;
    if (count % mod != 0) {
      count %= mod;
    }
    int64_t inv = pow_mod(a, mod - 2, mod);
    count *= inv;
    if (count % mod != 0) {
      count %= mod;
    }
    a--;
  }
  return count;
}

void solve(long long N, long long K, long long C, std::string S) {
  list<ll> L, R;
  for (ll i = 0; i < S.length(); i++) {
    if (S[i] == 'o') {
      L.push_back(i + 1);
      i += C;
    }
    if (L.size() == K) {
      break;
    }
  }
  for (ll i = S.length() - 1; i >= 0; i--) {
    if (S[i] == 'o') {
      R.push_front(i + 1);
      i -= C;
    }
    if (R.size() == K) {
      break;
    }
  }

  auto l = L.begin();
  auto r = R.begin();

  for (auto i = 0; i < L.size(); i++) {
    if (*l == *r) {
      cout << *l << endl;
    }
    l++;
    r++;
  }
}

int main() {
  long long N;
  std::cin >> N;
  long long K;
  std::cin >> K;
  long long C;
  std::cin >> C;
  std::string S;
  std::cin >> S;
  solve(N, K, C, S);
  return 0;
}