#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 2e5 + 2;
const int mod = 1e9 + 7;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, k;
cin >> n >> k;
string s; cin >> s;
vector<int> a(n);
for (int i = 0; i < n; i++) {
if (s[i] == 'J') a[i] = 0;
if (s[i] == 'O') a[i] = 1;
if (s[i] == 'I') a[i] = 2;
}
vector<vector<int>> pref(3, vector<int>(n));
vector<vector<int>> pom(3, vector<int>(n + 1, -1));
vector<vector<int>> wh(3, vector<int>(n + 1, -1));
for (int i = 0; i < n; i++) {
if (i > 0) {
pref[0][i] += pref[0][i - 1];
}
if (a[i] != 0) continue;
pom[0][++pref[0][i]] = i;
if (pref[0][i] >= k) wh[0][i] = pom[0][pref[0][i] - k + 1];
}
for (int i = n - 1; i >= 0; i--) {
if (i < n - 1) {
pref[2][i] += pref[2][i + 1];
}
if (a[i] != 2) continue;
pom[2][++pref[2][i]] = i;
if (pref[2][i] >= k) wh[2][i] = pom[2][pref[2][i] - k + 1];
}
for (int i = 0; i < n; i++) {
if (i > 0) pref[1][i] += pref[1][i - 1];
if (a[i] == 1) pref[1][i]++;
}
auto Get = [&] (int l, int r) {
if (l > r) return 0;
int o = pref[1][r];
if (l > 0) o -= pref[1][l - 1];
return o;
};
if (pref[0].back() < k || pref[2][0] < k) {
cout << -1 << en;
return 0;
}
int l = 0;
while (l < n && pref[0][l] < k) l++;
int r = n - 1;
while (r >= 0 && pref[2][r] < k) r--;
int ans = 1e9;
while (l < r) {
if (Get(l + 1, r - 1) >= k && s[r] == 'I' && s[l] == 'J') smin(ans, wh[2][r] - wh[0][l] + 1 - 3 * k);
else if (Get(l + 1, r - 1) < k) break;
if (Get(l + 2, r - 1) > Get(l + 1, r - 2)) l++;
else r--;
}
if (ans == 1e9) ans = -1;
cout << ans << en;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |