#include <bits/stdc++.h>
#define DIM 200010
using namespace std;
int sp_j[DIM],sp_i[DIM],sp_o[DIM],cnt_left[DIM],cnt_right[DIM];
char v[DIM];
int n,k,i;
int main (){
//ifstream cin ("date.in");
//ofstream cout ("date.out");
cin>>n>>k>>v+1;
for (i=1;i<=n;i++){
sp_j[i] = sp_j[i-1] + (v[i] == 'J');
sp_o[i] = sp_o[i-1] + (v[i] == 'O');
sp_i[i] = sp_i[i-1] + (v[i] == 'I');
}
/// cnt_left[i] - nr minim de caractere pe care trebuie sa le sterg a.i sa am k de J
cnt_left[0] = -1;
for (i=1;i<=n;i++){
int st = 1, dr = i, sol = -1;
while (st <= dr){
int mid = (st+dr)>>1;
if (sp_j[i] - sp_j[mid-1] >= k){
sol = mid;
st = mid+1;
} else dr = mid-1;
}
if (sol == -1)
cnt_left[i] = -1;
else cnt_left[i] = i - sol + 1 - k;
}
/// cnt_right[i] - sa am k de I de la poz i in dreapta
for (i=1;i<=n;i++){
int st = i, dr = n, sol = -1;
while (st <= dr){
int mid = (st+dr)>>1;
if (sp_i[mid] - sp_i[i-1] >= k){
sol = mid;
dr = mid-1;
} else st = mid+1;
}
if (sol == -1)
cnt_right[i] = -1;
else cnt_right[i] = sol - i + 1 - k;
}
int ans = n+1;
for (i=1;i<=n;i++){
if (cnt_right[i] == -1)
continue;
int st = 1, dr = i-1, sol = -1;
while (st <= dr){
int mid = (st+dr)>>1;
if (sp_o[i-1] - sp_o[mid-1] >= k){
sol = mid;
st = mid+1;
} else dr = mid-1;
}
if (sol != -1 && cnt_left[sol-1] != -1)
ans = min (ans, cnt_right[i] + cnt_left[sol-1] + i-1 - sol + 1 - k);
}
if (ans == n+1)
cout<<-1;
else cout<<ans;
return 0;
}
Compilation message
ho_t2.cpp: In function 'int main()':
ho_t2.cpp:13:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
cin>>n>>k>>v+1;
~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
384 KB |
Output is correct |
12 |
Correct |
4 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
4 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
384 KB |
Output is correct |
12 |
Correct |
4 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
4 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
26 |
Correct |
5 ms |
384 KB |
Output is correct |
27 |
Correct |
5 ms |
384 KB |
Output is correct |
28 |
Correct |
5 ms |
384 KB |
Output is correct |
29 |
Correct |
6 ms |
384 KB |
Output is correct |
30 |
Correct |
5 ms |
384 KB |
Output is correct |
31 |
Correct |
5 ms |
384 KB |
Output is correct |
32 |
Correct |
5 ms |
384 KB |
Output is correct |
33 |
Correct |
5 ms |
384 KB |
Output is correct |
34 |
Correct |
5 ms |
384 KB |
Output is correct |
35 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
384 KB |
Output is correct |
12 |
Correct |
4 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
4 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
26 |
Correct |
5 ms |
384 KB |
Output is correct |
27 |
Correct |
5 ms |
384 KB |
Output is correct |
28 |
Correct |
5 ms |
384 KB |
Output is correct |
29 |
Correct |
6 ms |
384 KB |
Output is correct |
30 |
Correct |
5 ms |
384 KB |
Output is correct |
31 |
Correct |
5 ms |
384 KB |
Output is correct |
32 |
Correct |
5 ms |
384 KB |
Output is correct |
33 |
Correct |
5 ms |
384 KB |
Output is correct |
34 |
Correct |
5 ms |
384 KB |
Output is correct |
35 |
Correct |
5 ms |
384 KB |
Output is correct |
36 |
Correct |
32 ms |
4352 KB |
Output is correct |
37 |
Correct |
36 ms |
4644 KB |
Output is correct |
38 |
Correct |
35 ms |
4608 KB |
Output is correct |
39 |
Correct |
39 ms |
4608 KB |
Output is correct |
40 |
Correct |
36 ms |
4600 KB |
Output is correct |
41 |
Correct |
39 ms |
4600 KB |
Output is correct |
42 |
Correct |
38 ms |
4608 KB |
Output is correct |
43 |
Correct |
23 ms |
2944 KB |
Output is correct |
44 |
Correct |
28 ms |
3712 KB |
Output is correct |
45 |
Correct |
35 ms |
4600 KB |
Output is correct |
46 |
Correct |
35 ms |
4608 KB |
Output is correct |
47 |
Correct |
35 ms |
4608 KB |
Output is correct |
48 |
Correct |
35 ms |
4608 KB |
Output is correct |
49 |
Correct |
25 ms |
3328 KB |
Output is correct |
50 |
Correct |
35 ms |
4608 KB |
Output is correct |
51 |
Correct |
36 ms |
4600 KB |
Output is correct |
52 |
Correct |
25 ms |
4488 KB |
Output is correct |
53 |
Correct |
26 ms |
4600 KB |
Output is correct |
54 |
Correct |
39 ms |
4648 KB |
Output is correct |
55 |
Correct |
34 ms |
4600 KB |
Output is correct |
56 |
Correct |
27 ms |
4600 KB |
Output is correct |