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;
int positions[n][3];
string opt = "JOI";
for (int i = 0; i < 3; i++) {
int leftPointer = 0, rightPointer = 0, count = 0;
if (s[0] == opt[i]){
count++;
}
while (leftPointer < n) {
while (count < k && rightPointer < n) {
rightPointer++;
if (rightPointer == n){
break;;
}
if (s[rightPointer] == opt[i]){
count++;
}
}
if (count < k) {
positions[leftPointer][i] = INT_MAX;
leftPointer++;
continue;
}
positions[leftPointer][i] = rightPointer;
if (s[leftPointer] == opt[i]){
count--;
}
leftPointer++;
}
}
int ans = INT_MAX;
for (int i = 0; i < n; i++) {
int leftPointer = i, rightPointer = positions[i][0];
for (int j = 1; j < 3; j++) {
if (rightPointer == INT_MAX){
continue;
}
rightPointer = positions[rightPointer][j];
}
ans = min(ans, rightPointer - leftPointer + 1 - 3 * k);
}
if (ans > n)
cout << -1;
else
cout << ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |