#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
int n, k;
cin >> n >> k;
vector<char> joi(n);
vector<int> j, o, i;
for(int x = 0; x < n; x++) {
cin >> joi[x];
if(joi[x] == 'J') {
j.push_back(x);
} else if(joi[x] == 'O') {
o.push_back(x);
} else {
i.push_back(x);
}
}
int ans = INT_MAX;
for(int x = 0; x < j.size() - k + 1; x++) {
int posO = lower_bound(o.begin(), o.end(), j[x + k - 1]) - o.begin();
if(posO < o.size() - k + 1) {
int posI = lower_bound(i.begin(), i.end(), o[posO + k - 1]) - i.begin();
if(posI < i.size() - k + 1) {
int cur = i[posI + k - 1];
ans = min(ans, (cur - j[x] - 3 * k + 1));
}
}
}
if(ans == INT_MAX) {
cout << -1 << endl;
} else {
cout << ans << endl;
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |