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
#define ld long double
const int N = 1e6 + 5;
int t,n,k,prefo[N],prefj[N],prefi[N],st,l,r,idx,mid,ans;
string s;
signed main() {
ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
cin>>n>>k;
cin>>s;
s="@"+s;
for(int i=1;i<=n;i++){
prefj[i]=prefj[i-1]+(s[i]=='J');
prefo[i]=prefo[i-1]+(s[i]=='O');
prefi[i]=prefi[i-1]+(s[i]=='I');
}
ans=1e9;
for (int i=1;i<=n;i++) {
st=i;
l=i; r=n;
idx=-1;
while(l<=r){
mid=(l+r)/2;
if(prefj[mid]-prefj[st-1]>=k){
idx=mid;
r=mid-1;
}
else{
l=mid+1;
}
}
if (idx==-1) break;
st=idx+1;
l=st; r=n;
idx=-1;
while(l<=r){
mid=(l+r)/2;
if(prefo[mid]-prefo[st-1]>=k){
idx=mid;
r=mid-1;
}
else{
l=mid+1;
}
}
if(idx==-1) break;
st=idx+1;
l=st; r=n;
idx=-1;
while(l<=r){
mid=(l+r)/2;
if(prefi[mid]-prefi[st-1]>=k){
idx=mid;
r=mid-1;
}
else{
l=mid+1;
}
}
if(idx==-1) break;
ans=min(ans, idx-i+1-3*k);
}
if (ans==1e9) 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... |