#include <bits/stdc++.h>
using namespace std;
int main(){
int n,k;
cin>>n>>k;
string s;
cin>>s;
s = 'K'+s;
vector<int> jpos,ipos;
int oct[n]; oct[0] = 0;
int mpj[n], mpi[n];
memset(mpj,-1,sizeof(mpj)); memset(mpi,-1,sizeof(mpi));
for(int i=1; i<=n; i++){
if(s[i]=='J'){ jpos.push_back(i); mpj[i] = jpos.size()-1;}
if(s[i]=='I'){ ipos.push_back(i); mpi[i] = ipos.size()-1;}
if(s[i]=='O') oct[i]=oct[i-1]+1;
else oct[i]=oct[i-1];
}
int min_span = 1e9;
int jsz=jpos.size(),isz=ipos.size();
int j_r = jpos[jsz-1], j_l = jpos[jsz-k];
int i_l=-1,i_r=-1;
for(int i=j_r+1; i<=n; i++){
if(oct[i-1]-oct[j_r]>=k && s[i]=='I'){
i_l = i; break;
}
}
if(i_l != -1){
if(mpi[i_l] + k <= isz) i_r = ipos[mpi[i_l]+k-1];
}
if(i_r!=-1) min_span = min(min_span, i_r-j_l+1);
//cout<<min_span<<endl;
for(int i=jsz-k-1; i>=0; i--){
j_l = jpos[i]; j_r = jpos[i+k-1];
//cout<<j_l<<' '<<j_r<<endl;
if(i_l==-1){
if(oct[ipos[isz-1]-1] - oct[j_r] >= k) i_l=ipos[isz-1];
else continue;
}
int ival=0;
for(int v=i_l; v>0; v--){
if(oct[v-1] - oct[j_r] < k){ ival=v; break;}
}
for(int v=ival+1; v<=n; v++){
if(s[v] == 'I'){ i_l = v; break;}
}
if(i_l != -1){
if(mpi[i_l] + k <= isz) i_r = ipos[mpi[i_l]+k-1];
}
//cout<<i_l<<' '<<i_r<<endl;
//cout<<mpi[i_l]<<endl;
if(i_r!=-1) min_span = min(min_span, i_r-j_l+1);
//cout<<min_span<<endl;
}
//cout<<min_span<<endl;
if(min_span==1e9) cout<<-1;
else cout<<(min_span-(3*k));
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
256 KB |
Output is correct |
2 |
Correct |
5 ms |
256 KB |
Output is correct |
3 |
Correct |
5 ms |
256 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
6 ms |
256 KB |
Output is correct |
6 |
Correct |
5 ms |
256 KB |
Output is correct |
7 |
Correct |
5 ms |
256 KB |
Output is correct |
8 |
Correct |
4 ms |
256 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
256 KB |
Output is correct |
12 |
Correct |
5 ms |
256 KB |
Output is correct |
13 |
Correct |
5 ms |
256 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
256 KB |
Output is correct |
2 |
Correct |
5 ms |
256 KB |
Output is correct |
3 |
Correct |
5 ms |
256 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
6 ms |
256 KB |
Output is correct |
6 |
Correct |
5 ms |
256 KB |
Output is correct |
7 |
Correct |
5 ms |
256 KB |
Output is correct |
8 |
Correct |
4 ms |
256 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
256 KB |
Output is correct |
12 |
Correct |
5 ms |
256 KB |
Output is correct |
13 |
Correct |
5 ms |
256 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
6 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
6 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
26 |
Correct |
7 ms |
384 KB |
Output is correct |
27 |
Correct |
5 ms |
372 KB |
Output is correct |
28 |
Correct |
5 ms |
384 KB |
Output is correct |
29 |
Correct |
5 ms |
384 KB |
Output is correct |
30 |
Correct |
5 ms |
384 KB |
Output is correct |
31 |
Correct |
5 ms |
384 KB |
Output is correct |
32 |
Correct |
5 ms |
384 KB |
Output is correct |
33 |
Correct |
6 ms |
384 KB |
Output is correct |
34 |
Correct |
6 ms |
384 KB |
Output is correct |
35 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
256 KB |
Output is correct |
2 |
Correct |
5 ms |
256 KB |
Output is correct |
3 |
Correct |
5 ms |
256 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
6 ms |
256 KB |
Output is correct |
6 |
Correct |
5 ms |
256 KB |
Output is correct |
7 |
Correct |
5 ms |
256 KB |
Output is correct |
8 |
Correct |
4 ms |
256 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
256 KB |
Output is correct |
12 |
Correct |
5 ms |
256 KB |
Output is correct |
13 |
Correct |
5 ms |
256 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
6 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
6 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
26 |
Correct |
7 ms |
384 KB |
Output is correct |
27 |
Correct |
5 ms |
372 KB |
Output is correct |
28 |
Correct |
5 ms |
384 KB |
Output is correct |
29 |
Correct |
5 ms |
384 KB |
Output is correct |
30 |
Correct |
5 ms |
384 KB |
Output is correct |
31 |
Correct |
5 ms |
384 KB |
Output is correct |
32 |
Correct |
5 ms |
384 KB |
Output is correct |
33 |
Correct |
6 ms |
384 KB |
Output is correct |
34 |
Correct |
6 ms |
384 KB |
Output is correct |
35 |
Correct |
5 ms |
384 KB |
Output is correct |
36 |
Correct |
23 ms |
3664 KB |
Output is correct |
37 |
Correct |
20 ms |
3928 KB |
Output is correct |
38 |
Correct |
20 ms |
3916 KB |
Output is correct |
39 |
Correct |
20 ms |
3916 KB |
Output is correct |
40 |
Correct |
19 ms |
4044 KB |
Output is correct |
41 |
Correct |
20 ms |
4044 KB |
Output is correct |
42 |
Correct |
21 ms |
4044 KB |
Output is correct |
43 |
Correct |
14 ms |
2708 KB |
Output is correct |
44 |
Correct |
15 ms |
2936 KB |
Output is correct |
45 |
Correct |
19 ms |
4044 KB |
Output is correct |
46 |
Correct |
19 ms |
3936 KB |
Output is correct |
47 |
Correct |
19 ms |
4172 KB |
Output is correct |
48 |
Correct |
19 ms |
3908 KB |
Output is correct |
49 |
Correct |
14 ms |
2828 KB |
Output is correct |
50 |
Correct |
19 ms |
3916 KB |
Output is correct |
51 |
Correct |
20 ms |
3936 KB |
Output is correct |
52 |
Correct |
18 ms |
3668 KB |
Output is correct |
53 |
Correct |
18 ms |
3916 KB |
Output is correct |
54 |
Execution timed out |
2081 ms |
4040 KB |
Time limit exceeded |
55 |
Halted |
0 ms |
0 KB |
- |