#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
int N, K;
string str;
int main() {
cin >> N >> K >> str;
vector<int> J, O, I;
for (int i = 0; i < (int)str.size(); i++) {
if (str[i] == 'J') {
J.push_back(i+1);
}
else if (str[i] == 'O') {
O.push_back(i+1);
}
else {
I.push_back(i+1);
}
}
vector<pair<int, int>> A, B, C;
for (int i = 0; i+K-1 < (int)J.size(); i++) {
A.emplace_back(J[i], J[K+i-1]);
}
for (int i = 0; i+K-1 < (int)O.size(); i++) {
B.emplace_back(O[i], O[K+i-1]);
}
for (int i = 0; i+K-1 < (int)I.size(); i++) {
C.emplace_back(I[i], I[K+i-1]);
}
int a = 0;
int b = 0;
int c = 0;
int ans = INT_MAX;
while (a < (int)A.size() && b < (int)B.size() && c < (int)C.size()) {
if (B[b].f < A[a].s) {
b++;
continue;
}
if (C[c].f < B[b].s) {
c++;
continue;
}
ans = min(ans, C[c].s-A[a].f-3*K+1);
a++;
}
if (ans == INT_MAX) ans = -1;
cout << ans << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
308 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
304 KB |
Output is correct |
6 |
Correct |
1 ms |
300 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
300 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
308 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
304 KB |
Output is correct |
6 |
Correct |
1 ms |
300 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
300 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
300 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 |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
300 KB |
Output is correct |
20 |
Correct |
1 ms |
300 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
300 KB |
Output is correct |
29 |
Correct |
1 ms |
300 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
300 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
308 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
304 KB |
Output is correct |
6 |
Correct |
1 ms |
300 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
300 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
300 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 |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
300 KB |
Output is correct |
20 |
Correct |
1 ms |
300 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
300 KB |
Output is correct |
29 |
Correct |
1 ms |
300 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
300 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
212 KB |
Output is correct |
36 |
Correct |
7 ms |
2984 KB |
Output is correct |
37 |
Correct |
8 ms |
3964 KB |
Output is correct |
38 |
Correct |
8 ms |
3996 KB |
Output is correct |
39 |
Correct |
8 ms |
3128 KB |
Output is correct |
40 |
Correct |
6 ms |
2904 KB |
Output is correct |
41 |
Correct |
7 ms |
3088 KB |
Output is correct |
42 |
Correct |
8 ms |
3960 KB |
Output is correct |
43 |
Correct |
4 ms |
1880 KB |
Output is correct |
44 |
Correct |
5 ms |
2488 KB |
Output is correct |
45 |
Correct |
6 ms |
2776 KB |
Output is correct |
46 |
Correct |
6 ms |
2884 KB |
Output is correct |
47 |
Correct |
7 ms |
2844 KB |
Output is correct |
48 |
Correct |
6 ms |
2820 KB |
Output is correct |
49 |
Correct |
4 ms |
1912 KB |
Output is correct |
50 |
Correct |
6 ms |
2840 KB |
Output is correct |
51 |
Correct |
6 ms |
2872 KB |
Output is correct |
52 |
Correct |
5 ms |
1688 KB |
Output is correct |
53 |
Correct |
5 ms |
1860 KB |
Output is correct |
54 |
Correct |
7 ms |
3944 KB |
Output is correct |
55 |
Correct |
4 ms |
2212 KB |
Output is correct |
56 |
Correct |
5 ms |
1888 KB |
Output is correct |