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>
#define ll long long
using namespace std;
const ll N = 5e5+5, oo = 1e18+10;
ll n, k, cnt[N];
string s;
ll sum(ll l, ll r) {
return cnt[r] - cnt[l - 1];
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
cin >> n >> k >> s;
vector<int> I, J;
for(int i = 0; i < n; ++i) {
if(s[i] == 'J') {
J.push_back(i);
}
if(s[i] == 'I') {
I.push_back(i);
}
cnt[i] = cnt[i - 1] + (s[i] == 'O');
}
ll ans = oo;
for(int i = k - 1, j = 0; i < (int)J.size(); ++i) {
while(j+k <= (int)I.size() && (I[j] < J[i] || sum(J[i], I[j]) < k)) ++j;
if(j+k <= (int)I.size() && sum(J[i], I[j]) >= k) {
ll lft = J[i] - J[i - k + 1] + 1 - k;
ll rgh = I[j+k-1] - I[j] + 1 - k;
ll mid = I[j] - J[i] - 1 - k;
ans = min(ans, lft + mid + rgh);
}
}
if(ans == oo) {
ans = -1;
}
cout << ans << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |