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;
const int N = (int) 2e5 + 7;
const int INF = (int) 1e9;
int J[N], O[N], I[N];
int whereJ[N], whereO[N], whereI[N];
int n, k;
string s;
signed main() {
ios::sync_with_stdio(0); cin.tie(0);
// freopen("input", "r", stdin);
cin >> n >> k >> s;
s = "#" + s;
for (int i = 1; i <= n; i++) {
J[i] = J[i - 1];
O[i] = O[i - 1];
I[i] = I[i - 1];
if (s[i] == 'J') {
J[i]++;
} else if (s[i] == 'O') {
O[i]++;
} else {
I[i]++;
}
}
for (int i = 1; i <= n; i++) {
int low = 1, high = n;
while (low <= high) {
int mid = (low + high) / 2;
if (J[mid] - J[i - 1] >= k) {
whereJ[i] = mid;
high = mid - 1;
} else {
low = mid + 1;
}
}
}
for (int i = 1; i <= n; i++) {
int low = 1, high = n;
while (low <= high) {
int mid = (low + high) / 2;
if (O[mid] - O[i - 1] >= k) {
whereO[i] = mid;
high = mid - 1;
} else {
low = mid + 1;
}
}
}
for (int i = 1; i <= n; i++) {
int low = 1, high = n;
while (low <= high) {
int mid = (low + high) / 2;
if (I[mid] - I[i - 1] >= k) {
whereI[i] = mid;
high = mid - 1;
} else {
low = mid + 1;
}
}
}
int ret = INF;
for (int i = 1; i <= n; i++) {
int j = whereJ[i];
j = whereO[j];
j = whereI[j];
if (j != 0) {
ret = min(ret, j - i + 1 - 3 * k);
}
}
if (ret == INF) {
ret = -1;
}
cout << ret << "\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... |