This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define size(x) (int)x.size()
#define all(x) x.begin(), x.end()
template<class S, class T>
bool chmin(S &a, const T &b) {
return a > b ? (a = b) == b : false;
}
template<class S, class T>
bool chmax(S &a, const T &b) {
return a < b ? (a = b) == b : false;
}
const int inf = 1e9 + 7;
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
int n, k; cin >> n >> k;
string s; cin >> s;
s = '$' + s;
int res = inf;
deque<int> J, O, I;
for (int i = 1; i <= n; ++i) {
if (s[i] == 'O') O.push_back(i);
else if (s[i] == 'I' && size(O) >= k) {
I.push_back(i);
}
}
for (int i = 1; i <= n; ++i) {
if (s[i] == 'O' && size(J) >= k && size(O) >= k && size(I) >= k) {
int cur = O[k - 1] - O[0] + 1 - k;
cur += O[0] - J.back() - 1;
cur += J.back() - J[size(J) - k] + 1 - k;
cur += I[0] - O[k - 1] - 1;
cur += I[k - 1] - I[0] + 1 - k;
chmin(res, cur);
}
if (s[i] == 'J') J.push_back(i);
else if (s[i] == 'O') {
O.pop_front();
if (size(O) < k) break;
while (!I.empty() && I.front() < O[k - 1]) I.pop_front();
}
}
cout << (res == inf ? -1 : res);
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |