#include <bits/stdc++.h>
using namespace std;
const int k_N = 2e5 + 3;
int n, k;
string s, joi = "JOI";
int nxt[k_N][3];
int cnt[k_N][3];
int sum(int l, int r, int idx){
if(l == 0) return cnt[r][idx];
return cnt[r][idx] - cnt[l - 1][idx];
}
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> k;
cin >> s;
for(int j = 0; j < 3; ++j)
cnt[0][j] = (s[0] == joi[j]);
for(int i = 1; i < s.size(); ++i)
for(int j = 0; j < 3; ++j)
cnt[i][j] = (s[i] == joi[j]) + cnt[i - 1][j];
for(int i = 0; i < n; ++i){
for(int idx = 0; idx < 3; ++idx){
int l = i, r = n;
while(l != r){
int mid = (l + r) >> 1;
if(sum(i, mid, idx) >= k)
r = mid;
else
l = mid + 1;
}
nxt[i][idx] = l;
}
//cout << "nxt[" << i << "] = " << l << "\n";
}
nxt[n][0] = nxt[n][1] = nxt[n][2] = n;
int ans = n + 1;
for(int i = 0; i < n; ++i){
if(s[i] != 'J') continue;
int curr = i;
if(nxt[curr][0] != n){
curr = nxt[curr][0] + 1;
if(nxt[curr][1] != n){
curr = nxt[curr][1] + 1;
if(nxt[curr][2] != n){
int en = nxt[curr][2];
ans = min(ans, en - i + 1 - 3 * k);
}
}
}
}
if(ans == n + 1) ans = -1;
cout << ans << "\n";
}
Compilation message
ho_t2.cpp: In function 'int main()':
ho_t2.cpp:28:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 1; i < s.size(); ++i)
~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
512 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
2 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
384 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
1 ms |
384 KB |
Output is correct |
29 |
Correct |
1 ms |
384 KB |
Output is correct |
30 |
Correct |
1 ms |
384 KB |
Output is correct |
31 |
Correct |
1 ms |
384 KB |
Output is correct |
32 |
Correct |
1 ms |
384 KB |
Output is correct |
33 |
Correct |
1 ms |
384 KB |
Output is correct |
34 |
Correct |
1 ms |
384 KB |
Output is correct |
35 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
512 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
2 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
384 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
1 ms |
384 KB |
Output is correct |
29 |
Correct |
1 ms |
384 KB |
Output is correct |
30 |
Correct |
1 ms |
384 KB |
Output is correct |
31 |
Correct |
1 ms |
384 KB |
Output is correct |
32 |
Correct |
1 ms |
384 KB |
Output is correct |
33 |
Correct |
1 ms |
384 KB |
Output is correct |
34 |
Correct |
1 ms |
384 KB |
Output is correct |
35 |
Correct |
1 ms |
384 KB |
Output is correct |
36 |
Correct |
26 ms |
5260 KB |
Output is correct |
37 |
Correct |
29 ms |
5508 KB |
Output is correct |
38 |
Correct |
26 ms |
5508 KB |
Output is correct |
39 |
Correct |
39 ms |
5508 KB |
Output is correct |
40 |
Correct |
45 ms |
5508 KB |
Output is correct |
41 |
Correct |
45 ms |
5508 KB |
Output is correct |
42 |
Correct |
38 ms |
5512 KB |
Output is correct |
43 |
Correct |
29 ms |
3584 KB |
Output is correct |
44 |
Correct |
27 ms |
4356 KB |
Output is correct |
45 |
Correct |
33 ms |
5504 KB |
Output is correct |
46 |
Correct |
35 ms |
5508 KB |
Output is correct |
47 |
Correct |
33 ms |
5508 KB |
Output is correct |
48 |
Correct |
34 ms |
5512 KB |
Output is correct |
49 |
Correct |
23 ms |
3852 KB |
Output is correct |
50 |
Correct |
33 ms |
5516 KB |
Output is correct |
51 |
Correct |
34 ms |
5508 KB |
Output is correct |
52 |
Correct |
23 ms |
5388 KB |
Output is correct |
53 |
Correct |
26 ms |
5668 KB |
Output is correct |
54 |
Correct |
25 ms |
5516 KB |
Output is correct |
55 |
Correct |
25 ms |
5516 KB |
Output is correct |
56 |
Correct |
22 ms |
5508 KB |
Output is correct |