#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
mt19937_64 rng((int)std::chrono::steady_clock::now().time_since_epoch().count());
const int MAXN = 3e5 + 10;
const int MOD = 1e9 + 7;
#define int long long
int rnd(int x, int y) { // random number generator
int u= uniform_int_distribution<int>(x, y)(rng); return u;
}
void solve(int tc) {
int n, k;
cin >> n >> k;
string s;
cin >> s;
int freq[n+1][3];
for(int i=0; i<3; i++) freq[0][i] = 0;
for(int i=0; i<n; i++) s[i] = (s[i] == 'J' ? '0' : (s[i] == 'O' ? '1' : '2'));
for(int i=1; i<=n; i++) {
for(int j=0; j<3; j++) {
freq[i][j] = freq[i-1][j] + (s[i-1] == j + '0');
}
}
int ans = 1e18;
for(int i=1; i<=n; i++) {
bool valid = 1;
int lb = i, rb = n;
while(lb<rb) {
int mid = (lb+rb) >> 1;
if(freq[mid][0] - freq[i-1][0] >= k) rb = mid;
else lb = mid+1;
}
valid &= (freq[lb][0] - freq[i-1][0] == k);
rb = n;
int sto = lb;
lb++;
while(lb<rb) {
int mid = (lb+rb) >> 1;
if(freq[mid][1] - freq[sto][1] >= k) rb = mid;
else lb = mid+1;
}
valid &= (freq[lb][1] - freq[sto][1] == k);
rb = n;
sto = lb;
lb++;
while(lb<rb) {
int mid = (lb+rb) >> 1;
if(freq[mid][2] - freq[sto][2] >= k) rb = mid;
else lb = mid+1;
}
valid &= (freq[lb][2] - freq[sto][2] == k);
if(valid) ans = min(ans, lb-i + 1 - 3*k);
}
cout << (ans == 1e18? -1 : ans);
}
int32_t main(){
ios::sync_with_stdio(0); cin.tie(0);
int t = 1; //cin >> t;
for(int i=1; i<=t; i++) solve(i);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
316 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
316 KB |
Output is correct |
9 |
Correct |
0 ms |
316 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
320 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
316 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
316 KB |
Output is correct |
9 |
Correct |
0 ms |
316 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
320 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
316 KB |
Output is correct |
16 |
Correct |
1 ms |
316 KB |
Output is correct |
17 |
Correct |
1 ms |
308 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
316 KB |
Output is correct |
21 |
Correct |
1 ms |
316 KB |
Output is correct |
22 |
Correct |
1 ms |
316 KB |
Output is correct |
23 |
Correct |
1 ms |
296 KB |
Output is correct |
24 |
Correct |
1 ms |
324 KB |
Output is correct |
25 |
Correct |
1 ms |
324 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
0 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
320 KB |
Output is correct |
30 |
Correct |
1 ms |
312 KB |
Output is correct |
31 |
Correct |
0 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
1 ms |
316 KB |
Output is correct |
34 |
Correct |
0 ms |
312 KB |
Output is correct |
35 |
Correct |
1 ms |
312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
316 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
316 KB |
Output is correct |
9 |
Correct |
0 ms |
316 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
320 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
316 KB |
Output is correct |
16 |
Correct |
1 ms |
316 KB |
Output is correct |
17 |
Correct |
1 ms |
308 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
316 KB |
Output is correct |
21 |
Correct |
1 ms |
316 KB |
Output is correct |
22 |
Correct |
1 ms |
316 KB |
Output is correct |
23 |
Correct |
1 ms |
296 KB |
Output is correct |
24 |
Correct |
1 ms |
324 KB |
Output is correct |
25 |
Correct |
1 ms |
324 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
0 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
320 KB |
Output is correct |
30 |
Correct |
1 ms |
312 KB |
Output is correct |
31 |
Correct |
0 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
1 ms |
316 KB |
Output is correct |
34 |
Correct |
0 ms |
312 KB |
Output is correct |
35 |
Correct |
1 ms |
312 KB |
Output is correct |
36 |
Correct |
19 ms |
5088 KB |
Output is correct |
37 |
Correct |
20 ms |
5456 KB |
Output is correct |
38 |
Correct |
19 ms |
5520 KB |
Output is correct |
39 |
Correct |
24 ms |
5480 KB |
Output is correct |
40 |
Correct |
19 ms |
5464 KB |
Output is correct |
41 |
Correct |
24 ms |
5464 KB |
Output is correct |
42 |
Correct |
31 ms |
5464 KB |
Output is correct |
43 |
Correct |
12 ms |
3532 KB |
Output is correct |
44 |
Correct |
13 ms |
4312 KB |
Output is correct |
45 |
Correct |
18 ms |
5464 KB |
Output is correct |
46 |
Correct |
23 ms |
5452 KB |
Output is correct |
47 |
Correct |
18 ms |
5440 KB |
Output is correct |
48 |
Correct |
18 ms |
5464 KB |
Output is correct |
49 |
Correct |
12 ms |
3792 KB |
Output is correct |
50 |
Correct |
27 ms |
5464 KB |
Output is correct |
51 |
Correct |
18 ms |
5464 KB |
Output is correct |
52 |
Correct |
18 ms |
5220 KB |
Output is correct |
53 |
Correct |
14 ms |
5484 KB |
Output is correct |
54 |
Correct |
22 ms |
5464 KB |
Output is correct |
55 |
Correct |
12 ms |
5436 KB |
Output is correct |
56 |
Correct |
16 ms |
5420 KB |
Output is correct |