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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

#define FOR(i, x, n) for (ll i = x; i < n; i++)
#define pb push_back
#define pf push_front
#define ll long long
#define hii cout << "hii" << endl
#define pii pair<int, int>
#define pll pair<ll, ll>
#define int ll
#define mpp make_pair
#define endl '\\n'
#define ff first
#define ss second
#define vi vector<int>
#define all(s) s.begin(), s.end()
#define si size()

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (T i : v)
    os << i << ' ';
  return os;
}
template <class T> ostream &operator<<(ostream &os, const set<T> &v) {
  for (T i : v)
    os << i << ' ';
  return os;
}
template <class T, class S>
ostream &operator<<(ostream &os, const pair<T, S> &v) {
  os << v.first << ' ' << v.second;
  return os;
}
template <class T, class S>
ostream &operator<<(ostream &os, const unordered_map<T, S> &v) {
  for (auto i : v)
    os << '(' << i.first << "=>" << i.second << ')' << ' ';
  return os;
}

#ifndef ONLINE_JUDGE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <class Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << endl;
}
template <class Arg1, class... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *sep = strchr(names + 1, ',');
  cerr.write(names, sep - names) << " : " << arg1 << "  ";
  __f(sep + 1, args...);
}
#else
#define trace(...) 0
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target(                                                            \\
    "avx2,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define _CRT_SECURE_NO_WARNINGS
#endif // ifndef ONLINE_JUDGE

const int N = 5e5 + 5;
const int MAXN = 1e5 + 5;
const int mod = 1e9 + 7;
const int MOD = 998244353;
const int INF = 1e15;
const int LG = 21;

int arr[N];

pair<int, int> DP1[N];
pair<int, int> DP2[N];
bool visited[N];
vector<int> v;
int query = 1;
int n, k, c;
string input;

void print(string &ret) {
  cout << "Case #" << query++ << ": ";
  cout << ret << endl;
  return;
}

pair<int, int> f1(int idx) {
  if (idx > n) {
    return {0, mod};
  }
  if (visited[idx]) {
    return DP1[idx];
  }
  visited[idx] = 1;
  if (v[idx]) {
    pair<int, int> ans2 = f1(idx + c + 1);
    pair<int, int> ans1 = f1(idx + 1);
    if (ans1.first >= ans2.first + 1) {
      return DP1[idx] = ans1;
    } else {
      return DP1[idx] = {ans2.first + 1, idx};
    }
  } else {
    return DP1[idx] = f1(idx + 1);
  }
}

pair<int, int> f2(int idx) {
  if (idx < 1) {
    return {0, -mod};
  }
  if (visited[idx]) {
    return DP2[idx];
  }
  visited[idx] = 1;
  if (v[idx]) {
    pair<int, int> ans2 = f2(idx - c - 1);
    pair<int, int> ans1 = f2(idx - 1);
    if (ans1.first >= ans2.first + 1) {
      return DP2[idx] = ans1;
    } else {
      return DP2[idx] = {ans2.first + 1, idx};
    }
  } else {
    return DP2[idx] = f2(idx - 1);
  }
}

void solve() {
  cin >> n >> k >> c;
  cin >> input;
  v.push_back(0);
  for (int i = 0; i < n; i++) {
    v.push_back(input[i] == 'o');
  }
  f1(1);
  memset(visited, 0, sizeof(visited));
  f2(n);
  set<int> ret;
  for (int i = 1; i <= n; i++) {
    if (i == 6) {
      trace(DP1[i], DP2[i]);
    }
    if (!v[i])
      continue;
    if (i == 1) {
      if (DP1[i + 1].first < k) {
        ret.insert(i);
      }
    } else if (i == n) {
      if (DP2[i - 1].first < k) {
        ret.insert(i);
      }
    } else {
      int r = DP1[i + 1].second;
      int l = DP2[i - 1].second;
      if (i == 2) {
        trace(l, r);
      }
      if (DP1[i + 1].first + DP2[i - 1].first < k) {
        ret.insert(i);
      }
    }
  }
  for (int x : ret) {
    cout << x << endl;
  }
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int t = 1;
  // cin >> t;
  while (t--)
    solve();
  return 0;
}

/*
// 自分の回答は以下。max_04だけTLEなので一旦oとする。
#include <bits/stdc++.h>
#define REP(i, n) for(int i=0;i<int(n);++i)
using namespace std;
template<class A>void pr(A a){cout << a << endl;}
template<class A,class B>void pr(A a,B b){cout << a << " "  ;p(b);}
template<class A,class B,class C>void pr(A a,B b,C c){cout << a << " " ;p(b,c);}
template<class A,class B,class C,class D>void pr(A a,B b,C c,D d){cout << a << "
" ;p(b,c,d);} typedef long long ll; typedef pair<ll, ll> l_l; typedef pair<int,
int> i_i; int INF=numeric_limits<int>::max();
// for(int j=0;j<N;++j) {}

int main(void) {
  int n,k,c;
  scanf("%d%d%d\\n",&n,&k,&c);
  string s;cin>>s;
  int L[k];
  int R[k];

  L[0] = s.find('o');
  for(int j=1;j<k;++j) {
    int o_i = s.substr(L[j-1]+c+1,n-(L[j-1]+c+1)).find('o')+L[j-1]+c+1;
    L[j] = o_i;
  }

  // REP(i,k) {
  //   pr(L[i]);
  // }

  // pr("===");

  reverse(s.begin(),s.end());

  R[k-1] = n-1-s.find('o');
  for(int j=1;j<k;++j) {
    R[k-1-j] =
n-1-(s.substr(n-1-R[k-j]+c+1,n-(n-1-R[k-j]+c+1)).find('o')+n-1-R[k-j]+c+1);
  }

  // REP(i,k) {
  //   pr(R[i]);
  // }

  REP(i,k) {
    if(L[i]==R[i]) printf("%d\\n", L[i]+1);
  }
  return 0;
}


*/

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < int(n); ++i)
using namespace std;
template <class A> void pr(A a) { cout << a << endl; }
template <class A, class B> void pr(A a, B b) {
  cout << a << " ";
  p(b);
}
template <class A, class B, class C> void pr(A a, B b, C c) {
  cout << a << " ";
  p(b, c);
}
template <class A, class B, class C, class D> void pr(A a, B b, C c, D d) {
  cout << a << " ";
  p(b, c, d);
}
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
int INF = numeric_limits<int>::max();
// for(int j=0;j<N;++j) {}

int main(void) {
  int n, k, c;
  scanf("%d%d%d\\n", &n, &k, &c);
  string s;
  cin >> s;
  int L[k];
  int R[k];

  L[0] = s.find('o');
  for (int j = 1; j < k; ++j) {
    int o_i = s.substr(L[j - 1] + c + 1, n - (L[j - 1] + c + 1)).find('o') +
              L[j - 1] + c + 1;
    L[j] = o_i;
  }

  // REP(i,k) {
  //   pr(L[i]);
  // }

  // pr("===");

  reverse(s.begin(), s.end());

  R[k - 1] = n - 1 - s.find('o');
  for (int j = 1; j < k; ++j) {
    R[k - 1 - j] =
        n - 1 -
        (s.substr(n - 1 - R[k - j] + c + 1, n - (n - 1 - R[k - j] + c + 1))
             .find('o') +
         n - 1 - R[k - j] + c + 1);
  }

  // REP(i,k) {
  //   pr(R[i]);
  // }

  REP(i, k) {
    if (L[i] == R[i])
      printf("%d\\n", L[i] + 1);
  }
  return 0;
}