//#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define X first
#define Y second
int n;int K;
string s;
int psj[200005];
int pso[200005];
int psi[200005];
int dpj[200005];
int dpi[200005];
signed main(){
cin>>n>>K>>s;
s="*"+s;
for(int i=1;i<=n;i++){
psj[i]=psj[i-1];
pso[i]=pso[i-1];
psi[i]=psi[i-1];
if(s[i]=='I'){
psi[i]++;
}
if(s[i]=='O'){
pso[i]++;
}
if(s[i]=='J'){
psj[i]++;
}
}
for(int i=1;i<=n;i++){
dpj[i]=1e16;
if(s[i]!='J'){continue;}
if(psj[i]<K){continue;}
int l=0;int r=i;
while(l<r){
int mi=l+(r-l+1)/2;
if(mi==0||(mi!=0&&psj[i]-psj[mi-1]>=K)){
l=mi;
}else{
r=mi-1;
}
}
if(l!=0){
dpj[i]=i-l+1-K;
}
}
for(int i=1;i<=n;i++){
dpi[i]=1e16;
if(s[i]!='I'){continue;}
if(psi[n]-psi[i-1]<K){continue;}
int l=i+K-1;int r=n+1;
while(l<r){
int mi=l+(r-l)/2;
if(mi==n+1||(mi!=n+1&&psi[mi]-psi[i-1]>=K)){
r=mi;
}else{
l=mi+1;
}
}
if(l!=n+1){
dpi[i]=l-i+1-K;
}
}
dpj[0]=1e16;
int ans=1e16;
int mnv=1e16;
int lit=0;
for(int i=1;i<=n;i++){
if(dpi[i]<=n&&pso[i-1]>=K){
while(lit+1<=n&&pso[i-1]-pso[lit+1]>=K){
lit++;
mnv=min(mnv,dpj[lit]-lit);
}
ans=min(ans,dpi[i]+i-1+mnv-K);
}
}
/*
for(int i=1;i<=n;i++){
if(dpj[i]>n){dpj[i]=-1;}
if(dpi[i]>n){dpi[i]=-1;}
cout<<dpj[i]<<" ";
}cout<<endl;
for(int i=1;i<=n;i++){
cout<<dpi[i]<<" ";
}*/
if(ans>n){
cout<<-1<<endl;return 0;
}
cout<<ans<<endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
312 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
312 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
432 KB |
Output is correct |
23 |
Correct |
1 ms |
308 KB |
Output is correct |
24 |
Correct |
1 ms |
440 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
436 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
436 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
312 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
432 KB |
Output is correct |
23 |
Correct |
1 ms |
308 KB |
Output is correct |
24 |
Correct |
1 ms |
440 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
436 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
436 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
17 ms |
8044 KB |
Output is correct |
37 |
Correct |
18 ms |
8552 KB |
Output is correct |
38 |
Correct |
21 ms |
8632 KB |
Output is correct |
39 |
Correct |
35 ms |
8472 KB |
Output is correct |
40 |
Correct |
18 ms |
8584 KB |
Output is correct |
41 |
Correct |
19 ms |
8588 KB |
Output is correct |
42 |
Correct |
19 ms |
8608 KB |
Output is correct |
43 |
Correct |
10 ms |
5324 KB |
Output is correct |
44 |
Correct |
13 ms |
6708 KB |
Output is correct |
45 |
Correct |
17 ms |
8608 KB |
Output is correct |
46 |
Correct |
17 ms |
8636 KB |
Output is correct |
47 |
Correct |
17 ms |
8572 KB |
Output is correct |
48 |
Correct |
17 ms |
8612 KB |
Output is correct |
49 |
Correct |
12 ms |
5840 KB |
Output is correct |
50 |
Correct |
20 ms |
8600 KB |
Output is correct |
51 |
Correct |
17 ms |
8608 KB |
Output is correct |
52 |
Correct |
11 ms |
8344 KB |
Output is correct |
53 |
Correct |
13 ms |
8544 KB |
Output is correct |
54 |
Correct |
15 ms |
8576 KB |
Output is correct |
55 |
Correct |
12 ms |
8608 KB |
Output is correct |
56 |
Correct |
8 ms |
8608 KB |
Output is correct |