#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize ("Ofast")
#pragma GCC target("fma,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#pragma GCC optimize("unroll-loops")
int main () {
ios::sync_with_stdio(0);
cin.tie(0);
int n, k;
cin >> n >> k;
string w;
cin >> w;
vector<int> prefix_j (n);
vector<int> prefix_i (n);
for (int i = 0; i < n; i++) {
prefix_j[i] = (w[i] == 'J');
prefix_i[i] = (w[i] == 'I');
if (i) {
prefix_j[i] += prefix_j[i - 1];
prefix_i[i] += prefix_i[i - 1];
}
}
prefix_j.emplace_back(prefix_j.back());
prefix_i.emplace_back(prefix_i.back());
auto range_sum = [&] (int lo, int hi, vector<int> &prefix) {
return (hi >= 0 ? prefix[hi] : 0) - (lo - 1 >= 0 ? prefix[lo - 1] : 0);
};
auto upper = [&] (int lo, int hi, vector<int> &prefix) {
if (range_sum(lo, hi, prefix) < k) {
return (int) 1e9;
}
int inicio = lo;
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if (range_sum(inicio, mid, prefix) >= k) hi = mid;
else lo = mid + 1;
}
return hi;
};
auto lower = [&] (int lo, int hi, vector<int> &prefix) {
if (range_sum(lo, hi, prefix) < k) {
return (int) 1e9;
}
int inicio = hi;
while (lo < hi) {
int mid = lo + (hi - lo) / 2 + 1;
if (range_sum(mid, inicio, prefix) >= k) lo = mid;
else hi = mid - 1;
}
return lo;
};
int lo = 0;
int cnt = 0;
int mn = 1e9;
for (int hi = 0; hi < n; hi++) {
cnt += (w[hi] == 'O');
// Remover extra
while (cnt > k) {
cnt -= (w[lo] == 'O');
++lo;
}
if (cnt == k) {
// Ajustar janela
while (w[lo] != 'O') {
++lo;
}
int menor = lower (0, lo - 1, prefix_j);
int maior = upper (hi + 1, n, prefix_i);
if (menor > 1e7 || maior > 1e7) continue;
int cur = (maior - menor + 1) - (k * 3);
mn = min (mn, cur);
}
}
cout << (mn > 1e7 ? -1 : mn) << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
388 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
388 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
5 ms |
512 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
26 |
Correct |
5 ms |
384 KB |
Output is correct |
27 |
Correct |
5 ms |
384 KB |
Output is correct |
28 |
Correct |
5 ms |
384 KB |
Output is correct |
29 |
Correct |
5 ms |
384 KB |
Output is correct |
30 |
Correct |
5 ms |
384 KB |
Output is correct |
31 |
Correct |
4 ms |
384 KB |
Output is correct |
32 |
Correct |
5 ms |
512 KB |
Output is correct |
33 |
Correct |
5 ms |
384 KB |
Output is correct |
34 |
Correct |
5 ms |
384 KB |
Output is correct |
35 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
388 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
5 ms |
512 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
26 |
Correct |
5 ms |
384 KB |
Output is correct |
27 |
Correct |
5 ms |
384 KB |
Output is correct |
28 |
Correct |
5 ms |
384 KB |
Output is correct |
29 |
Correct |
5 ms |
384 KB |
Output is correct |
30 |
Correct |
5 ms |
384 KB |
Output is correct |
31 |
Correct |
4 ms |
384 KB |
Output is correct |
32 |
Correct |
5 ms |
512 KB |
Output is correct |
33 |
Correct |
5 ms |
384 KB |
Output is correct |
34 |
Correct |
5 ms |
384 KB |
Output is correct |
35 |
Correct |
5 ms |
384 KB |
Output is correct |
36 |
Correct |
27 ms |
2996 KB |
Output is correct |
37 |
Correct |
28 ms |
3212 KB |
Output is correct |
38 |
Correct |
28 ms |
3196 KB |
Output is correct |
39 |
Correct |
29 ms |
3204 KB |
Output is correct |
40 |
Correct |
20 ms |
3196 KB |
Output is correct |
41 |
Correct |
26 ms |
3212 KB |
Output is correct |
42 |
Correct |
31 ms |
3196 KB |
Output is correct |
43 |
Correct |
12 ms |
2200 KB |
Output is correct |
44 |
Correct |
15 ms |
2604 KB |
Output is correct |
45 |
Correct |
17 ms |
3212 KB |
Output is correct |
46 |
Correct |
17 ms |
3196 KB |
Output is correct |
47 |
Correct |
17 ms |
3196 KB |
Output is correct |
48 |
Correct |
17 ms |
3196 KB |
Output is correct |
49 |
Correct |
13 ms |
2316 KB |
Output is correct |
50 |
Correct |
18 ms |
3196 KB |
Output is correct |
51 |
Correct |
18 ms |
3196 KB |
Output is correct |
52 |
Correct |
9 ms |
3100 KB |
Output is correct |
53 |
Correct |
9 ms |
3196 KB |
Output is correct |
54 |
Correct |
22 ms |
3212 KB |
Output is correct |
55 |
Correct |
14 ms |
3196 KB |
Output is correct |
56 |
Correct |
10 ms |
3212 KB |
Output is correct |