#include <bits/stdc++.h>
using namespace std;
#define int long long
#define vi vector<int>
#define endl '\n'
const int N = (int)1e5+5;
void Solve(){
int n , k; cin>>n>>k;
string s; cin>>s;
deque<char> st;
for(auto &i : s){
st.push_back(i);
}
while (!st.empty() and st.front()!='J'){
st.pop_front();
}
while(!st.empty() and st.back() != 'I'){
st.pop_back();
}
if(st.empty()){
cout<<-1<<endl;
return;
}
s = " " + s;
vi prej(n+1) , prei(n+1) , preo(n+1);
for(int i = 1 ; i <= n ; i++){
prej[i] = prej[i-1] + (s[i]=='J');
prei[i] = prei[i-1] + (s[i]=='I');
preo[i] = preo[i-1] + (s[i]=='O');
}
// cout<<"j"<<" ";
// for(int i = 1 ; i<= n ; i++){
// cout<<prej[i]<<" ";
// }
// cout<<endl;
// cout<<"o"<<" ";
// for(int i = 1 ; i<= n ; i++){
// cout<<preo[i]<<" ";
// }
// cout<<endl;
// cout<<"i"<<" ";
// for(int i = 1 ; i<= n ; i++){
// cout<<prei[i]<<" ";
// }
// cout<<endl;
int ans = 1e18;
for (int i = 1; i <= n; ++i) {
if (s[i]=='J'){
int j = lower_bound(prej.begin(), prej.end(),prej[i-1]+k) - prej.begin();
// cout<<i<<" "<<j<<" " << preo[j]+k <<endl;
if (j>=n+1)continue;
int o = lower_bound(preo.begin(), preo.end(),preo[j]+k) - preo.begin();
// cout<<j<<" "<<o<<endl;
if(o>=n+1) continue;
int ii = lower_bound(prei.begin(), prei.end(),prei[o]+k) - prei.begin();
if(ii>=n+1) continue;
int temp = (ii-i+1 - (3*k));
// cout<<temp<<endl;
ans = min(ans,temp);
}
}
if(ans>=1e18){
cout<<-1<<endl;
return;
}
cout<<ans<<endl;
}
int32_t main(){
Solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
432 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
600 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
432 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
600 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
600 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
436 KB |
Output is correct |
28 |
Correct |
1 ms |
592 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
344 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
344 KB |
Output is correct |
35 |
Correct |
1 ms |
356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
432 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
600 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
600 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
436 KB |
Output is correct |
28 |
Correct |
1 ms |
592 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
344 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
344 KB |
Output is correct |
35 |
Correct |
1 ms |
356 KB |
Output is correct |
36 |
Correct |
17 ms |
5172 KB |
Output is correct |
37 |
Correct |
17 ms |
5748 KB |
Output is correct |
38 |
Correct |
17 ms |
5672 KB |
Output is correct |
39 |
Correct |
16 ms |
5924 KB |
Output is correct |
40 |
Correct |
13 ms |
5668 KB |
Output is correct |
41 |
Correct |
15 ms |
5672 KB |
Output is correct |
42 |
Correct |
17 ms |
5748 KB |
Output is correct |
43 |
Correct |
8 ms |
3692 KB |
Output is correct |
44 |
Correct |
11 ms |
4556 KB |
Output is correct |
45 |
Correct |
12 ms |
5672 KB |
Output is correct |
46 |
Correct |
12 ms |
5748 KB |
Output is correct |
47 |
Correct |
13 ms |
5752 KB |
Output is correct |
48 |
Correct |
12 ms |
5672 KB |
Output is correct |
49 |
Correct |
8 ms |
3944 KB |
Output is correct |
50 |
Correct |
13 ms |
5672 KB |
Output is correct |
51 |
Correct |
12 ms |
5748 KB |
Output is correct |
52 |
Correct |
8 ms |
5416 KB |
Output is correct |
53 |
Correct |
9 ms |
5704 KB |
Output is correct |
54 |
Correct |
11 ms |
5668 KB |
Output is correct |
55 |
Correct |
9 ms |
5708 KB |
Output is correct |
56 |
Correct |
7 ms |
5672 KB |
Output is correct |