#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ff first
#define sc second
#define pb push_back
#define in insert
ll a[200001],d[200001];
int32_t main () {
//ios::sync_with_stdio(false);
//cin.tie(nullptr);
ll n, mx; cin>>n>>mx;
string s; cin>>s;
ll x = -1 , y = -1 , z = -1;
for (int i = n - 1; i >= 0; i--) {
if (s[i] == 'J') {
a[i] = x;
x = i;
}
if (s[i] == 'O') {
a[i] = y;
y = i;
}
if (s[i] == 'I') {
a[i] = z;
z = i;
}
}
/*
for (int i = 0; i < n; i++)
cout<<a[i]<<" ";*/
for (int i = 0; i < n; i++){
if(d[i] != 0) continue;
int k = i , l = i , cnt = 1;
while (k != -1 && cnt <= mx) {
cnt++;
l = k;
k = a[k];
}
//cout<<l<<" ";
int j = i;
while (l != -1){
d[j] = l;
j = a[j];
l = a[l];
}
while (j != -1) {
d[j] = -1;
j = a[j];
}
}
/*
for (int i = 0; i < n; i++){
cout<<d[i]<<" ";
}*/
x = -1 ; y = -1 ; z = -1;
for(int i = 0; i < n; i++) {
if(s[i] =='J') {
if (d[i] == -1) break;
x = i;
for(int j = d[i] + 1; j < n; j++) {
if (s[j] == 'O') {
if (d[j] == -1) break;
y = j;
for (int k = d[j] + 1; k < n; k++) {
if (s[k] == 'I') {
if (d[k] == -1) break;
z = k;
break;
}
}
break;
}
}
break;
}
}//cout<<x<<" "<<y<<" "<<z;
if (x == -1 || y == -1 || z == -1) {
cout<<-1; return 0;
}
ll mn = d[z] - x + 1 - 3 * mx;// cout<<mn;
while (d[x] != -1) {
x = a[x];
if (x == -1 || d[x] == -1) break;
while (d[x] > y && y != -1 && d[y] != -1) {
y = a[y];
}
if(y == -1 || d[y] == -1) break;
while(d[y] > z && z != -1 && d[z] != -1) {
z = a[z];
}
if (z == -1 || d[z] == -1) break;
mn = min(mn , d[z] - x + 1 - 3 * mx);
}
cout<<mn;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
304 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
304 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
304 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
304 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
304 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
312 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
308 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
304 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
304 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
304 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
312 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
308 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
8 ms |
3684 KB |
Output is correct |
37 |
Correct |
10 ms |
3940 KB |
Output is correct |
38 |
Correct |
10 ms |
3920 KB |
Output is correct |
39 |
Correct |
12 ms |
3816 KB |
Output is correct |
40 |
Correct |
9 ms |
3940 KB |
Output is correct |
41 |
Correct |
10 ms |
3940 KB |
Output is correct |
42 |
Correct |
10 ms |
3948 KB |
Output is correct |
43 |
Correct |
7 ms |
2640 KB |
Output is correct |
44 |
Correct |
7 ms |
3136 KB |
Output is correct |
45 |
Correct |
9 ms |
3940 KB |
Output is correct |
46 |
Correct |
9 ms |
3916 KB |
Output is correct |
47 |
Correct |
9 ms |
3940 KB |
Output is correct |
48 |
Correct |
9 ms |
3904 KB |
Output is correct |
49 |
Correct |
6 ms |
2788 KB |
Output is correct |
50 |
Correct |
11 ms |
3920 KB |
Output is correct |
51 |
Correct |
12 ms |
3940 KB |
Output is correct |
52 |
Correct |
11 ms |
3732 KB |
Output is correct |
53 |
Correct |
11 ms |
3940 KB |
Output is correct |
54 |
Correct |
8 ms |
3940 KB |
Output is correct |
55 |
Correct |
8 ms |
3840 KB |
Output is correct |
56 |
Correct |
8 ms |
3944 KB |
Output is correct |