#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) {
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 |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
0 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
0 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
844 KB |
Output is correct |
16 |
Correct |
1 ms |
972 KB |
Output is correct |
17 |
Correct |
1 ms |
972 KB |
Output is correct |
18 |
Correct |
1 ms |
716 KB |
Output is correct |
19 |
Correct |
1 ms |
972 KB |
Output is correct |
20 |
Correct |
1 ms |
972 KB |
Output is correct |
21 |
Correct |
1 ms |
972 KB |
Output is correct |
22 |
Correct |
1 ms |
972 KB |
Output is correct |
23 |
Correct |
1 ms |
588 KB |
Output is correct |
24 |
Correct |
1 ms |
972 KB |
Output is correct |
25 |
Correct |
1 ms |
972 KB |
Output is correct |
26 |
Correct |
1 ms |
972 KB |
Output is correct |
27 |
Correct |
1 ms |
972 KB |
Output is correct |
28 |
Correct |
1 ms |
972 KB |
Output is correct |
29 |
Correct |
1 ms |
972 KB |
Output is correct |
30 |
Correct |
1 ms |
972 KB |
Output is correct |
31 |
Correct |
1 ms |
588 KB |
Output is correct |
32 |
Correct |
1 ms |
972 KB |
Output is correct |
33 |
Correct |
1 ms |
972 KB |
Output is correct |
34 |
Correct |
1 ms |
972 KB |
Output is correct |
35 |
Correct |
1 ms |
972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
0 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
844 KB |
Output is correct |
16 |
Correct |
1 ms |
972 KB |
Output is correct |
17 |
Correct |
1 ms |
972 KB |
Output is correct |
18 |
Correct |
1 ms |
716 KB |
Output is correct |
19 |
Correct |
1 ms |
972 KB |
Output is correct |
20 |
Correct |
1 ms |
972 KB |
Output is correct |
21 |
Correct |
1 ms |
972 KB |
Output is correct |
22 |
Correct |
1 ms |
972 KB |
Output is correct |
23 |
Correct |
1 ms |
588 KB |
Output is correct |
24 |
Correct |
1 ms |
972 KB |
Output is correct |
25 |
Correct |
1 ms |
972 KB |
Output is correct |
26 |
Correct |
1 ms |
972 KB |
Output is correct |
27 |
Correct |
1 ms |
972 KB |
Output is correct |
28 |
Correct |
1 ms |
972 KB |
Output is correct |
29 |
Correct |
1 ms |
972 KB |
Output is correct |
30 |
Correct |
1 ms |
972 KB |
Output is correct |
31 |
Correct |
1 ms |
588 KB |
Output is correct |
32 |
Correct |
1 ms |
972 KB |
Output is correct |
33 |
Correct |
1 ms |
972 KB |
Output is correct |
34 |
Correct |
1 ms |
972 KB |
Output is correct |
35 |
Correct |
1 ms |
972 KB |
Output is correct |
36 |
Correct |
105 ms |
42332 KB |
Output is correct |
37 |
Correct |
104 ms |
45188 KB |
Output is correct |
38 |
Correct |
105 ms |
45224 KB |
Output is correct |
39 |
Correct |
116 ms |
44792 KB |
Output is correct |
40 |
Correct |
114 ms |
45200 KB |
Output is correct |
41 |
Correct |
115 ms |
45212 KB |
Output is correct |
42 |
Correct |
120 ms |
45224 KB |
Output is correct |
43 |
Correct |
57 ms |
28364 KB |
Output is correct |
44 |
Correct |
80 ms |
35160 KB |
Output is correct |
45 |
Correct |
114 ms |
45200 KB |
Output is correct |
46 |
Correct |
123 ms |
45264 KB |
Output is correct |
47 |
Correct |
115 ms |
45228 KB |
Output is correct |
48 |
Correct |
114 ms |
45240 KB |
Output is correct |
49 |
Correct |
62 ms |
30288 KB |
Output is correct |
50 |
Correct |
113 ms |
45340 KB |
Output is correct |
51 |
Correct |
118 ms |
45164 KB |
Output is correct |
52 |
Correct |
108 ms |
43488 KB |
Output is correct |
53 |
Correct |
112 ms |
45228 KB |
Output is correct |
54 |
Correct |
89 ms |
45236 KB |
Output is correct |
55 |
Correct |
89 ms |
45228 KB |
Output is correct |
56 |
Correct |
92 ms |
45224 KB |
Output is correct |