#include <bits/stdc++.h>
using namespace std;
template<typename T>
void out(T x) { cout << x << endl; exit(0); }
#define watch(x) cout << (#x) << " is " << (x) << endl
using ll = long long;
const int maxn = 1e6 + 5;
int n,k;
string s;
int a[maxn];
int acc[maxn][3];
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin>>n>>k;
cin>>s;
for (int i=0; i<n; i++) {
if (s[i]=='J') {
a[i+1]=0;
} else if (s[i]=='O') {
a[i+1]=1;
} else if (s[i]=='I') {
a[i+1]=2;
} else assert(false);
}
for (int i=1; i<=n; i++) {
acc[i][a[i]]++;
for (int j=0; j<3; j++) {
acc[i][j] += acc[i-1][j];
}
}
auto qry = [&](int l, int r, int i) {
return acc[r][i] - acc[l-1][i];
};
int res = 2*n;
for (int i=1; i<=n; i++) {
if (a[i]==1) {
if (qry(1,i,0)<k || qry(i,n,1)<k || qry(1,n,2)<k) {
continue;
}
int L = -1, R = -1;
// reach back for J
{
int lo = 1;
int hi = i+1;
while (hi-lo>1) {
int mid=(lo+hi)/2;
if (qry(mid,i,0)>=k) {
lo = mid;
} else {
hi = mid;
}
}
L = lo;
}
int I = -1;
// find kth I
{
int lo = i-1;
int hi = n;
while (hi-lo>1) {
int mid=(lo+hi)/2;
if (qry(i,mid,1)>=k) {
hi = mid;
} else {
lo = mid;
}
}
I = hi;
}
if (qry(I,n,2)<k) {
continue;
}
// find kth O
{
int lo = I-1;
int hi = n;
while (hi-lo>1) {
int mid=(lo+hi)/2;
if (qry(I,mid,2)>=k) {
hi = mid;
} else {
lo = mid;
}
}
R = hi;
}
// watch(i);
// cout<<L<<" "<<I<<" "<<R<<endl;
int cur = (R-L+1) - 3*k;
res = min(res, cur);
}
}
if (res>n) res=-1;
cout<<res<<endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
2 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
1 ms |
364 KB |
Output is correct |
31 |
Correct |
1 ms |
384 KB |
Output is correct |
32 |
Correct |
1 ms |
364 KB |
Output is correct |
33 |
Correct |
1 ms |
364 KB |
Output is correct |
34 |
Correct |
1 ms |
364 KB |
Output is correct |
35 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
2 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
1 ms |
364 KB |
Output is correct |
31 |
Correct |
1 ms |
384 KB |
Output is correct |
32 |
Correct |
1 ms |
364 KB |
Output is correct |
33 |
Correct |
1 ms |
364 KB |
Output is correct |
34 |
Correct |
1 ms |
364 KB |
Output is correct |
35 |
Correct |
1 ms |
364 KB |
Output is correct |
36 |
Correct |
24 ms |
3576 KB |
Output is correct |
37 |
Correct |
35 ms |
3832 KB |
Output is correct |
38 |
Correct |
27 ms |
3832 KB |
Output is correct |
39 |
Correct |
28 ms |
3704 KB |
Output is correct |
40 |
Correct |
13 ms |
3960 KB |
Output is correct |
41 |
Correct |
23 ms |
3960 KB |
Output is correct |
42 |
Correct |
28 ms |
3960 KB |
Output is correct |
43 |
Correct |
7 ms |
2668 KB |
Output is correct |
44 |
Correct |
8 ms |
3192 KB |
Output is correct |
45 |
Correct |
13 ms |
3960 KB |
Output is correct |
46 |
Correct |
12 ms |
3960 KB |
Output is correct |
47 |
Correct |
11 ms |
3960 KB |
Output is correct |
48 |
Correct |
11 ms |
3960 KB |
Output is correct |
49 |
Correct |
9 ms |
2808 KB |
Output is correct |
50 |
Correct |
12 ms |
3960 KB |
Output is correct |
51 |
Correct |
11 ms |
4012 KB |
Output is correct |
52 |
Correct |
7 ms |
3880 KB |
Output is correct |
53 |
Correct |
7 ms |
3960 KB |
Output is correct |
54 |
Correct |
25 ms |
4056 KB |
Output is correct |
55 |
Correct |
11 ms |
3960 KB |
Output is correct |
56 |
Correct |
5 ms |
3960 KB |
Output is correct |