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>
int main() {
std::ios::sync_with_stdio(0); std::cin.tie(0);
int n, k; std::cin >> n >> k;
std::deque <int> J, O, I, a;
for (int i = 0; i < n; i++) {
char p; std::cin >> p;
if (p == 'J') J.push_back(i);
if (p == 'O') O.push_back(i);
if (p == 'I') I.push_back(i);
}
int ans = 1 << 30;
for (int i = 0, j = 0; j + k <= (int)O.size(); j++) {
int l = O[j], r = O[j + k - 1];
while (i < (int)J.size() && J[i] < l) a.push_back(J[i++]);
while ((int)a.size() > k) a.pop_front();
while (I.size() && I.front() < r) I.pop_front();
if ((int)a.size() == k && (int)I.size() >= k) ans = std::min(ans, I[k - 1] - a[0] + 1);
}
std::cout << (ans == (1 << 30) ? -1 : ans - k * 3) << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |