#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define cerr if(false) cerr
#define watch(x) cerr << (#x) << " is " << (x) << endl;
#define endl '\n'
#define ld long double
#define int long long
#define pii pair<int, int>
#define fi first
#define se second
#define sz(a) (int)(a).size()
#define all(x) (x).begin(), (x).end()
const int maxn = 200500;
int n, k;
string s;
int psa[5][maxn];
int query(int i, int l, int r) {
return psa[i][r] - psa[i][l-1];
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k;
cin >> s;
s = "."+s;
for(int i = 1; i <= n; i++) {
if(s[i] == 'J') psa[1][i]++;
if(s[i] == 'O') psa[2][i]++;
if(s[i] == 'I') psa[3][i]++;
}
for(int i = 1; i <= 3; i++) {
for(int j = 1; j <= n; j++) {
psa[i][j] += psa[i][j-1];
}
}
int ans = 1e18;
// start j from i
for(int i = 1; i <= n; i++) {
int l = 1, r = n;
int lg = -1;
while(l <= r) {
int mid = (l+r)/2;
if(query(1, i, mid) >= k) {
r = mid-1;
lg = mid;
} else {
l = mid+1;
}
}
if(lg == -1) continue;
int lst = lg;
l = lst+1, r = n;
lg = -1;
while(l <= r) {
int mid = (l+r)/2;
if(query(2, lst+1, mid) >= k) {
r = mid-1;
lg = mid;
} else {
l = mid+1;
}
}
if(lg == -1) continue;
lst = lg;
l = lst+1, r = n;
lg = -1;
while(l <= r) {
int mid = (l+r)/2;
if(query(3, lst+1, mid) >= k) {
r = mid-1;
lg = mid;
} else {
l = mid+1;
}
}
if(lg == -1) continue;
ans = min(ans, lg-i+1 - 3*k);
}
if(ans == 1e18) {
cout << -1 << endl;
} else {
cout << ans << endl;
}
}
/*
find smallest subarray with k-level JOI as subsequence
bsearch
10 2
OJIJOIOIIJ
9 3
JJJOOOIII
9 3
IIIOOOJJJ
Did you read the bounds?
Did you make typos?
Are there edge cases (N=1?)
Are array sizes proper?
Integer overflow?
DS reset properly between test cases?
Is using long longs causing TLE?
Are you using floating points?
*/
# |
결과 |
실행 시간 |
메모리 |
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 |
0 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 |
# |
결과 |
실행 시간 |
메모리 |
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 |
0 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 |
3 ms |
492 KB |
Output is correct |
17 |
Correct |
1 ms |
512 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
492 KB |
Output is correct |
20 |
Correct |
1 ms |
492 KB |
Output is correct |
21 |
Correct |
1 ms |
492 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
492 KB |
Output is correct |
25 |
Correct |
1 ms |
492 KB |
Output is correct |
26 |
Correct |
1 ms |
492 KB |
Output is correct |
27 |
Correct |
1 ms |
492 KB |
Output is correct |
28 |
Correct |
1 ms |
640 KB |
Output is correct |
29 |
Correct |
1 ms |
492 KB |
Output is correct |
30 |
Correct |
1 ms |
492 KB |
Output is correct |
31 |
Correct |
1 ms |
364 KB |
Output is correct |
32 |
Correct |
1 ms |
492 KB |
Output is correct |
33 |
Correct |
1 ms |
492 KB |
Output is correct |
34 |
Correct |
1 ms |
492 KB |
Output is correct |
35 |
Correct |
1 ms |
492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
0 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 |
3 ms |
492 KB |
Output is correct |
17 |
Correct |
1 ms |
512 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
492 KB |
Output is correct |
20 |
Correct |
1 ms |
492 KB |
Output is correct |
21 |
Correct |
1 ms |
492 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
492 KB |
Output is correct |
25 |
Correct |
1 ms |
492 KB |
Output is correct |
26 |
Correct |
1 ms |
492 KB |
Output is correct |
27 |
Correct |
1 ms |
492 KB |
Output is correct |
28 |
Correct |
1 ms |
640 KB |
Output is correct |
29 |
Correct |
1 ms |
492 KB |
Output is correct |
30 |
Correct |
1 ms |
492 KB |
Output is correct |
31 |
Correct |
1 ms |
364 KB |
Output is correct |
32 |
Correct |
1 ms |
492 KB |
Output is correct |
33 |
Correct |
1 ms |
492 KB |
Output is correct |
34 |
Correct |
1 ms |
492 KB |
Output is correct |
35 |
Correct |
1 ms |
492 KB |
Output is correct |
36 |
Correct |
25 ms |
5056 KB |
Output is correct |
37 |
Correct |
27 ms |
5556 KB |
Output is correct |
38 |
Correct |
28 ms |
5684 KB |
Output is correct |
39 |
Correct |
28 ms |
5556 KB |
Output is correct |
40 |
Correct |
25 ms |
5556 KB |
Output is correct |
41 |
Correct |
32 ms |
5556 KB |
Output is correct |
42 |
Correct |
30 ms |
5556 KB |
Output is correct |
43 |
Correct |
12 ms |
3820 KB |
Output is correct |
44 |
Correct |
15 ms |
4448 KB |
Output is correct |
45 |
Correct |
20 ms |
5556 KB |
Output is correct |
46 |
Correct |
19 ms |
5556 KB |
Output is correct |
47 |
Correct |
19 ms |
5556 KB |
Output is correct |
48 |
Correct |
19 ms |
5556 KB |
Output is correct |
49 |
Correct |
15 ms |
3700 KB |
Output is correct |
50 |
Correct |
19 ms |
5556 KB |
Output is correct |
51 |
Correct |
22 ms |
5556 KB |
Output is correct |
52 |
Correct |
11 ms |
5308 KB |
Output is correct |
53 |
Correct |
12 ms |
5556 KB |
Output is correct |
54 |
Correct |
18 ms |
5556 KB |
Output is correct |
55 |
Correct |
12 ms |
5556 KB |
Output is correct |
56 |
Correct |
10 ms |
5556 KB |
Output is correct |