#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
const int kN = 2e5;
const int kLog = 17;
int n, k, nxt[3][1 + kN][1 + kLog], nxt2[3][1 + kN];
string s;
void minSelf(int &x, int y) {
if (y < x) {
x = y;
}
}
void build(int t) {
nxt[t][n][0] = n;
nxt2[t][n] = n;
for (int i = n - 1; i >= 0; --i) {
if (i + 1 < n && s[i + 1] - '0' == t) {
nxt[t][i][0] = i + 1;
} else {
nxt[t][i][0] = nxt[t][i + 1][0];
}
if (s[i] - '0' == t) {
nxt2[t][i] = i;
} else {
nxt2[t][i] = nxt2[t][i + 1];
}
}
for (int i = 1; i <= kLog; ++i) {
nxt[t][n][i] = n;
for (int j = 0; j < n; ++j) {
if (nxt[t][j][i - 1] == n) {
nxt[t][j][i] = n;
} else {
nxt[t][j][i] = nxt[t][nxt[t][j][i - 1]][i - 1];
}
}
}
}
void lift(int t, int &index) {
for (int i = kLog; i >= 0; --i) {
if (k & (1 << i)) {
index = nxt[t][index][i];
}
}
}
void testCase() {
cin >> n >> k >> s;
k -= 1;
for (int i = 0; i < n; ++i) {
if (s[i] == 'J') {
s[i] = '0';
} else if (s[i] == 'O') {
s[i] = '1';
} else if (s[i] == 'I') {
s[i] = '2';
}
}
for (int i = 0; i < 3; ++i) {
build(i);
}
int ans = INF;
for (int i = 0; i < n; ++i) {
if (s[i] == '0') {
int j = i;
for (int p = 0; p < 3; ++p) {
lift(p, j);
if (p + 1 < 3) {
j = nxt2[p + 1][j];
}
}
if (j < n) {
minSelf(ans, j - i + 1 - 3 * (k + 1));
}
}
}
if (ans == INF) {
cout << "-1\n";
} else {
cout << ans << '\n';
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int tests = 1;
for (int tc = 0; tc < tests; ++tc) {
testCase();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
0 ms |
336 KB |
Output is correct |
4 |
Correct |
0 ms |
324 KB |
Output is correct |
5 |
Correct |
0 ms |
328 KB |
Output is correct |
6 |
Correct |
0 ms |
316 KB |
Output is correct |
7 |
Correct |
0 ms |
336 KB |
Output is correct |
8 |
Correct |
0 ms |
336 KB |
Output is correct |
9 |
Correct |
0 ms |
336 KB |
Output is correct |
10 |
Correct |
0 ms |
324 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
0 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
0 ms |
336 KB |
Output is correct |
4 |
Correct |
0 ms |
324 KB |
Output is correct |
5 |
Correct |
0 ms |
328 KB |
Output is correct |
6 |
Correct |
0 ms |
316 KB |
Output is correct |
7 |
Correct |
0 ms |
336 KB |
Output is correct |
8 |
Correct |
0 ms |
336 KB |
Output is correct |
9 |
Correct |
0 ms |
336 KB |
Output is correct |
10 |
Correct |
0 ms |
324 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
0 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
832 KB |
Output is correct |
16 |
Correct |
1 ms |
940 KB |
Output is correct |
17 |
Correct |
1 ms |
976 KB |
Output is correct |
18 |
Correct |
1 ms |
708 KB |
Output is correct |
19 |
Correct |
1 ms |
976 KB |
Output is correct |
20 |
Correct |
1 ms |
976 KB |
Output is correct |
21 |
Correct |
1 ms |
976 KB |
Output is correct |
22 |
Correct |
1 ms |
964 KB |
Output is correct |
23 |
Correct |
1 ms |
704 KB |
Output is correct |
24 |
Correct |
1 ms |
976 KB |
Output is correct |
25 |
Correct |
1 ms |
976 KB |
Output is correct |
26 |
Correct |
1 ms |
976 KB |
Output is correct |
27 |
Correct |
1 ms |
976 KB |
Output is correct |
28 |
Correct |
1 ms |
976 KB |
Output is correct |
29 |
Correct |
1 ms |
976 KB |
Output is correct |
30 |
Correct |
1 ms |
976 KB |
Output is correct |
31 |
Correct |
1 ms |
592 KB |
Output is correct |
32 |
Correct |
1 ms |
976 KB |
Output is correct |
33 |
Correct |
1 ms |
976 KB |
Output is correct |
34 |
Correct |
1 ms |
960 KB |
Output is correct |
35 |
Correct |
1 ms |
976 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
0 ms |
336 KB |
Output is correct |
4 |
Correct |
0 ms |
324 KB |
Output is correct |
5 |
Correct |
0 ms |
328 KB |
Output is correct |
6 |
Correct |
0 ms |
316 KB |
Output is correct |
7 |
Correct |
0 ms |
336 KB |
Output is correct |
8 |
Correct |
0 ms |
336 KB |
Output is correct |
9 |
Correct |
0 ms |
336 KB |
Output is correct |
10 |
Correct |
0 ms |
324 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
0 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
832 KB |
Output is correct |
16 |
Correct |
1 ms |
940 KB |
Output is correct |
17 |
Correct |
1 ms |
976 KB |
Output is correct |
18 |
Correct |
1 ms |
708 KB |
Output is correct |
19 |
Correct |
1 ms |
976 KB |
Output is correct |
20 |
Correct |
1 ms |
976 KB |
Output is correct |
21 |
Correct |
1 ms |
976 KB |
Output is correct |
22 |
Correct |
1 ms |
964 KB |
Output is correct |
23 |
Correct |
1 ms |
704 KB |
Output is correct |
24 |
Correct |
1 ms |
976 KB |
Output is correct |
25 |
Correct |
1 ms |
976 KB |
Output is correct |
26 |
Correct |
1 ms |
976 KB |
Output is correct |
27 |
Correct |
1 ms |
976 KB |
Output is correct |
28 |
Correct |
1 ms |
976 KB |
Output is correct |
29 |
Correct |
1 ms |
976 KB |
Output is correct |
30 |
Correct |
1 ms |
976 KB |
Output is correct |
31 |
Correct |
1 ms |
592 KB |
Output is correct |
32 |
Correct |
1 ms |
976 KB |
Output is correct |
33 |
Correct |
1 ms |
976 KB |
Output is correct |
34 |
Correct |
1 ms |
960 KB |
Output is correct |
35 |
Correct |
1 ms |
976 KB |
Output is correct |
36 |
Correct |
106 ms |
42196 KB |
Output is correct |
37 |
Correct |
112 ms |
45400 KB |
Output is correct |
38 |
Correct |
107 ms |
45396 KB |
Output is correct |
39 |
Correct |
123 ms |
45000 KB |
Output is correct |
40 |
Correct |
134 ms |
45396 KB |
Output is correct |
41 |
Correct |
126 ms |
45424 KB |
Output is correct |
42 |
Correct |
118 ms |
45432 KB |
Output is correct |
43 |
Correct |
56 ms |
28356 KB |
Output is correct |
44 |
Correct |
102 ms |
35284 KB |
Output is correct |
45 |
Correct |
136 ms |
45408 KB |
Output is correct |
46 |
Correct |
120 ms |
45432 KB |
Output is correct |
47 |
Correct |
120 ms |
45400 KB |
Output is correct |
48 |
Correct |
121 ms |
45336 KB |
Output is correct |
49 |
Correct |
73 ms |
30300 KB |
Output is correct |
50 |
Correct |
115 ms |
45532 KB |
Output is correct |
51 |
Correct |
116 ms |
45404 KB |
Output is correct |
52 |
Correct |
107 ms |
43732 KB |
Output is correct |
53 |
Correct |
120 ms |
45332 KB |
Output is correct |
54 |
Correct |
88 ms |
45380 KB |
Output is correct |
55 |
Correct |
89 ms |
45396 KB |
Output is correct |
56 |
Correct |
95 ms |
45652 KB |
Output is correct |