#include <cstdio>
#include <cstring>
#include <algorithm>
#define N 200005
int n, k, f[N][4], far_pos[N][4], near_pos[N][4];
char s[N];
int cnt(int l, int r, int c) { return f[r][c] - f[l-1][c]; }
int suf(int l, int c) { return f[n][c] - f[l-1][c]; }
#define chmax(a, b) a = std::max(a, b)
#define chmin(a, b) a = std::min(a, b)
int ok(int m)
{
/* get O's from l to r */
/*
* --JJJJOOOOOOOOOOOOOIIII-
* l r
*/
int cost[4] = {0};
for (int r = 1; r <= n; ++r)
{
/* c[r][1] - c[l-1][1] == k
* c[l-1][1] == c[r][1] - m */
if (f[r][2] < k) continue;
int l = far_pos[f[r][2] - k][2] + 1;
cost[2] = r-l+1-k;
/* f[l-1][1] - c[l0-1][1] == m
* c[l0-1][1] == f[l-1][1] - m */
if (f[l-1][1] < k) continue;
int l0 = far_pos[f[l-1][1] - k][1] + 1;
//if (l0 > 1e9) continue;
cost[1] = l-1-l0+1-k;
/* f[r1][3] - f[r][3] == k
* f[r1][3] == k + f[r][3] */
int r1 = near_pos[k + f[r][3]][3];
if (r1 == -1) continue;
cost[3] = r1-r-k;
if (cost[1] + cost[2] + cost[3] <= m)
{
//printf("(%d@%d): %d %d - %d %d - %d %d\n",m,r,l0,l-1,l,r,r+1,r1);
return 1;
}
}
return 0;
}
int main()
{
scanf("%d%d%s", &n, &k, s + 1);
for (int i = 1; i <= n; ++i) for (int j = 0; j < 3; ++j) if (j["JOI"] == s[i]) s[i] = j + 1;
for (int i = 1; i <= n; ++i) { for (int j = 1; j <= 3; ++j) f[i][j] = f[i-1][j]; ++f[i][s[i]]; }
memset(far_pos, -1, sizeof far_pos);
memset(near_pos, 63, sizeof near_pos);
for (int i = 0; i <= n; ++i) for (int j = 1; j <= 3; ++j) far_pos[f[i][j]][j] = i;
for (int i = n; i >= 1; --i)
for (int j = 1; j <= 3; ++j) near_pos[f[i][j]][j] = i;
//for (int i = 1; i <= n; ++i) for (int j = 1; j <= 3; ++j) chmax(far_pos[f[i][j]][j], far_pos[f[i][j]-1][j]);
int l = 0, r = n - 1, z = -1;
while (l <= r)
{
int m = (l+r)/2;
if (ok(m)) r=m-1, z=m;
else l=m+1;
}
printf("%d", z);
return 0;
}
Compilation message
ho_t2.cpp: In function 'int main()':
ho_t2.cpp:56:96: warning: array subscript has type 'char' [-Wchar-subscripts]
56 | for (int i = 1; i <= n; ++i) { for (int j = 1; j <= 3; ++j) f[i][j] = f[i-1][j]; ++f[i][s[i]]; }
| ~~~^
ho_t2.cpp:54:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
54 | scanf("%d%d%s", &n, &k, s + 1);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6484 KB |
Output is correct |
2 |
Correct |
2 ms |
6484 KB |
Output is correct |
3 |
Correct |
2 ms |
6484 KB |
Output is correct |
4 |
Correct |
2 ms |
6484 KB |
Output is correct |
5 |
Correct |
2 ms |
6532 KB |
Output is correct |
6 |
Correct |
2 ms |
6484 KB |
Output is correct |
7 |
Correct |
2 ms |
6484 KB |
Output is correct |
8 |
Correct |
2 ms |
6484 KB |
Output is correct |
9 |
Correct |
2 ms |
6484 KB |
Output is correct |
10 |
Correct |
3 ms |
6480 KB |
Output is correct |
11 |
Correct |
2 ms |
6484 KB |
Output is correct |
12 |
Correct |
2 ms |
6484 KB |
Output is correct |
13 |
Correct |
3 ms |
6484 KB |
Output is correct |
14 |
Correct |
2 ms |
6484 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6484 KB |
Output is correct |
2 |
Correct |
2 ms |
6484 KB |
Output is correct |
3 |
Correct |
2 ms |
6484 KB |
Output is correct |
4 |
Correct |
2 ms |
6484 KB |
Output is correct |
5 |
Correct |
2 ms |
6532 KB |
Output is correct |
6 |
Correct |
2 ms |
6484 KB |
Output is correct |
7 |
Correct |
2 ms |
6484 KB |
Output is correct |
8 |
Correct |
2 ms |
6484 KB |
Output is correct |
9 |
Correct |
2 ms |
6484 KB |
Output is correct |
10 |
Correct |
3 ms |
6480 KB |
Output is correct |
11 |
Correct |
2 ms |
6484 KB |
Output is correct |
12 |
Correct |
2 ms |
6484 KB |
Output is correct |
13 |
Correct |
3 ms |
6484 KB |
Output is correct |
14 |
Correct |
2 ms |
6484 KB |
Output is correct |
15 |
Correct |
2 ms |
6596 KB |
Output is correct |
16 |
Correct |
3 ms |
6484 KB |
Output is correct |
17 |
Correct |
2 ms |
6484 KB |
Output is correct |
18 |
Correct |
2 ms |
6564 KB |
Output is correct |
19 |
Correct |
2 ms |
6484 KB |
Output is correct |
20 |
Correct |
3 ms |
6564 KB |
Output is correct |
21 |
Correct |
2 ms |
6612 KB |
Output is correct |
22 |
Correct |
2 ms |
6484 KB |
Output is correct |
23 |
Correct |
3 ms |
6564 KB |
Output is correct |
24 |
Correct |
2 ms |
6612 KB |
Output is correct |
25 |
Correct |
2 ms |
6564 KB |
Output is correct |
26 |
Correct |
2 ms |
6484 KB |
Output is correct |
27 |
Correct |
3 ms |
6484 KB |
Output is correct |
28 |
Correct |
3 ms |
6564 KB |
Output is correct |
29 |
Correct |
2 ms |
6612 KB |
Output is correct |
30 |
Correct |
3 ms |
6612 KB |
Output is correct |
31 |
Correct |
2 ms |
6484 KB |
Output is correct |
32 |
Correct |
2 ms |
6612 KB |
Output is correct |
33 |
Correct |
2 ms |
6612 KB |
Output is correct |
34 |
Correct |
2 ms |
6612 KB |
Output is correct |
35 |
Correct |
2 ms |
6612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6484 KB |
Output is correct |
2 |
Correct |
2 ms |
6484 KB |
Output is correct |
3 |
Correct |
2 ms |
6484 KB |
Output is correct |
4 |
Correct |
2 ms |
6484 KB |
Output is correct |
5 |
Correct |
2 ms |
6532 KB |
Output is correct |
6 |
Correct |
2 ms |
6484 KB |
Output is correct |
7 |
Correct |
2 ms |
6484 KB |
Output is correct |
8 |
Correct |
2 ms |
6484 KB |
Output is correct |
9 |
Correct |
2 ms |
6484 KB |
Output is correct |
10 |
Correct |
3 ms |
6480 KB |
Output is correct |
11 |
Correct |
2 ms |
6484 KB |
Output is correct |
12 |
Correct |
2 ms |
6484 KB |
Output is correct |
13 |
Correct |
3 ms |
6484 KB |
Output is correct |
14 |
Correct |
2 ms |
6484 KB |
Output is correct |
15 |
Correct |
2 ms |
6596 KB |
Output is correct |
16 |
Correct |
3 ms |
6484 KB |
Output is correct |
17 |
Correct |
2 ms |
6484 KB |
Output is correct |
18 |
Correct |
2 ms |
6564 KB |
Output is correct |
19 |
Correct |
2 ms |
6484 KB |
Output is correct |
20 |
Correct |
3 ms |
6564 KB |
Output is correct |
21 |
Correct |
2 ms |
6612 KB |
Output is correct |
22 |
Correct |
2 ms |
6484 KB |
Output is correct |
23 |
Correct |
3 ms |
6564 KB |
Output is correct |
24 |
Correct |
2 ms |
6612 KB |
Output is correct |
25 |
Correct |
2 ms |
6564 KB |
Output is correct |
26 |
Correct |
2 ms |
6484 KB |
Output is correct |
27 |
Correct |
3 ms |
6484 KB |
Output is correct |
28 |
Correct |
3 ms |
6564 KB |
Output is correct |
29 |
Correct |
2 ms |
6612 KB |
Output is correct |
30 |
Correct |
3 ms |
6612 KB |
Output is correct |
31 |
Correct |
2 ms |
6484 KB |
Output is correct |
32 |
Correct |
2 ms |
6612 KB |
Output is correct |
33 |
Correct |
2 ms |
6612 KB |
Output is correct |
34 |
Correct |
2 ms |
6612 KB |
Output is correct |
35 |
Correct |
2 ms |
6612 KB |
Output is correct |
36 |
Correct |
6 ms |
9736 KB |
Output is correct |
37 |
Correct |
7 ms |
10036 KB |
Output is correct |
38 |
Correct |
6 ms |
10032 KB |
Output is correct |
39 |
Correct |
12 ms |
9928 KB |
Output is correct |
40 |
Correct |
11 ms |
10016 KB |
Output is correct |
41 |
Correct |
12 ms |
10012 KB |
Output is correct |
42 |
Correct |
12 ms |
10052 KB |
Output is correct |
43 |
Correct |
7 ms |
8660 KB |
Output is correct |
44 |
Correct |
11 ms |
9332 KB |
Output is correct |
45 |
Correct |
13 ms |
10064 KB |
Output is correct |
46 |
Correct |
10 ms |
10068 KB |
Output is correct |
47 |
Correct |
10 ms |
10068 KB |
Output is correct |
48 |
Correct |
11 ms |
10068 KB |
Output is correct |
49 |
Correct |
9 ms |
8824 KB |
Output is correct |
50 |
Correct |
12 ms |
10068 KB |
Output is correct |
51 |
Correct |
12 ms |
10060 KB |
Output is correct |
52 |
Correct |
9 ms |
9884 KB |
Output is correct |
53 |
Correct |
9 ms |
10064 KB |
Output is correct |
54 |
Correct |
11 ms |
10016 KB |
Output is correct |
55 |
Correct |
8 ms |
10068 KB |
Output is correct |
56 |
Correct |
7 ms |
10068 KB |
Output is correct |