#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, k;
cin >> n >> k;
string s;
cin >> s;
vector<int> prefix_j(n + 1), prefix_o(n + 1), prefix_i(n + 1);
for (int i = 0; i < n; i++) {
prefix_j[i + 1] = prefix_j[i] + (s[i] == 'J');
prefix_o[i + 1] = prefix_o[i] + (s[i] == 'O');
prefix_i[i + 1] = prefix_i[i] + (s[i] == 'I');
}
int ans = n + 1;
for (int i = k; i <= n - 2 * k; i++) {
if (prefix_j[i] < k || prefix_o[n] - prefix_o[i] < k) {
continue;
}
auto check = [&](int j) {
if (prefix_o[j] - prefix_o[i] < k) {
return -1;
}
int l = j, r = n + 1;
while (l + 1 < r) {
int mid = (l + r) / 2;
if (prefix_i[mid] - prefix_i[j] >= k) {
r = mid;
} else {
l = mid;
}
}
return r <= n ? r : -2;
};
int l = i, r = n + 1;
while (l + 1 < r) {
int mid = (l + r) / 2;
if (~check(mid)) {
r = mid;
} else {
l = mid;
}
}
if (r <= n) {
r = check(r);
int L = 0, R = i + 1;
while (L + 1 < R) {
int mid = (L + R) / 2;
if (prefix_j[i] - prefix_j[mid - 1] >= k) {
L = mid;
} else {
R = mid;
}
}
if (r > 0) {
ans = min(ans, r - L + 1 - 3 * k);
}
}
}
cout << (ans > n ? -1 : ans) << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
504 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
504 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
2 ms |
336 KB |
Output is correct |
16 |
Correct |
2 ms |
336 KB |
Output is correct |
17 |
Correct |
2 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
2 ms |
336 KB |
Output is correct |
20 |
Correct |
2 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
504 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
1 ms |
336 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
1 ms |
336 KB |
Output is correct |
27 |
Correct |
1 ms |
452 KB |
Output is correct |
28 |
Correct |
1 ms |
336 KB |
Output is correct |
29 |
Correct |
1 ms |
484 KB |
Output is correct |
30 |
Correct |
1 ms |
508 KB |
Output is correct |
31 |
Correct |
1 ms |
336 KB |
Output is correct |
32 |
Correct |
1 ms |
336 KB |
Output is correct |
33 |
Correct |
2 ms |
336 KB |
Output is correct |
34 |
Correct |
2 ms |
336 KB |
Output is correct |
35 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
504 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
2 ms |
336 KB |
Output is correct |
16 |
Correct |
2 ms |
336 KB |
Output is correct |
17 |
Correct |
2 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
2 ms |
336 KB |
Output is correct |
20 |
Correct |
2 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
504 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
1 ms |
336 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
1 ms |
336 KB |
Output is correct |
27 |
Correct |
1 ms |
452 KB |
Output is correct |
28 |
Correct |
1 ms |
336 KB |
Output is correct |
29 |
Correct |
1 ms |
484 KB |
Output is correct |
30 |
Correct |
1 ms |
508 KB |
Output is correct |
31 |
Correct |
1 ms |
336 KB |
Output is correct |
32 |
Correct |
1 ms |
336 KB |
Output is correct |
33 |
Correct |
2 ms |
336 KB |
Output is correct |
34 |
Correct |
2 ms |
336 KB |
Output is correct |
35 |
Correct |
1 ms |
336 KB |
Output is correct |
36 |
Correct |
109 ms |
2936 KB |
Output is correct |
37 |
Correct |
111 ms |
3292 KB |
Output is correct |
38 |
Correct |
117 ms |
3120 KB |
Output is correct |
39 |
Correct |
90 ms |
3292 KB |
Output is correct |
40 |
Correct |
41 ms |
3292 KB |
Output is correct |
41 |
Correct |
110 ms |
3292 KB |
Output is correct |
42 |
Correct |
153 ms |
3292 KB |
Output is correct |
43 |
Correct |
15 ms |
2128 KB |
Output is correct |
44 |
Correct |
20 ms |
2696 KB |
Output is correct |
45 |
Correct |
25 ms |
3292 KB |
Output is correct |
46 |
Correct |
29 ms |
3548 KB |
Output is correct |
47 |
Correct |
24 ms |
3304 KB |
Output is correct |
48 |
Correct |
23 ms |
3292 KB |
Output is correct |
49 |
Correct |
15 ms |
2412 KB |
Output is correct |
50 |
Correct |
23 ms |
3332 KB |
Output is correct |
51 |
Correct |
31 ms |
3300 KB |
Output is correct |
52 |
Correct |
4 ms |
3036 KB |
Output is correct |
53 |
Correct |
4 ms |
3292 KB |
Output is correct |
54 |
Correct |
98 ms |
3292 KB |
Output is correct |
55 |
Correct |
22 ms |
3328 KB |
Output is correct |
56 |
Correct |
4 ms |
3292 KB |
Output is correct |