This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "bits/stdc++.h"
using namespace std;
const int maxn = 2e5 + 5;
int n, k, a[maxn], pref[maxn], suf[maxn];
int ps[maxn];
string s;
void solve() {
cin >> n >> k >> s;
s = ' ' + s;
vector<int> lst;
for(int i = 1; i <= n; ++i) {
ps[i] = ps[i - 1] + (s[i] == 'O');
if(s[i] != 'J') continue;
lst.push_back(i);
if((int)lst.size() < k) continue;
int pos = lst[(int)lst.size() - k];
pref[i] = (i - pos - k + 1);
}
lst.clear();
memset(suf, 0x3f, sizeof(suf));
for(int i = n; i; --i) {
if(s[i] != 'I') continue;
lst.push_back(i);
if((int)lst.size() < k) continue;
int pos = lst[(int)lst.size() - k];
suf[i] = (pos - i - k + 1);
}
for(int i = n - 1; i; --i) {
suf[i] = min(suf[i + 1], suf[i]);
}
int ans = 1e9;
for(int i = 1; i <= n; ++i) {
if(s[i] != 'J') continue;
int x = lower_bound(ps + 1, ps + n + 1, ps[i] + k) - ps;
if(x > n) continue;
ans = min(ans, (x - i - k) + pref[i] + suf[x + 1]);
}
cout << (ans == 1e9 ? -1 : ans);
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
solve();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |