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=200000+10;
int n,k;
string s;
int dpj[maxn],dpo[maxn],dpi[maxn];
int cal(int ind){
if(dpj[ind]==-1||dpo[dpj[ind]]==-1){
return -1;
}
return dpi[dpo[dpj[ind]]];
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>k;
cin>>s;
deque<int>v;
for(int i=n-1;i>=0;i--){
if(s[i]=='J'){
v.push_front(i);
}
if((int)v.size()>k){
v.pop_back();
}
if((int)v.size()==k){
dpj[i]=v.back();
}
else{
dpj[i]=-1;
}
}
v.clear();
for(int i=n-1;i>=0;i--){
if(s[i]=='O'){
v.push_front(i);
}
if((int)v.size()>k){
v.pop_back();
}
if((int)v.size()==k){
dpo[i]=v.back();
}
else{
dpo[i]=-1;
}
}
v.clear();
for(int i=n-1;i>=0;i--){
if(s[i]=='I'){
v.push_front(i);
}
if((int)v.size()>k){
v.pop_back();
}
if((int)v.size()==k){
dpi[i]=v.back();
}
else{
dpi[i]=-1;
}
}
int f=0,res=n+2;
for(int i=0;i<n;i++){
int z=cal(i);
if(z!=-1){
// cout<<i<<" "<<z<<"\n";
f=1;
res=min(res,z-i+1);
}
}
if(f==0){
cout<<-1<<"\n";
}
else{
res-=3*k;
cout<<res<<"\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... |