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 f first
#define s second
int N, K;
string str;
int main() {
cin >> N >> K >> str;
vector<int> J, O, I;
for (int i = 0; i < (int)str.size(); i++) {
if (str[i] == 'J') {
J.push_back(i+1);
}
else if (str[i] == 'O') {
O.push_back(i+1);
}
else {
I.push_back(i+1);
}
}
vector<pair<int, int>> A, B, C;
for (int i = 0; i+K-1 < (int)J.size(); i++) {
A.emplace_back(J[i], J[K+i-1]);
}
for (int i = 0; i+K-1 < (int)O.size(); i++) {
B.emplace_back(O[i], O[K+i-1]);
}
for (int i = 0; i+K-1 < (int)I.size(); i++) {
C.emplace_back(I[i], I[K+i-1]);
}
int a = 0;
int b = 0;
int c = 0;
int ans = INT_MAX;
while (a < (int)A.size() && b < (int)B.size() && c < (int)C.size()) {
if (B[b].f < A[a].s) {
b++;
continue;
}
if (C[c].f < B[b].s) {
c++;
continue;
}
ans = min(ans, C[c].s-A[a].f-3*K+1);
a++;
}
if (ans == INT_MAX) ans = -1;
cout << 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... |