#include <bits/stdc++.h>
#define DEBUG 0
using namespace std;
const int N = 2e5 + 10;
const int INF = 1e9 + 7;
char s[N];
int find(int x, vector <int> vec) {
int l = 0, r = vec.size() - 1, pos = -1;
while(l <= r) {
int mid = (l + r) / 2;
if(vec[mid] < x) {
l = mid + 1;
}
else {
pos = mid;
r = mid - 1;
}
}
return pos;
}
int main() {
cin.tie(0)->sync_with_stdio(0);
int n, k;
cin >> n >> k;
cin >> (s + 1);
vector <int> J, O, I;
for(int i = 1; i <= n; i++) {
if(s[i] == 'J') {
J.push_back(i);
}
else if(s[i] == 'O') {
O.push_back(i);
}
else {
I.push_back(i);
}
}
int ans = INF;
for(int i = 0; i + k - 1 < J.size(); i++) {
int pos_j = i + k - 1;
int pos_o = find(J[pos_j] + 1, O);
if(pos_o == -1 or pos_o + k - 1 >= O.size()) {
continue;
}
pos_o = pos_o + k - 1;
int pos_i = find(O[pos_o] + 1, I);
if(pos_i == -1 or pos_i + k - 1 >= I.size()) {
continue;
}
pos_i = pos_i + k - 1;
ans = min(ans, (I[pos_i] - O[pos_i] - k) + (O[pos_i] - J[pos_j] - k) + (J[pos_j] - J[pos_j - k + 1] - k + 1));
}
if(ans == INF) {
ans = -1;
}
cout << ans;
return 0;
}
Compilation message
ho_t2.cpp: In function 'int main()':
ho_t2.cpp:45:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | for(int i = 0; i + k - 1 < J.size(); i++) {
| ~~~~~~~~~~^~~~~~~~~~
ho_t2.cpp:48:41: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
48 | if(pos_o == -1 or pos_o + k - 1 >= O.size()) {
| ~~~~~~~~~~~~~~^~~~~~~~~~~
ho_t2.cpp:53:41: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
53 | if(pos_i == -1 or pos_i + k - 1 >= I.size()) {
| ~~~~~~~~~~~~~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
0 ms |
316 KB |
Output is correct |
4 |
Correct |
0 ms |
324 KB |
Output is correct |
5 |
Correct |
0 ms |
320 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 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 |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
0 ms |
316 KB |
Output is correct |
4 |
Correct |
0 ms |
324 KB |
Output is correct |
5 |
Correct |
0 ms |
320 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 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 |
308 KB |
Output is correct |
16 |
Correct |
1 ms |
312 KB |
Output is correct |
17 |
Correct |
1 ms |
316 KB |
Output is correct |
18 |
Correct |
1 ms |
320 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
320 KB |
Output is correct |
24 |
Correct |
1 ms |
324 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
316 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
308 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
320 KB |
Output is correct |
33 |
Correct |
1 ms |
312 KB |
Output is correct |
34 |
Correct |
1 ms |
332 KB |
Output is correct |
35 |
Correct |
1 ms |
316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
0 ms |
316 KB |
Output is correct |
4 |
Correct |
0 ms |
324 KB |
Output is correct |
5 |
Correct |
0 ms |
320 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 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 |
308 KB |
Output is correct |
16 |
Correct |
1 ms |
312 KB |
Output is correct |
17 |
Correct |
1 ms |
316 KB |
Output is correct |
18 |
Correct |
1 ms |
320 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
320 KB |
Output is correct |
24 |
Correct |
1 ms |
324 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
316 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
308 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
320 KB |
Output is correct |
33 |
Correct |
1 ms |
312 KB |
Output is correct |
34 |
Correct |
1 ms |
332 KB |
Output is correct |
35 |
Correct |
1 ms |
316 KB |
Output is correct |
36 |
Correct |
1246 ms |
1712 KB |
Output is correct |
37 |
Correct |
1358 ms |
1828 KB |
Output is correct |
38 |
Correct |
1389 ms |
1856 KB |
Output is correct |
39 |
Correct |
1249 ms |
1840 KB |
Output is correct |
40 |
Correct |
828 ms |
1868 KB |
Output is correct |
41 |
Correct |
1092 ms |
1868 KB |
Output is correct |
42 |
Correct |
1176 ms |
1868 KB |
Output is correct |
43 |
Correct |
298 ms |
1356 KB |
Output is correct |
44 |
Correct |
384 ms |
1484 KB |
Output is correct |
45 |
Correct |
699 ms |
1996 KB |
Output is correct |
46 |
Correct |
701 ms |
1820 KB |
Output is correct |
47 |
Correct |
637 ms |
1868 KB |
Output is correct |
48 |
Correct |
723 ms |
1992 KB |
Output is correct |
49 |
Correct |
276 ms |
1352 KB |
Output is correct |
50 |
Correct |
682 ms |
1872 KB |
Output is correct |
51 |
Correct |
729 ms |
1868 KB |
Output is correct |
52 |
Correct |
89 ms |
1736 KB |
Output is correct |
53 |
Correct |
109 ms |
1852 KB |
Output is correct |
54 |
Correct |
1377 ms |
2012 KB |
Output is correct |
55 |
Correct |
402 ms |
1992 KB |
Output is correct |
56 |
Correct |
3 ms |
1896 KB |
Output is correct |