#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";
}
// Binary Search Sol (TLE)
/*int main() {
cin >> N >> K >> s;
vector<int> J, O, I;
for (int i = 0; i < (int)s.size(); i++) {
if (s[i] == 'J') J.push_back(i+1);
else if (s[i] == 'O') O.push_back(i+1);
else I.push_back(i+1);
}
O.push_back(INT_MAX);
I.push_back(INT_MAX);
int ans = INT_MAX;
for (int i = 0; i+K-1 < (int)J.size(); i++) {
int cur = 0;
vector<int> arr;
for (int j = i; j < i+K; j++) {
if (!arr.empty()) cur += J[j]-arr.back()-1;
arr.push_back(J[j]);
}
int u = upper_bound(O.begin(), O.end(), arr.back())-O.begin();
if (u == INT_MAX) break;
bool flag = false;
for (int j = u; j < u+K; j++) {
if (O[j] == INT_MAX) {
flag = true;
break;
}
cur += O[j]-arr.back()-1;
arr.push_back(O[j]);
}
if (flag) break;
u = upper_bound(I.begin(), I.end(), arr.back())-I.begin();
if (u == INT_MAX) break;
for (int j = u; j < u+K; j++) {
if (I[j] == INT_MAX) {
flag = true;
break;
}
cur += I[j]-arr.back()-1;
arr.push_back(I[j]);
}
if (flag) break;
ans = min(ans, cur);
}
if (ans == INT_MAX) ans = -1;
cout << ans << "\n";
}*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
276 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 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 |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
276 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
0 ms |
340 KB |
Output is correct |
26 |
Correct |
0 ms |
340 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
0 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
340 KB |
Output is correct |
34 |
Correct |
0 ms |
340 KB |
Output is correct |
35 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
276 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
0 ms |
340 KB |
Output is correct |
26 |
Correct |
0 ms |
340 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
0 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
340 KB |
Output is correct |
34 |
Correct |
0 ms |
340 KB |
Output is correct |
35 |
Correct |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
6 ms |
2912 KB |
Output is correct |
37 |
Correct |
7 ms |
3860 KB |
Output is correct |
38 |
Correct |
7 ms |
3868 KB |
Output is correct |
39 |
Correct |
7 ms |
3012 KB |
Output is correct |
40 |
Correct |
6 ms |
2736 KB |
Output is correct |
41 |
Correct |
6 ms |
2960 KB |
Output is correct |
42 |
Correct |
7 ms |
3864 KB |
Output is correct |
43 |
Correct |
4 ms |
1752 KB |
Output is correct |
44 |
Correct |
5 ms |
2392 KB |
Output is correct |
45 |
Correct |
6 ms |
2776 KB |
Output is correct |
46 |
Correct |
6 ms |
2756 KB |
Output is correct |
47 |
Correct |
6 ms |
2764 KB |
Output is correct |
48 |
Correct |
6 ms |
2764 KB |
Output is correct |
49 |
Correct |
5 ms |
1720 KB |
Output is correct |
50 |
Correct |
6 ms |
2652 KB |
Output is correct |
51 |
Correct |
6 ms |
2772 KB |
Output is correct |
52 |
Correct |
5 ms |
1496 KB |
Output is correct |
53 |
Correct |
5 ms |
1732 KB |
Output is correct |
54 |
Correct |
5 ms |
3816 KB |
Output is correct |
55 |
Correct |
4 ms |
2020 KB |
Output is correct |
56 |
Correct |
4 ms |
1764 KB |
Output is correct |