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 all(fl) fl.begin(),fl.end()
#define pb push_back
#define fi first
#define se second
#define for1(i,j,k) for(int i=j;i<=k;i++)
#define for2(i,j,k) for(int i=j;i>=k;i--)
#define for3(i,j,k,l) for(int i=j;i<=k;i+=l)
#define lb lower_bound
#define ub upper_bound
#define sz(a) (int)a.size()
#define pii pair<int,int>
#define pli pair<long long,int>
#define gcd __gcd
#define lcm(x,y) x*y/__gcd(x,y)
#define pil pair<int,long long>
vector<int>pos[3];
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
//freopen("temp.INP","r",stdin);
//freopen("temp.OUT","w",stdout);
int n,k;
cin>>n>>k;
string s;
cin>>s;
s=" "+s;
int ans=n+1;
for1(i,1,n){
if (s[i]=='J')pos[0].pb(i);
if (s[i]=='O')pos[1].pb(i);
if (s[i]=='I')pos[2].pb(i);
}
for1(i,1,n){
if (s[i]=='J'){
int id=lower_bound(pos[0].begin(),pos[0].end(),i)-pos[0].begin();
if (id+k-1>=sz(pos[0]))continue;
int l1=i,r1=pos[0][id+k-1];
//if (i==2)cout<<l1<<" "<<r1<<'\n';
int id2=upper_bound(pos[1].begin(),pos[1].end(),r1)-pos[1].begin();
if (id2==sz(pos[1])||id2+k-1>=sz(pos[1]))continue;
int l2=pos[1][id2],r2=pos[1][id2+k-1];
//if (i==2)cout<<l2<<" "<<r2<<'\n';
int id3=upper_bound(pos[2].begin(),pos[2].end(),r2)-pos[2].begin();
if (id3==sz(pos[2])||id3+k-1>=sz(pos[2]))continue;
int l3=pos[2][id3],r3=pos[2][id3+k-1];
//if (i==2)cout<<l3<<" "<<r3<<'\n';
ans=min(ans,r3-l1+1-3*k);
}
}
if (ans>n)ans=-1;
cout<<ans;
}
Compilation message (stderr)
ho_t2.cpp: In function 'int main()':
ho_t2.cpp:43:13: warning: unused variable 'l2' [-Wunused-variable]
43 | int l2=pos[1][id2],r2=pos[1][id2+k-1];
| ^~
ho_t2.cpp:47:13: warning: unused variable 'l3' [-Wunused-variable]
47 | int l3=pos[2][id3],r3=pos[2][id3+k-1];
| ^~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |