#include<bits/stdc++.h>
using namespace std;
const int MAXN = 200010;
int J[MAXN], O[MAXN], I[MAXN];
int N, K;
string s;
int bb(int v[], int x){
// achar ultimo cara i, v[i] < x
if(v[0] >= x) return 0;
int ans = 0;
int pot = (1 << 30);
while(pot){
int mans = ans + pot;
if(mans < N && v[mans] < x) ans = mans;
pot /= 2;
}
if(ans + 1 == N) return -1;
else return ans + 1;
}
int solveI(int i){
if(i == N) return -1;
int prev = I[i - 1];
int nxt = bb(I, prev + K);
if(nxt == -1) return -1;
int cost = (nxt - i + 1) - K;
return cost;
}
int solveO(int i){
if(i == N) return -1;
int prev = O[i - 1];
int nxt = bb(O, prev + K);
if(nxt == -1) return -1;
int cost = (nxt - i + 1) - K;
int x;
if((x = solveI(nxt + 1)) == -1) return -1;
return cost + x;
}
int solveJ(int i){ // starting from i
int prev = 0;
if(i > 0) prev = J[i - 1];
int nxt = bb(J, prev + K);
if(nxt == -1) return -1;
int cost = (nxt - i + 1) - K;
int x;
if((x = solveO(nxt + 1)) == -1) return -1;
return cost + x;
}
int32_t main(){
cin.tie(NULL)->sync_with_stdio(false);
cin >> N >> K;
cin >> s;
for(int i = 0; i < N; ++i){
J[i] = (s[i] == 'J');
O[i] = (s[i] == 'O');
I[i] = (s[i] == 'I');
if(i){
J[i] += J[i - 1];
O[i] += O[i - 1];
I[i] += I[i - 1];
}
}
int ans = INT_MAX;
for(int i = 0; i < N; ++i){
int x = solveJ(i);
if(x == -1) break;
// cout << i << " " << x << "\n";
ans = min(ans, x);
}
cout << ((ans == INT_MAX)? -1 : ans) << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
328 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
328 KB |
Output is correct |
13 |
Correct |
1 ms |
328 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
328 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
328 KB |
Output is correct |
13 |
Correct |
1 ms |
328 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
320 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
0 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
420 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
0 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
328 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
328 KB |
Output is correct |
13 |
Correct |
1 ms |
328 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
320 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
0 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
420 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
0 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
324 KB |
Output is correct |
36 |
Correct |
47 ms |
2912 KB |
Output is correct |
37 |
Correct |
55 ms |
3168 KB |
Output is correct |
38 |
Correct |
55 ms |
3316 KB |
Output is correct |
39 |
Correct |
48 ms |
3168 KB |
Output is correct |
40 |
Correct |
14 ms |
3148 KB |
Output is correct |
41 |
Correct |
38 ms |
3168 KB |
Output is correct |
42 |
Correct |
58 ms |
3168 KB |
Output is correct |
43 |
Correct |
2 ms |
2132 KB |
Output is correct |
44 |
Correct |
2 ms |
2528 KB |
Output is correct |
45 |
Correct |
3 ms |
3100 KB |
Output is correct |
46 |
Correct |
2 ms |
3192 KB |
Output is correct |
47 |
Correct |
2 ms |
3168 KB |
Output is correct |
48 |
Correct |
4 ms |
3168 KB |
Output is correct |
49 |
Correct |
2 ms |
2272 KB |
Output is correct |
50 |
Correct |
3 ms |
3168 KB |
Output is correct |
51 |
Correct |
2 ms |
3168 KB |
Output is correct |
52 |
Correct |
2 ms |
3044 KB |
Output is correct |
53 |
Correct |
3 ms |
3160 KB |
Output is correct |
54 |
Correct |
20 ms |
3168 KB |
Output is correct |
55 |
Correct |
8 ms |
3196 KB |
Output is correct |
56 |
Correct |
2 ms |
3148 KB |
Output is correct |