#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;}
}
//binse
int llll=1, rrrr=n;
while(llll<rrrr){
int mmmm = (llll+rrrr)/2;
if(oct[mmmm]-oct[j_r] >= k) rrrr=mmmm;
else llll=mmmm+1;
}
ival=llll-1;
//binse(ival+1);
int lll=0, rrr=isz-1;
while(lll<rrr){
int mmm = (lll+rrr)/2;
if(ipos[mmm] > ival) rrr=mmm;
else lll=mmm+1;
}
i_l = ipos[lll];
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 |
4 ms |
256 KB |
Output is correct |
3 |
Correct |
4 ms |
256 KB |
Output is correct |
4 |
Correct |
5 ms |
256 KB |
Output is correct |
5 |
Correct |
5 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 |
5 ms |
256 KB |
Output is correct |
9 |
Correct |
4 ms |
256 KB |
Output is correct |
10 |
Correct |
5 ms |
256 KB |
Output is correct |
11 |
Correct |
5 ms |
256 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
4 ms |
256 KB |
Output is correct |
14 |
Correct |
5 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
256 KB |
Output is correct |
2 |
Correct |
4 ms |
256 KB |
Output is correct |
3 |
Correct |
4 ms |
256 KB |
Output is correct |
4 |
Correct |
5 ms |
256 KB |
Output is correct |
5 |
Correct |
5 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 |
5 ms |
256 KB |
Output is correct |
9 |
Correct |
4 ms |
256 KB |
Output is correct |
10 |
Correct |
5 ms |
256 KB |
Output is correct |
11 |
Correct |
5 ms |
256 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
4 ms |
256 KB |
Output is correct |
14 |
Correct |
5 ms |
256 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 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 |
5 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 |
5 ms |
512 KB |
Output is correct |
27 |
Correct |
5 ms |
384 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 |
5 ms |
384 KB |
Output is correct |
34 |
Correct |
5 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 |
4 ms |
256 KB |
Output is correct |
3 |
Correct |
4 ms |
256 KB |
Output is correct |
4 |
Correct |
5 ms |
256 KB |
Output is correct |
5 |
Correct |
5 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 |
5 ms |
256 KB |
Output is correct |
9 |
Correct |
4 ms |
256 KB |
Output is correct |
10 |
Correct |
5 ms |
256 KB |
Output is correct |
11 |
Correct |
5 ms |
256 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
4 ms |
256 KB |
Output is correct |
14 |
Correct |
5 ms |
256 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 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 |
5 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 |
5 ms |
512 KB |
Output is correct |
27 |
Correct |
5 ms |
384 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 |
5 ms |
384 KB |
Output is correct |
34 |
Correct |
5 ms |
384 KB |
Output is correct |
35 |
Correct |
5 ms |
384 KB |
Output is correct |
36 |
Correct |
27 ms |
3672 KB |
Output is correct |
37 |
Correct |
27 ms |
3928 KB |
Output is correct |
38 |
Correct |
27 ms |
3916 KB |
Output is correct |
39 |
Correct |
25 ms |
3916 KB |
Output is correct |
40 |
Correct |
22 ms |
4044 KB |
Output is correct |
41 |
Correct |
25 ms |
4044 KB |
Output is correct |
42 |
Correct |
27 ms |
4044 KB |
Output is correct |
43 |
Correct |
15 ms |
2708 KB |
Output is correct |
44 |
Correct |
17 ms |
2936 KB |
Output is correct |
45 |
Correct |
23 ms |
4044 KB |
Output is correct |
46 |
Correct |
22 ms |
3936 KB |
Output is correct |
47 |
Correct |
22 ms |
4172 KB |
Output is correct |
48 |
Correct |
21 ms |
3916 KB |
Output is correct |
49 |
Correct |
15 ms |
2828 KB |
Output is correct |
50 |
Correct |
22 ms |
3916 KB |
Output is correct |
51 |
Correct |
22 ms |
3936 KB |
Output is correct |
52 |
Correct |
18 ms |
3668 KB |
Output is correct |
53 |
Correct |
18 ms |
4040 KB |
Output is correct |
54 |
Correct |
21 ms |
4040 KB |
Output is correct |
55 |
Correct |
19 ms |
4040 KB |
Output is correct |
56 |
Correct |
17 ms |
4040 KB |
Output is correct |