#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
signed main() {
int n, k;
cin >> n>> k;
string s;
cin >> s;
vector<int>v(n);
for (int i = 0 ; i < n ; i++) {
if (s[i] == 'J') v[i] = 0;
else if (s[i] == 'O') v[i] = 1;
else v[i] = 2;
}
vector<vector<int> >num(n, vector<int>(3, 0));
vector<vector<int> >pos(3);
num[0][v[0]]++;
pos[v[0]].push_back(0);
for (int i = 1 ; i < n ; i++) {
num[i] = num[i-1];
num[i][v[i]] = num[i-1][v[i]]+1;
pos[v[i]].push_back(i);
}
int mn = -1;
for (int i = 0 ; i < n ; i++) {
int l = i+k*3-2, r = n;
while (l+1 < r) {
int j = (l + r) / 2;
//cout << l << " " << r << " " << j << endl;
int numj, numi;
if (i == 0) numj = 0;
else numj = num[i-1][0];
numi = num[j][2];
if (numj + k > pos[0].size() || numi < k) {
l = j;
continue;
}
int posj = pos[0][(numj + k) - 1];
int posi = pos[2][(numi - k + 1) - 1];
if (num[posi][1] - num[posj][1] >= k) {
//cout << i << " " << j << "\n";
r = j;
} else {
l = j;
}
}
if (r == n) break;
//cout << i << " " << r << "\n";
if (mn == -1) mn = r - i + 1;
else mn = min(mn, r-i+1);
//for (int j = i+k*3-1 ; j < n ; j++) {
//}
}
if (mn == -1) cout << "-1\n";
else cout << mn - k * 3 << "\n";
}
Compilation message
ho_t2.cpp: In function 'int main()':
ho_t2.cpp:36:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
36 | if (numj + k > pos[0].size() || numi < k) {
| ~~~~~~~~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
428 KB |
Output is correct |
21 |
Correct |
1 ms |
600 KB |
Output is correct |
22 |
Correct |
0 ms |
604 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
0 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
600 KB |
Output is correct |
27 |
Correct |
0 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
0 ms |
604 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
1 ms |
604 KB |
Output is correct |
34 |
Correct |
1 ms |
604 KB |
Output is correct |
35 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
428 KB |
Output is correct |
21 |
Correct |
1 ms |
600 KB |
Output is correct |
22 |
Correct |
0 ms |
604 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
0 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
600 KB |
Output is correct |
27 |
Correct |
0 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
0 ms |
604 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
1 ms |
604 KB |
Output is correct |
34 |
Correct |
1 ms |
604 KB |
Output is correct |
35 |
Correct |
1 ms |
604 KB |
Output is correct |
36 |
Correct |
48 ms |
12312 KB |
Output is correct |
37 |
Correct |
52 ms |
13344 KB |
Output is correct |
38 |
Correct |
51 ms |
13404 KB |
Output is correct |
39 |
Correct |
52 ms |
13516 KB |
Output is correct |
40 |
Correct |
22 ms |
13672 KB |
Output is correct |
41 |
Correct |
41 ms |
13928 KB |
Output is correct |
42 |
Correct |
55 ms |
13668 KB |
Output is correct |
43 |
Correct |
9 ms |
8680 KB |
Output is correct |
44 |
Correct |
12 ms |
10568 KB |
Output is correct |
45 |
Correct |
15 ms |
13744 KB |
Output is correct |
46 |
Correct |
16 ms |
13672 KB |
Output is correct |
47 |
Correct |
17 ms |
13684 KB |
Output is correct |
48 |
Correct |
15 ms |
13800 KB |
Output is correct |
49 |
Correct |
11 ms |
9064 KB |
Output is correct |
50 |
Correct |
15 ms |
13756 KB |
Output is correct |
51 |
Correct |
17 ms |
13676 KB |
Output is correct |
52 |
Correct |
14 ms |
12904 KB |
Output is correct |
53 |
Correct |
16 ms |
13912 KB |
Output is correct |
54 |
Correct |
24 ms |
13800 KB |
Output is correct |
55 |
Correct |
19 ms |
13800 KB |
Output is correct |
56 |
Correct |
17 ms |
14312 KB |
Output is correct |