#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 lst[3][N], prv[N];
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>> wh(3, vector<int>(n + 1, -1)), pref(3, vector<int>(n, 0));
vector<int> cnt(3);
for (int i = 0; i < n; i++) {
wh[a[i]][++cnt[a[i]]] = i;
pref[a[i]][i]++;
if (i > 0) for (int j = 0; j < 3; j++) pref[j][i] += pref[j][i - 1];
}
auto Get = [&] (int l, int r, int o) {
if (l > r) return 0;
int ans = pref[o][r];
if (l > 0) ans -= pref[o][l - 1];
return ans;
};
if (wh[0][k] == -1 || wh[1][k] == -1 || wh[2][k] == -1) {
cout << -1 << en;
return 0;
}
int ans = 1e9;
int l = k; int r = cnt[2] - k + 1;
while (wh[0][l] < wh[2][r]) {
if (Get(wh[0][l] + 1, wh[2][r] - 1, 1) >= k) smin(ans, wh[2][r + k - 1] - wh[0][l - k + 1] + 1 - 3 * k);
if (l == cnt[0] && r == 1) break;
if (Get(wh[0][l + 1] + 1, wh[2][r] - 1, 1) > Get(wh[0][l] + 1, wh[2][r - 1] - 1, 1) && l < cnt[0]) l++;
else if (r < cnt[2]) r--;
}
if (ans == 1e9) ans = -1;
cout << ans << en;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2073 ms |
212 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2073 ms |
212 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2073 ms |
212 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |