# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
203552 | arman_ferdous | JJOOII 2 (JOI20_ho_t2) | C++14 | 13 ms | 2936 KiB |
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 = 2e5+100;
int n, k;
char s[N];
int J[N], O[N], I[N];
int main() {
scanf("%d %d %s", &n, &k, s + 1);
int ptr = -1, cnt = 0;
for(int i = 1; i <= n; i++) {
while(ptr + 1 <= n && cnt < k) {
ptr++;
if(s[ptr] == 'J') cnt++;
}
if(cnt == k) J[i] = ptr;
if(s[i] == 'J') cnt--;
} ptr = -1, cnt = 0;
for(int i = 1; i <= n; i++) {
while(ptr + 1 <= n && cnt < k) {
ptr++;
if(s[ptr] == 'O') cnt++;
}
if(cnt == k) O[i] = ptr;
if(s[i] == 'O') cnt--;
} ptr = -1, cnt = 0;
for(int i = 1; i <= n; i++) {
while(ptr + 1 <= n && cnt < k) {
ptr++;
if(s[ptr] == 'I') cnt++;
}
if(cnt == k) I[i] = ptr;
if(s[i] == 'I') cnt--;
}
int ans = n * 3;
for(int i = 1; i <= n; i++) {
int to = I[O[J[i]]];
if(to >= i) ans = min(ans, to - i + 1 - 3 * k);
}
if(ans == n * 3) ans = -1;
printf("%d\n", ans);
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |