This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (auto i = (a); i < (b); ++i)
#define per(i, a, b) for (auto i = (b); i-- > (a); )
#define lb(x...) lower_bound(x)
#define ub(x...) upper_bound(x)
const int N = 1 << 18, inf = 1e9;
char s[N];
int ci[N], co[N], cj[N];
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
int n, k, res = inf; cin >> n >> k >> s;
rep(i, 0, n) {
cj[i + 1] = cj[i];
co[i + 1] = co[i];
ci[i + 1] = ci[i];
if (s[i] == 'J') ++cj[i + 1];
if (s[i] == 'O') ++co[i + 1];
if (s[i] == 'I') ++ci[i + 1];
}
rep(l, 0, n) {
int j = lb(cj + l, cj + n + 1, cj[l] + k) - cj;
int o = lb(co + j, co + n + 1, co[j] + k) - co;
int i = lb(ci + o, ci + n + 1, ci[o] + k) - ci;
if (i == n + 1) break;
res = min(res, i - l - 3 * k);
}
cout << (res < inf ? res : -1);
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |