#include <bits/stdc++.h>
using namespace std;
#define int long long
#define mx 1000007
#define inf LLONG_MAX/20
#define pi pair<int, int>
#define mp make_pair
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, k;
cin >> n >> k;
string s;
cin >> s;
int J[n];
int O[n];
int I[n];
J[0] = 0;
O[0] = 0;
I[0] = 0;
if(s[0] == 'J') J[0]++;
else if(s[0] == 'O') O[0]++;
else if(s[0] == 'I') I[0]++;
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 if(s[i] == 'I') I[i]++;
}
int ans = inf;
for(int i = 0; i < n; i++){
//fused the i=0 and i>0 cases together, so we repeat code less (and hence less likely to make mistake)
int prevJ = 0;
if (i != 0){
prevJ = J[i-1];
}
//starting from index i, the number of Js that came before is J[i-1] (if i-1 exists, otherwise 0)
int jay = lower_bound(J + i, J + n, k + prevJ) - J;
if (jay == n) continue;
//jay is now the last J that we take
//so our next lower_bound starts one space after that
//then the number of Os before we start taking Os is O[jay], since we start on jay+1
int oou = lower_bound(O + jay + 1, O + n, k + O[jay]) - O;
if(oou == n) continue;
//oou is now the last O that we take
//so our next lower_bound starts one space after that
//then the number of Is before we start taking Is is I[oou], since we start on oou+1
int aii = lower_bound(I + oou + 1, I + n, k + I[oou]) - I;
if(aii == n) continue;
//made a change here too; length of subarray from [l, r] is r-l+1, not r-l
int cost = (aii - i + 1) - (3 * k);
ans = min(cost, ans);
}
if(ans == inf) ans = -1;
cout << ans;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
456 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
456 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
456 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
456 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
456 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
456 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
15 ms |
5092 KB |
Output is correct |
37 |
Correct |
17 ms |
5592 KB |
Output is correct |
38 |
Correct |
20 ms |
5604 KB |
Output is correct |
39 |
Correct |
18 ms |
5608 KB |
Output is correct |
40 |
Correct |
15 ms |
5608 KB |
Output is correct |
41 |
Correct |
17 ms |
5608 KB |
Output is correct |
42 |
Correct |
20 ms |
5608 KB |
Output is correct |
43 |
Correct |
8 ms |
3676 KB |
Output is correct |
44 |
Correct |
10 ms |
4324 KB |
Output is correct |
45 |
Correct |
15 ms |
5608 KB |
Output is correct |
46 |
Correct |
12 ms |
5608 KB |
Output is correct |
47 |
Correct |
12 ms |
5608 KB |
Output is correct |
48 |
Correct |
13 ms |
5608 KB |
Output is correct |
49 |
Correct |
9 ms |
3804 KB |
Output is correct |
50 |
Correct |
12 ms |
5608 KB |
Output is correct |
51 |
Correct |
14 ms |
5608 KB |
Output is correct |
52 |
Correct |
8 ms |
5352 KB |
Output is correct |
53 |
Correct |
8 ms |
5608 KB |
Output is correct |
54 |
Correct |
9 ms |
5604 KB |
Output is correct |
55 |
Correct |
7 ms |
5608 KB |
Output is correct |
56 |
Correct |
6 ms |
5608 KB |
Output is correct |