#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> pii;
const int MXN = 200005;
int n, k, cid[256], ID[3][MXN], pid[3], jx[MXN], ix[MXN], ans = LLONG_MAX;
string s;
void init() {
cid['J'] = 0;
cid['O'] = 1;
cid['I'] = 2;
}
void GETX() {
ID[0][pid[0]] = LLONG_MAX;
ID[2][pid[2]] = LLONG_MAX;
int j = 0, i = 0;
for (int o = 0; o < pid[1]; o++) {
while (ID[0][j] < ID[1][o]) j++;
jx[o] = j - 1;
while (ID[2][i] < ID[1][o]) i++;
ix[o] = i;
}
}
int f(int from, int to, int space) {
return to - from - space + 1;
}
int solve() {
cin >> n >> k >> s;
for (int i = 0; i < n; i++) {
ID[cid[s[i]]][pid[cid[s[i]]]] = i;
pid[cid[s[i]]]++;
}
for (int i = 0; i < 3; i++) if (pid[i] < k) return -1;
// for (int i = 0; i < 3; i++) {
// for (int j = 0; j < pid[i]; j++) {
// cout << ID[i][j] << ' ';
// }
// cout << endl;
// }
GETX();
// for (int i = 0; i < pid[1]; i++) {
// cout << jx[i] << ' ' << ix[i] << endl;
// }
for (int i = 0; i <= pid[1] - k; i++) {
if (jx[i] - k + 1 < 0) continue;
if (ix[i + k - 1] + k - 1 >= pid[2]) continue;
// cout << i << " PRING" << endl;
ans = min(ans, f(ID[1][i], ID[1][i + k - 1], k) + f(ID[0][jx[i] - k + 1], ID[1][i], k + 1) + f(ID[1][i + k - 1], ID[2][ix[i + k - 1] + k - 1], k + 1));
}
if (ans == LLONG_MAX) return -1;
return ans;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
init();
cout << solve() << endl;
return 0;
}
Compilation message
ho_t2.cpp: In function 'long long int solve()':
ho_t2.cpp:36:20: warning: array subscript has type 'char' [-Wchar-subscripts]
36 | ID[cid[s[i]]][pid[cid[s[i]]]] = i;
| ^
ho_t2.cpp:36:35: warning: array subscript has type 'char' [-Wchar-subscripts]
36 | ID[cid[s[i]]][pid[cid[s[i]]]] = i;
| ^
ho_t2.cpp:37:21: warning: array subscript has type 'char' [-Wchar-subscripts]
37 | pid[cid[s[i]]]++;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
328 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
328 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
328 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 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 |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
328 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
328 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
328 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 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 |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
328 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
5 ms |
3172 KB |
Output is correct |
37 |
Correct |
6 ms |
3424 KB |
Output is correct |
38 |
Correct |
6 ms |
3428 KB |
Output is correct |
39 |
Correct |
6 ms |
3424 KB |
Output is correct |
40 |
Correct |
6 ms |
3424 KB |
Output is correct |
41 |
Correct |
5 ms |
3424 KB |
Output is correct |
42 |
Correct |
6 ms |
3424 KB |
Output is correct |
43 |
Correct |
3 ms |
2260 KB |
Output is correct |
44 |
Correct |
5 ms |
2784 KB |
Output is correct |
45 |
Correct |
6 ms |
3424 KB |
Output is correct |
46 |
Correct |
5 ms |
3424 KB |
Output is correct |
47 |
Correct |
6 ms |
3376 KB |
Output is correct |
48 |
Correct |
6 ms |
3448 KB |
Output is correct |
49 |
Correct |
4 ms |
2404 KB |
Output is correct |
50 |
Correct |
5 ms |
3460 KB |
Output is correct |
51 |
Correct |
6 ms |
3424 KB |
Output is correct |
52 |
Correct |
5 ms |
3316 KB |
Output is correct |
53 |
Correct |
6 ms |
3404 KB |
Output is correct |
54 |
Correct |
5 ms |
3404 KB |
Output is correct |
55 |
Correct |
5 ms |
3428 KB |
Output is correct |
56 |
Correct |
5 ms |
3408 KB |
Output is correct |