#include <iostream>
using namespace std;
int val(char c){
if (c == 'J') return 0;
if (c == 'O') return 1;
if (c == 'I') return 2;
}
const int INF = 1e9 + 9;
int main(){
cin.tie(0)->sync_with_stdio(0);
int n, k; cin >> n >> k;
string s; cin >> s;
int l[3]{}, r[3]{}, cnt[3]{};
int ans = INF;
while (true){
while (r[0] < n && cnt[0] < k){
if (val(s[r[0]++]) == 0)
cnt[0]++;
}
while (l[1] < r[0]){
if (val(s[l[1]++]) == 1)
cnt[1]--;
}
while (r[1] < n && cnt[1] < k){
if (val(s[r[1]++]) == 1)
cnt[1]++;
}
while (l[2] < r[1]){
if (val(s[l[2]++]) == 2)
cnt[2]--;
}
while (r[2] < n && cnt[2] < k){
if (val(s[r[2]++]) == 2)
cnt[2]++;
}
if (cnt[0] < k || cnt[1] < k || cnt[2] < k) break;
ans = min(ans, r[2] - l[0] - 3 * k);
if (val(s[l[0]++]) == 0)
cnt[0]--;
}
cout << (ans == INF? -1: ans);
return 0;
}
Compilation message
ho_t2.cpp: In function 'int val(char)':
ho_t2.cpp:8:1: warning: control reaches end of non-void function [-Wreturn-type]
8 | }
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
320 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
324 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
316 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
320 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
324 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
316 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
320 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
320 KB |
Output is correct |
20 |
Correct |
1 ms |
316 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
320 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
320 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
324 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
320 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
324 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
316 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
320 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
320 KB |
Output is correct |
20 |
Correct |
1 ms |
316 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
320 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
320 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
324 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
212 KB |
Output is correct |
36 |
Correct |
6 ms |
852 KB |
Output is correct |
37 |
Correct |
6 ms |
852 KB |
Output is correct |
38 |
Correct |
6 ms |
852 KB |
Output is correct |
39 |
Correct |
7 ms |
844 KB |
Output is correct |
40 |
Correct |
4 ms |
836 KB |
Output is correct |
41 |
Correct |
6 ms |
852 KB |
Output is correct |
42 |
Correct |
8 ms |
844 KB |
Output is correct |
43 |
Correct |
2 ms |
592 KB |
Output is correct |
44 |
Correct |
3 ms |
852 KB |
Output is correct |
45 |
Correct |
4 ms |
832 KB |
Output is correct |
46 |
Correct |
4 ms |
868 KB |
Output is correct |
47 |
Correct |
4 ms |
852 KB |
Output is correct |
48 |
Correct |
4 ms |
844 KB |
Output is correct |
49 |
Correct |
3 ms |
768 KB |
Output is correct |
50 |
Correct |
4 ms |
836 KB |
Output is correct |
51 |
Correct |
4 ms |
852 KB |
Output is correct |
52 |
Correct |
5 ms |
844 KB |
Output is correct |
53 |
Correct |
5 ms |
852 KB |
Output is correct |
54 |
Correct |
2 ms |
852 KB |
Output is correct |
55 |
Correct |
2 ms |
852 KB |
Output is correct |
56 |
Correct |
2 ms |
852 KB |
Output is correct |