#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int main(){
ios::sync_with_stdio(0);cin.tie(0);
ll n,k;
string s;
cin >> n >> k >> s;
{
ll c0=0,c1=0,c2=0;
for(char c : s){
if(c=='J')c0++;
if(c0>=k&&c=='O')c1++;
if(c1>=k&&c=='I')c2++;
}
if(c2<k){
cout << "-1\n";
return 0;
}
}
vector<ll> a,b,c;
for(ll i = 0;i<n;i++){
if(s[i]=='J') a.push_back(i);
if(s[i]=='O') b.push_back(i);
if(s[i]=='I') c.push_back(i);
}
//for(ll i = 0 ;i<a.size();i++) cout << a[i] << " \n"[i+1==a.size()];
//for(ll i = 0 ;i<b.size();i++) cout << b[i] << " \n"[i+1==b.size()];
//for(ll i = 0 ;i<c.size();i++) cout << c[i] << " \n"[i+1==c.size()];
ll ans = 1e18;
for(ll i = 0;i+k<=a.size();i++){
ll L = a[i];
ll p0 = a[i+k-1];
//cout << "L=" << L << ", p0=" << p0 << "\n";
auto P1 = upper_bound(b.begin(),b.end(),p0);
if(P1==b.end()) continue;
ll p1 = P1-b.begin();
//cout << "p1=" << p1 << "\n";
if(p1+k>b.size()) continue;
ll p2 = b[p1+k-1];
//cout << "p2=" << p2 << "\n";
auto P3 = upper_bound(c.begin(),c.end(),p2);
if(P3==c.end()) continue;
ll p3 = P3-c.begin();
if(p3+k>c.size()) continue;
ll R = c[p3+k-1];
//cout << L << "~" << R << "\n";
ans = min(ans,R-L+1-k*3);
}
cout << ans << "\n";
}
Compilation message
ho_t2.cpp: In function 'int main()':
ho_t2.cpp:31:21: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
31 | for(ll i = 0;i+k<=a.size();i++){
| ~~~^~~~~~~~~~
ho_t2.cpp:39:16: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | if(p1+k>b.size()) continue;
| ~~~~^~~~~~~~~
ho_t2.cpp:45:16: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | if(p3+k>c.size()) continue;
| ~~~~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
452 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
456 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
452 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
456 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
456 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
456 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 |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
456 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
452 KB |
Output is correct |
32 |
Correct |
1 ms |
344 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
600 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
452 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
456 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
456 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
456 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 |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
456 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
452 KB |
Output is correct |
32 |
Correct |
1 ms |
344 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
600 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
9 ms |
2508 KB |
Output is correct |
37 |
Correct |
10 ms |
3004 KB |
Output is correct |
38 |
Correct |
11 ms |
3032 KB |
Output is correct |
39 |
Correct |
10 ms |
2984 KB |
Output is correct |
40 |
Correct |
10 ms |
3028 KB |
Output is correct |
41 |
Correct |
9 ms |
3028 KB |
Output is correct |
42 |
Correct |
12 ms |
3012 KB |
Output is correct |
43 |
Correct |
5 ms |
1968 KB |
Output is correct |
44 |
Correct |
6 ms |
2008 KB |
Output is correct |
45 |
Correct |
8 ms |
2968 KB |
Output is correct |
46 |
Correct |
8 ms |
2964 KB |
Output is correct |
47 |
Correct |
8 ms |
3008 KB |
Output is correct |
48 |
Correct |
8 ms |
3000 KB |
Output is correct |
49 |
Correct |
2 ms |
860 KB |
Output is correct |
50 |
Correct |
2 ms |
980 KB |
Output is correct |
51 |
Correct |
2 ms |
860 KB |
Output is correct |
52 |
Correct |
2 ms |
980 KB |
Output is correct |
53 |
Correct |
2 ms |
864 KB |
Output is correct |
54 |
Correct |
6 ms |
3448 KB |
Output is correct |
55 |
Correct |
3 ms |
3296 KB |
Output is correct |
56 |
Correct |
3 ms |
3224 KB |
Output is correct |