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>
#define f first
#define s second
#define pb push_back
using namespace std;
const int N=3e5+5;
long long n,k,cntj,cnto,cnti,prj[N],pri[N],pro[N],ans,ans1,nxt,xx,le,ri,le1,ri1,le2,ri2;
string s;
long long mpj[N],mpi[N],mpo[N],idx,pr;
int main() {
cin>>n>>k;
cin>>s;
for (int i=0; i<=2*n; i++) {
mpj[i]=-1;
mpo[i]=-1;
mpi[i]=-1;
}
for (int i=0; i<s.size(); i++) {
if (s[i]=='J') cntj++, mpj[cntj]=i;
if (s[i]=='O') cnto++, mpo[cnto]=i;
if (s[i]=='I') cnti++, mpi[cnti]=i;
prj[i]=cntj;
pro[i]=cnto;
pri[i]=cnti;
}
ans=1e9;
for (int le=k; le<=n-k-1; le++) {
idx=le;
xx=pro[le-1];
nxt=k+pro[le-1];
ri=mpo[nxt];
if (-1==mpo[nxt]) continue;
ans1=(ri-le+1)-k;
ri1=le-1;
xx=prj[ri1];
pr=xx-k+1;
if (pr<=0) continue;
// cout<<pr<<endl;
le1=mpj[pr];
if (-1==mpj[pr]) continue;
ans1+=(ri1-le1+1)-k;
le2=ri+1;
nxt=pri[ri]+k;
ri2=mpi[nxt];
if (-1==mpi[nxt]) continue;
ans1+=(ri2-le2+1)-k;
ans=min(ans,ans1);
}if (ans==1e9) cout<<-1<<endl;
else
cout<<ans<<endl;
}
Compilation message (stderr)
ho_t2.cpp: In function 'int main()':
ho_t2.cpp:18:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
18 | for (int i=0; i<s.size(); i++) {
| ~^~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |