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 j = 0, o = 0, i = 0;
for (int idx = 0; idx < n; idx++) {
if (s[idx] == 'J' && j < k) j++;
else if (s[idx] == 'O' && j == k && o < k) o++;
else if (s[idx] == 'I' && j == k && o == k && i < k) i++;
}
if (j != k || o != k || i != k) {
cout << "-1\n";
return 0;
}
int ans1 = 0;
j = 0, o = 0, i = 0;
for (int idx = 0; idx < n; idx++) {
if (j == k && o == k && i == k) break;
if (s[idx] == 'J' && j < k) j++;
else if (s[idx] == 'O' && j == k && o < k) o++;
else if (s[idx] == 'I' && j == k && o == k && i < k) i++;
else {
if (j || o || i)
ans1++;
}
}
int ans2 = 0;
j = 0, o = 0, i = 0;
for (int idx = n-1; idx >= 0; idx--) {
if (j == k && o == k && i == k) break;
if (s[idx] == 'I' && i < k) i++;
else if (s[idx] == 'O' && i == k && o < k) o++;
else if (s[idx] == 'J' && i == k && o == k && j < k) j++;
else {
if (j || o || i)
ans2++;
}
}
cout << min(ans1, ans2) << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |