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;
int main() {
int N, K;
cin >> N >> K;
string S;
cin >> S;
vector<int> J(N, N + 1), O(N, N + 1), I(N, N + 1);
for (int l = 0, r = 0, c = 0; l < N; l++) {
while (r < N && c < K) {
c += S[r] == 'J';
r++;
}
J[l] = (c == K ? r : N + 1);
c -= S[l] == 'J';
}
for (int l = 0, r = 0, c = 0; l < N; l++) {
while (r < N && c < K) {
c += S[r] == 'O';
r++;
}
O[l] = (c == K ? r : N + 1);
c -= S[l] == 'O';
}
for (int l = 0, r = 0, c = 0; l < N; l++) {
while (r < N && c < K) {
c += S[r] == 'I';
r++;
}
I[l] = (c == K ? r : N + 1);
c -= S[l] == 'I';
}
int ans = N + 1;
for (int i = 0; i < N; i++) {
if (J[i] >= N || O[J[i]] >= N || I[O[J[i]]] > N) {
continue;
}
ans = min(ans, I[O[J[i]]] - i - 3 * K);
}
cout << (ans == N + 1 ? -1 : ans) << "\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... |