#include "bits/stdc++.h"
using namespace std;
void fast() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
}
void ran() {
srand(chrono::steady_clock::now().time_since_epoch().count());
}
long long get_rand() {
long long a = rand();
long long b = rand();
return a * (RAND_MAX + 1ll) + b;
}
// #define endl '\n'
// #define double long double
// #define int long long
// int MOD = 1000 * 1000 * 1000 + 7;
// int MOD = 998244353;
int n, k;
string s;
const int MXN = 200005;
int pfJ[MXN];
int pfO[MXN];
int pfI[MXN];
int get(int pos) {
// K Js then K Os then K Is
// get the earliest position of that last I
int x = pfJ[pos-1] + k;
int l = lower_bound(pfJ, pfJ+n+1, x) - pfJ;
if (l == n+1) return INT_MAX;
int y = pfO[l] + k;
l = lower_bound(pfO, pfO+n+1, y) - pfO;
if (l == n+1) return INT_MAX;
int z = pfI[l] + k;
l = lower_bound(pfI, pfI+n+1, z) - pfI;
if (l == n+1) return INT_MAX;
return l;
}
signed main() {
ran(); fast();
cin >> n >> k >> s;
for (int i=1; i<=n; i++) {
pfJ[i] = pfJ[i-1];
pfO[i] = pfO[i-1];
pfI[i] = pfI[i-1];
if (s[i-1] == 'J') pfJ[i]++;
if (s[i-1] == 'O') pfO[i]++;
if (s[i-1] == 'I') pfI[i]++;
}
for (int i=n+1; i<MXN; i++) {
pfJ[i] = pfJ[i-1];
pfO[i] = pfO[i-1];
pfI[i] = pfI[i-1];
}
int mn = INT_MAX;
for (int i=1; i<=n; i++) {
// for every 'start'
int e = get(i);
if (e == INT_MAX) continue;
// cout << i << ' ' << e << endl;
mn = min(mn, e - i + 1 - 3 * k);
}
if (mn == INT_MAX) cout << -1 << endl;
else cout << mn << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2688 KB |
Output is correct |
2 |
Correct |
7 ms |
2560 KB |
Output is correct |
3 |
Correct |
7 ms |
2688 KB |
Output is correct |
4 |
Correct |
6 ms |
2688 KB |
Output is correct |
5 |
Correct |
6 ms |
2688 KB |
Output is correct |
6 |
Correct |
6 ms |
2688 KB |
Output is correct |
7 |
Correct |
6 ms |
2688 KB |
Output is correct |
8 |
Correct |
6 ms |
2688 KB |
Output is correct |
9 |
Correct |
6 ms |
2688 KB |
Output is correct |
10 |
Correct |
7 ms |
2688 KB |
Output is correct |
11 |
Correct |
7 ms |
2688 KB |
Output is correct |
12 |
Correct |
6 ms |
2688 KB |
Output is correct |
13 |
Correct |
6 ms |
2688 KB |
Output is correct |
14 |
Correct |
6 ms |
2688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2688 KB |
Output is correct |
2 |
Correct |
7 ms |
2560 KB |
Output is correct |
3 |
Correct |
7 ms |
2688 KB |
Output is correct |
4 |
Correct |
6 ms |
2688 KB |
Output is correct |
5 |
Correct |
6 ms |
2688 KB |
Output is correct |
6 |
Correct |
6 ms |
2688 KB |
Output is correct |
7 |
Correct |
6 ms |
2688 KB |
Output is correct |
8 |
Correct |
6 ms |
2688 KB |
Output is correct |
9 |
Correct |
6 ms |
2688 KB |
Output is correct |
10 |
Correct |
7 ms |
2688 KB |
Output is correct |
11 |
Correct |
7 ms |
2688 KB |
Output is correct |
12 |
Correct |
6 ms |
2688 KB |
Output is correct |
13 |
Correct |
6 ms |
2688 KB |
Output is correct |
14 |
Correct |
6 ms |
2688 KB |
Output is correct |
15 |
Correct |
7 ms |
2688 KB |
Output is correct |
16 |
Correct |
7 ms |
2688 KB |
Output is correct |
17 |
Correct |
7 ms |
2688 KB |
Output is correct |
18 |
Correct |
6 ms |
2688 KB |
Output is correct |
19 |
Correct |
7 ms |
2688 KB |
Output is correct |
20 |
Correct |
7 ms |
2688 KB |
Output is correct |
21 |
Correct |
7 ms |
2688 KB |
Output is correct |
22 |
Correct |
6 ms |
2688 KB |
Output is correct |
23 |
Correct |
6 ms |
2688 KB |
Output is correct |
24 |
Correct |
8 ms |
2688 KB |
Output is correct |
25 |
Correct |
6 ms |
2688 KB |
Output is correct |
26 |
Correct |
7 ms |
2688 KB |
Output is correct |
27 |
Correct |
6 ms |
2688 KB |
Output is correct |
28 |
Correct |
8 ms |
2688 KB |
Output is correct |
29 |
Correct |
6 ms |
2688 KB |
Output is correct |
30 |
Correct |
6 ms |
2688 KB |
Output is correct |
31 |
Correct |
6 ms |
2688 KB |
Output is correct |
32 |
Correct |
6 ms |
2688 KB |
Output is correct |
33 |
Correct |
6 ms |
2688 KB |
Output is correct |
34 |
Correct |
6 ms |
2688 KB |
Output is correct |
35 |
Correct |
8 ms |
2688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2688 KB |
Output is correct |
2 |
Correct |
7 ms |
2560 KB |
Output is correct |
3 |
Correct |
7 ms |
2688 KB |
Output is correct |
4 |
Correct |
6 ms |
2688 KB |
Output is correct |
5 |
Correct |
6 ms |
2688 KB |
Output is correct |
6 |
Correct |
6 ms |
2688 KB |
Output is correct |
7 |
Correct |
6 ms |
2688 KB |
Output is correct |
8 |
Correct |
6 ms |
2688 KB |
Output is correct |
9 |
Correct |
6 ms |
2688 KB |
Output is correct |
10 |
Correct |
7 ms |
2688 KB |
Output is correct |
11 |
Correct |
7 ms |
2688 KB |
Output is correct |
12 |
Correct |
6 ms |
2688 KB |
Output is correct |
13 |
Correct |
6 ms |
2688 KB |
Output is correct |
14 |
Correct |
6 ms |
2688 KB |
Output is correct |
15 |
Correct |
7 ms |
2688 KB |
Output is correct |
16 |
Correct |
7 ms |
2688 KB |
Output is correct |
17 |
Correct |
7 ms |
2688 KB |
Output is correct |
18 |
Correct |
6 ms |
2688 KB |
Output is correct |
19 |
Correct |
7 ms |
2688 KB |
Output is correct |
20 |
Correct |
7 ms |
2688 KB |
Output is correct |
21 |
Correct |
7 ms |
2688 KB |
Output is correct |
22 |
Correct |
6 ms |
2688 KB |
Output is correct |
23 |
Correct |
6 ms |
2688 KB |
Output is correct |
24 |
Correct |
8 ms |
2688 KB |
Output is correct |
25 |
Correct |
6 ms |
2688 KB |
Output is correct |
26 |
Correct |
7 ms |
2688 KB |
Output is correct |
27 |
Correct |
6 ms |
2688 KB |
Output is correct |
28 |
Correct |
8 ms |
2688 KB |
Output is correct |
29 |
Correct |
6 ms |
2688 KB |
Output is correct |
30 |
Correct |
6 ms |
2688 KB |
Output is correct |
31 |
Correct |
6 ms |
2688 KB |
Output is correct |
32 |
Correct |
6 ms |
2688 KB |
Output is correct |
33 |
Correct |
6 ms |
2688 KB |
Output is correct |
34 |
Correct |
6 ms |
2688 KB |
Output is correct |
35 |
Correct |
8 ms |
2688 KB |
Output is correct |
36 |
Correct |
40 ms |
3212 KB |
Output is correct |
37 |
Correct |
41 ms |
3212 KB |
Output is correct |
38 |
Correct |
36 ms |
3212 KB |
Output is correct |
39 |
Correct |
34 ms |
3356 KB |
Output is correct |
40 |
Correct |
26 ms |
3244 KB |
Output is correct |
41 |
Correct |
32 ms |
3212 KB |
Output is correct |
42 |
Correct |
35 ms |
3316 KB |
Output is correct |
43 |
Correct |
18 ms |
3200 KB |
Output is correct |
44 |
Correct |
21 ms |
3224 KB |
Output is correct |
45 |
Correct |
31 ms |
3212 KB |
Output is correct |
46 |
Correct |
24 ms |
3212 KB |
Output is correct |
47 |
Correct |
23 ms |
3212 KB |
Output is correct |
48 |
Correct |
23 ms |
3212 KB |
Output is correct |
49 |
Correct |
17 ms |
3084 KB |
Output is correct |
50 |
Correct |
24 ms |
3212 KB |
Output is correct |
51 |
Correct |
23 ms |
3212 KB |
Output is correct |
52 |
Correct |
16 ms |
3212 KB |
Output is correct |
53 |
Correct |
18 ms |
3212 KB |
Output is correct |
54 |
Correct |
25 ms |
3212 KB |
Output is correct |
55 |
Correct |
16 ms |
3212 KB |
Output is correct |
56 |
Correct |
14 ms |
3212 KB |
Output is correct |