#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 >= 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 |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Incorrect |
1 ms |
328 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Incorrect |
1 ms |
328 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Incorrect |
1 ms |
328 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |