# include <bits/stdc++.h>
#define f first
#define s second
#define pb push_back
using namespace std;
const int N=5e5+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;
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
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 |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
460 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
1 ms |
460 KB |
Output is correct |
18 |
Correct |
1 ms |
460 KB |
Output is correct |
19 |
Correct |
1 ms |
460 KB |
Output is correct |
20 |
Correct |
1 ms |
460 KB |
Output is correct |
21 |
Correct |
1 ms |
460 KB |
Output is correct |
22 |
Correct |
1 ms |
460 KB |
Output is correct |
23 |
Correct |
1 ms |
460 KB |
Output is correct |
24 |
Correct |
1 ms |
460 KB |
Output is correct |
25 |
Correct |
1 ms |
460 KB |
Output is correct |
26 |
Correct |
1 ms |
460 KB |
Output is correct |
27 |
Correct |
1 ms |
460 KB |
Output is correct |
28 |
Correct |
1 ms |
460 KB |
Output is correct |
29 |
Correct |
1 ms |
460 KB |
Output is correct |
30 |
Correct |
1 ms |
460 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
460 KB |
Output is correct |
33 |
Correct |
1 ms |
460 KB |
Output is correct |
34 |
Correct |
1 ms |
460 KB |
Output is correct |
35 |
Correct |
1 ms |
460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
460 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
1 ms |
460 KB |
Output is correct |
18 |
Correct |
1 ms |
460 KB |
Output is correct |
19 |
Correct |
1 ms |
460 KB |
Output is correct |
20 |
Correct |
1 ms |
460 KB |
Output is correct |
21 |
Correct |
1 ms |
460 KB |
Output is correct |
22 |
Correct |
1 ms |
460 KB |
Output is correct |
23 |
Correct |
1 ms |
460 KB |
Output is correct |
24 |
Correct |
1 ms |
460 KB |
Output is correct |
25 |
Correct |
1 ms |
460 KB |
Output is correct |
26 |
Correct |
1 ms |
460 KB |
Output is correct |
27 |
Correct |
1 ms |
460 KB |
Output is correct |
28 |
Correct |
1 ms |
460 KB |
Output is correct |
29 |
Correct |
1 ms |
460 KB |
Output is correct |
30 |
Correct |
1 ms |
460 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
460 KB |
Output is correct |
33 |
Correct |
1 ms |
460 KB |
Output is correct |
34 |
Correct |
1 ms |
460 KB |
Output is correct |
35 |
Correct |
1 ms |
460 KB |
Output is correct |
36 |
Correct |
14 ms |
13684 KB |
Output is correct |
37 |
Correct |
15 ms |
14784 KB |
Output is correct |
38 |
Correct |
16 ms |
14684 KB |
Output is correct |
39 |
Correct |
15 ms |
14600 KB |
Output is correct |
40 |
Correct |
15 ms |
14752 KB |
Output is correct |
41 |
Correct |
15 ms |
14684 KB |
Output is correct |
42 |
Correct |
15 ms |
14708 KB |
Output is correct |
43 |
Correct |
9 ms |
9272 KB |
Output is correct |
44 |
Correct |
11 ms |
11484 KB |
Output is correct |
45 |
Correct |
14 ms |
14684 KB |
Output is correct |
46 |
Correct |
14 ms |
14688 KB |
Output is correct |
47 |
Correct |
14 ms |
14684 KB |
Output is correct |
48 |
Correct |
14 ms |
14716 KB |
Output is correct |
49 |
Correct |
10 ms |
9948 KB |
Output is correct |
50 |
Correct |
16 ms |
14648 KB |
Output is correct |
51 |
Correct |
14 ms |
14752 KB |
Output is correct |
52 |
Correct |
14 ms |
14168 KB |
Output is correct |
53 |
Correct |
14 ms |
14684 KB |
Output is correct |
54 |
Correct |
14 ms |
14684 KB |
Output is correct |
55 |
Correct |
15 ms |
14684 KB |
Output is correct |
56 |
Correct |
14 ms |
14704 KB |
Output is correct |