#include <bits/stdc++.h>
using namespace std;
const int N=2e5+5;
int n,k,ans,d,x; string s;
vector <int> j,o,_i;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin>>n>>k>>s;
for (int i=0; i<n; i++){
if (s[i]=='J'){
j.push_back(i+1);
}
if (s[i]=='O'){
o.push_back(i+1);
}
if (s[i]=='I'){
_i.push_back(i+1);
}
} ans=1e9;
for (int i=0; i<o.size()-k+1; i++){
d=o[i+k-1]-o[i]-k+1; //cout<<d<<" ";
x=lower_bound(j.begin(),j.end(),o[i])-1-j.begin();
if (x<k-1) continue;
d+=j[x]-j[x-k+1]-k+1; //cout<<d<<" ";
x=lower_bound(_i.begin(),_i.end(),o[i+k-1])-_i.begin();
if (x>(int)_i.size()-k) continue;
d+=_i[x+k-1]-_i[x]-k+1; //cout<<d<<" ";
ans=min(ans,d); //cout<<"\n";
}
cout<<(ans<1e9?ans:-1);
}
Compilation message
ho_t2.cpp: In function 'int main()':
ho_t2.cpp:21:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
21 | for (int i=0; i<o.size()-k+1; i++){
| ~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |