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;
#define int long long
const int N=5005, INF=1e18;
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, k; cin >> n >> k;
vector<vector<int>> pref(n, vector<int>(n, 0));
string s; cin >> s;
for(int i =0; i<n; i++){
if(s[i]=='J') pref[0][i]++;
else if(s[i]=='O') pref[1][i]++;
else pref[2][i]++;
if(i>0){
pref[0][i]+=pref[0][i-1];
pref[1][i]+=pref[1][i-1];
pref[2][i]+=pref[2][i-1];
}
}
auto val=[&](int l, int r, int type){
if(l>r) return 0LL;
if(r==n) return 0LL;
if(l==0) return pref[type][r];
return pref[type][r]-pref[type][l-1];
};
int posj=0, poso=0, posi=0, ans=n+1;
for(int i=0; i<n; i++){
while(posj<n&&val(i, posj, 0)<k) posj++;
while(poso<n&&val(posj+1, poso, 1)<k) poso++;
while(posi<n&&val(poso+1, posi, 2)<k) posi++;
if(val(i, posj, 0)==k && val(posj+1, poso, 1)==k && val(poso+1, posi, 2)==k){
ans=min(ans, posi-i+1-k*3);
}
}
if(ans==n+1) cout<<-1;
else cout<<ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |