#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<int> wh(n);
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[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[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) smin(ans, wh[r] - wh[l] + 1 - 3 * k);
if (Get(l + 1, r) > Get(l, r - 1)) l++;
else r--;
}
if (ans == 1e9) ans = -1;
cout << ans << en;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |