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 pii pair<int,int>
#define ff first
#define ss second
#define all(x) x.begin(),x.end()
#define pb push_back
#define N 200010
int ans=1e9;
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
int n,k,i,j,a,b,c,d,e,f,o;
cin>>n>>k;
string s;
cin>>s;
for(a=-1,b=-1,c=-1,d=-1,e=-1,f=-1,i=0,j=0,o=0;;){
while(b<n&&j<k){
if(s[++b]=='J')j++;
}
while(c<=b){
if(c>=0&&s[c]=='O')o--;
c++;
}
while(d<n&&o<k){
if(s[++d]=='O')o++;
}
while(e<=d){
if(e>=0&&s[e]=='I')i--;
e++;
}
while(f<n&&i<k){
if(s[++f]=='I')i++;
}
if(f==n)break;
ans=min(f+d+b-e-c-a-3*k+3,ans);
if(a>=0&&s[a]=='J')j--;
a++;
}
cout<<(ans==1e9?-1:ans)<<'\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |