#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ii = pair<int, int>;
const int maxn = 2e5 + 5;
int n, k;
int cnt[3][maxn];
int to[3][maxn];
int a[maxn];
int main(int argc, char const *argv[])
{
#ifdef LOCAL
freopen("in", "r", stdin);
#endif
cin >> n >> k;
{
string s; cin >> s;
for(int i = 1; i <= n; i++) {
if(s[i-1] == 'J') a[i] = 0;
if(s[i-1] == 'O') a[i] = 1;
if(s[i-1] == 'I') a[i] = 2;
}
}
for(int i = 1; i <= n; i++) {
cnt[a[i]][i]++;
cnt[0][i] += cnt[0][i-1];
cnt[1][i] += cnt[1][i-1];
cnt[2][i] += cnt[2][i-1];
}
if(cnt[0][n] < k or cnt[1][n] < k or cnt[2][n] < k) {
cout << "-1" << endl;
return 0;
}
memset(to, -1, sizeof to);
for(int x = 0; x < 3; x++) {
int *cc = cnt[x];
int r = 0;
for(int i = 0; i < n; i++) {
while(r < n and cc[r] - cc[i] < k) {
r++;
}
if(cc[r] - cc[i] == k) {
to[x][i] = r;
}
}
}
int ans = INT_MAX;
for(int i = 0; i < n; i++) {
int now = i, flag = 1;
for(int x = 0; x < 3; x++) {
if(to[x][now] == -1) {
flag = 0;
break;
}
now = to[x][now];
}
if(flag) {
ans = min(ans, now - i);
}
}
if(ans == INT_MAX) {
cout << "-1" << endl;
} else {
cout << ans - 3*k << endl;
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
2680 KB |
Output is correct |
2 |
Correct |
6 ms |
2680 KB |
Output is correct |
3 |
Correct |
6 ms |
2680 KB |
Output is correct |
4 |
Correct |
6 ms |
2680 KB |
Output is correct |
5 |
Correct |
6 ms |
2680 KB |
Output is correct |
6 |
Correct |
6 ms |
2680 KB |
Output is correct |
7 |
Correct |
6 ms |
2680 KB |
Output is correct |
8 |
Correct |
6 ms |
2680 KB |
Output is correct |
9 |
Correct |
6 ms |
2680 KB |
Output is correct |
10 |
Correct |
7 ms |
2680 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
7 ms |
2680 KB |
Output is correct |
13 |
Correct |
6 ms |
2680 KB |
Output is correct |
14 |
Correct |
7 ms |
2680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
2680 KB |
Output is correct |
2 |
Correct |
6 ms |
2680 KB |
Output is correct |
3 |
Correct |
6 ms |
2680 KB |
Output is correct |
4 |
Correct |
6 ms |
2680 KB |
Output is correct |
5 |
Correct |
6 ms |
2680 KB |
Output is correct |
6 |
Correct |
6 ms |
2680 KB |
Output is correct |
7 |
Correct |
6 ms |
2680 KB |
Output is correct |
8 |
Correct |
6 ms |
2680 KB |
Output is correct |
9 |
Correct |
6 ms |
2680 KB |
Output is correct |
10 |
Correct |
7 ms |
2680 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
7 ms |
2680 KB |
Output is correct |
13 |
Correct |
6 ms |
2680 KB |
Output is correct |
14 |
Correct |
7 ms |
2680 KB |
Output is correct |
15 |
Correct |
6 ms |
2680 KB |
Output is correct |
16 |
Correct |
6 ms |
2680 KB |
Output is correct |
17 |
Correct |
6 ms |
2680 KB |
Output is correct |
18 |
Correct |
6 ms |
2680 KB |
Output is correct |
19 |
Correct |
7 ms |
2856 KB |
Output is correct |
20 |
Correct |
6 ms |
2680 KB |
Output is correct |
21 |
Correct |
6 ms |
2680 KB |
Output is correct |
22 |
Correct |
6 ms |
2680 KB |
Output is correct |
23 |
Correct |
6 ms |
2680 KB |
Output is correct |
24 |
Correct |
6 ms |
2680 KB |
Output is correct |
25 |
Correct |
6 ms |
2680 KB |
Output is correct |
26 |
Correct |
6 ms |
2808 KB |
Output is correct |
27 |
Correct |
6 ms |
2808 KB |
Output is correct |
28 |
Correct |
6 ms |
2680 KB |
Output is correct |
29 |
Correct |
6 ms |
2808 KB |
Output is correct |
30 |
Correct |
6 ms |
2680 KB |
Output is correct |
31 |
Correct |
6 ms |
2680 KB |
Output is correct |
32 |
Correct |
6 ms |
2680 KB |
Output is correct |
33 |
Correct |
6 ms |
2680 KB |
Output is correct |
34 |
Correct |
6 ms |
2680 KB |
Output is correct |
35 |
Correct |
6 ms |
2680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
2680 KB |
Output is correct |
2 |
Correct |
6 ms |
2680 KB |
Output is correct |
3 |
Correct |
6 ms |
2680 KB |
Output is correct |
4 |
Correct |
6 ms |
2680 KB |
Output is correct |
5 |
Correct |
6 ms |
2680 KB |
Output is correct |
6 |
Correct |
6 ms |
2680 KB |
Output is correct |
7 |
Correct |
6 ms |
2680 KB |
Output is correct |
8 |
Correct |
6 ms |
2680 KB |
Output is correct |
9 |
Correct |
6 ms |
2680 KB |
Output is correct |
10 |
Correct |
7 ms |
2680 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
7 ms |
2680 KB |
Output is correct |
13 |
Correct |
6 ms |
2680 KB |
Output is correct |
14 |
Correct |
7 ms |
2680 KB |
Output is correct |
15 |
Correct |
6 ms |
2680 KB |
Output is correct |
16 |
Correct |
6 ms |
2680 KB |
Output is correct |
17 |
Correct |
6 ms |
2680 KB |
Output is correct |
18 |
Correct |
6 ms |
2680 KB |
Output is correct |
19 |
Correct |
7 ms |
2856 KB |
Output is correct |
20 |
Correct |
6 ms |
2680 KB |
Output is correct |
21 |
Correct |
6 ms |
2680 KB |
Output is correct |
22 |
Correct |
6 ms |
2680 KB |
Output is correct |
23 |
Correct |
6 ms |
2680 KB |
Output is correct |
24 |
Correct |
6 ms |
2680 KB |
Output is correct |
25 |
Correct |
6 ms |
2680 KB |
Output is correct |
26 |
Correct |
6 ms |
2808 KB |
Output is correct |
27 |
Correct |
6 ms |
2808 KB |
Output is correct |
28 |
Correct |
6 ms |
2680 KB |
Output is correct |
29 |
Correct |
6 ms |
2808 KB |
Output is correct |
30 |
Correct |
6 ms |
2680 KB |
Output is correct |
31 |
Correct |
6 ms |
2680 KB |
Output is correct |
32 |
Correct |
6 ms |
2680 KB |
Output is correct |
33 |
Correct |
6 ms |
2680 KB |
Output is correct |
34 |
Correct |
6 ms |
2680 KB |
Output is correct |
35 |
Correct |
6 ms |
2680 KB |
Output is correct |
36 |
Correct |
23 ms |
5700 KB |
Output is correct |
37 |
Correct |
25 ms |
5944 KB |
Output is correct |
38 |
Correct |
24 ms |
5944 KB |
Output is correct |
39 |
Correct |
27 ms |
5944 KB |
Output is correct |
40 |
Correct |
25 ms |
5944 KB |
Output is correct |
41 |
Correct |
26 ms |
5944 KB |
Output is correct |
42 |
Correct |
26 ms |
5944 KB |
Output is correct |
43 |
Correct |
19 ms |
4736 KB |
Output is correct |
44 |
Correct |
21 ms |
5220 KB |
Output is correct |
45 |
Correct |
25 ms |
5944 KB |
Output is correct |
46 |
Correct |
25 ms |
5944 KB |
Output is correct |
47 |
Correct |
25 ms |
5944 KB |
Output is correct |
48 |
Correct |
26 ms |
5944 KB |
Output is correct |
49 |
Correct |
18 ms |
4856 KB |
Output is correct |
50 |
Correct |
25 ms |
5944 KB |
Output is correct |
51 |
Correct |
25 ms |
5940 KB |
Output is correct |
52 |
Correct |
22 ms |
5824 KB |
Output is correct |
53 |
Correct |
23 ms |
5940 KB |
Output is correct |
54 |
Correct |
21 ms |
5944 KB |
Output is correct |
55 |
Correct |
22 ms |
5940 KB |
Output is correct |
56 |
Correct |
21 ms |
5944 KB |
Output is correct |