#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 |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2392 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2392 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2392 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2392 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
0 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
2392 KB |
Output is correct |
26 |
Correct |
1 ms |
2392 KB |
Output is correct |
27 |
Correct |
1 ms |
2396 KB |
Output is correct |
28 |
Correct |
1 ms |
2396 KB |
Output is correct |
29 |
Correct |
1 ms |
2396 KB |
Output is correct |
30 |
Correct |
1 ms |
2396 KB |
Output is correct |
31 |
Correct |
0 ms |
2396 KB |
Output is correct |
32 |
Correct |
1 ms |
2396 KB |
Output is correct |
33 |
Correct |
0 ms |
2396 KB |
Output is correct |
34 |
Correct |
1 ms |
2396 KB |
Output is correct |
35 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2392 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2392 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2392 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
0 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
2392 KB |
Output is correct |
26 |
Correct |
1 ms |
2392 KB |
Output is correct |
27 |
Correct |
1 ms |
2396 KB |
Output is correct |
28 |
Correct |
1 ms |
2396 KB |
Output is correct |
29 |
Correct |
1 ms |
2396 KB |
Output is correct |
30 |
Correct |
1 ms |
2396 KB |
Output is correct |
31 |
Correct |
0 ms |
2396 KB |
Output is correct |
32 |
Correct |
1 ms |
2396 KB |
Output is correct |
33 |
Correct |
0 ms |
2396 KB |
Output is correct |
34 |
Correct |
1 ms |
2396 KB |
Output is correct |
35 |
Correct |
1 ms |
2396 KB |
Output is correct |
36 |
Correct |
4 ms |
2864 KB |
Output is correct |
37 |
Correct |
7 ms |
3048 KB |
Output is correct |
38 |
Correct |
5 ms |
3048 KB |
Output is correct |
39 |
Correct |
5 ms |
3048 KB |
Output is correct |
40 |
Correct |
6 ms |
3048 KB |
Output is correct |
41 |
Correct |
5 ms |
3044 KB |
Output is correct |
42 |
Correct |
6 ms |
2972 KB |
Output is correct |
43 |
Correct |
3 ms |
2924 KB |
Output is correct |
44 |
Correct |
4 ms |
2792 KB |
Output is correct |
45 |
Correct |
6 ms |
3048 KB |
Output is correct |
46 |
Correct |
6 ms |
3048 KB |
Output is correct |
47 |
Correct |
5 ms |
3048 KB |
Output is correct |
48 |
Correct |
5 ms |
2968 KB |
Output is correct |
49 |
Correct |
3 ms |
2792 KB |
Output is correct |
50 |
Correct |
5 ms |
2980 KB |
Output is correct |
51 |
Correct |
5 ms |
3048 KB |
Output is correct |
52 |
Correct |
6 ms |
3048 KB |
Output is correct |
53 |
Correct |
5 ms |
3048 KB |
Output is correct |
54 |
Correct |
4 ms |
3048 KB |
Output is correct |
55 |
Correct |
3 ms |
3048 KB |
Output is correct |
56 |
Correct |
3 ms |
3304 KB |
Output is correct |