#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<(int)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);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |