#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9+10;
int n, k;
vector<int> pos[3];
int get_next(int ind, int p)
{
auto it = lower_bound(pos[ind].begin(), pos[ind].end(), p);
if (it == pos[ind].end()) return -1;
int x = (int)(it-pos[ind].begin());
if (x+k-1 >= pos[ind].size()) return -1;
return pos[ind][x+k-1];
}
bool ok(int i)
{
if (i+k-1 >= pos[0].size()) return false;
int p = pos[0][i+k-1];
p = get_next(1, p+1);
if (p == -1) return false;
p = get_next(2, p+1);
if (p == -1) return false;
return true;
}
int main(void)
{
string s;
cin >> n >> k >> s;
int ans = inf;
for (int i = 0; i < n; i++)
{
if (s[i] == 'J') pos[0].push_back(i);
else if (s[i] == 'O') pos[1].push_back(i);
else pos[2].push_back(i);
if (s[i] != 'I' || pos[0].size() < k) continue;
int ini = 0, fim = pos[0].size()-1, opt = -1;
while (ini <= fim)
{
int mid = (ini+fim)>>1;
if (ok(mid)) opt = mid, ini = mid+1;
else fim = mid-1;
}
if (opt != -1)
ans = min(ans, i-pos[0][opt]+1-3*k);
}
if (ans != inf) cout << ans << "\n";
else cout << "-1\n";
}
Compilation message
ho_t2.cpp: In function 'int get_next(int, int)':
ho_t2.cpp:17:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
17 | if (x+k-1 >= pos[ind].size()) return -1;
| ~~~~~~^~~~~~~~~~~~~~~~~~
ho_t2.cpp: In function 'bool ok(int)':
ho_t2.cpp:24:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
24 | if (i+k-1 >= pos[0].size()) return false;
| ~~~~~~^~~~~~~~~~~~~~~~
ho_t2.cpp: In function 'int main()':
ho_t2.cpp:51:36: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
51 | if (s[i] != 'I' || pos[0].size() < k) continue;
| ~~~~~~~~~~~~~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
0 ms |
256 KB |
Output is correct |
8 |
Correct |
1 ms |
256 KB |
Output is correct |
9 |
Correct |
1 ms |
256 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
0 ms |
256 KB |
Output is correct |
12 |
Correct |
0 ms |
256 KB |
Output is correct |
13 |
Correct |
0 ms |
256 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
0 ms |
256 KB |
Output is correct |
8 |
Correct |
1 ms |
256 KB |
Output is correct |
9 |
Correct |
1 ms |
256 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
0 ms |
256 KB |
Output is correct |
12 |
Correct |
0 ms |
256 KB |
Output is correct |
13 |
Correct |
0 ms |
256 KB |
Output is correct |
14 |
Correct |
0 ms |
256 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 |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
2 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 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 |
0 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 |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
0 ms |
256 KB |
Output is correct |
8 |
Correct |
1 ms |
256 KB |
Output is correct |
9 |
Correct |
1 ms |
256 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
0 ms |
256 KB |
Output is correct |
12 |
Correct |
0 ms |
256 KB |
Output is correct |
13 |
Correct |
0 ms |
256 KB |
Output is correct |
14 |
Correct |
0 ms |
256 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 |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
2 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 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 |
0 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 |
111 ms |
1532 KB |
Output is correct |
37 |
Correct |
121 ms |
1860 KB |
Output is correct |
38 |
Correct |
123 ms |
2032 KB |
Output is correct |
39 |
Correct |
117 ms |
2048 KB |
Output is correct |
40 |
Correct |
93 ms |
2016 KB |
Output is correct |
41 |
Correct |
110 ms |
2032 KB |
Output is correct |
42 |
Correct |
128 ms |
1920 KB |
Output is correct |
43 |
Correct |
43 ms |
1280 KB |
Output is correct |
44 |
Correct |
54 ms |
1520 KB |
Output is correct |
45 |
Correct |
75 ms |
2048 KB |
Output is correct |
46 |
Correct |
73 ms |
1920 KB |
Output is correct |
47 |
Correct |
74 ms |
2048 KB |
Output is correct |
48 |
Correct |
79 ms |
2048 KB |
Output is correct |
49 |
Correct |
45 ms |
1284 KB |
Output is correct |
50 |
Correct |
73 ms |
1920 KB |
Output is correct |
51 |
Correct |
72 ms |
1920 KB |
Output is correct |
52 |
Correct |
18 ms |
1612 KB |
Output is correct |
53 |
Correct |
22 ms |
1868 KB |
Output is correct |
54 |
Correct |
66 ms |
2048 KB |
Output is correct |
55 |
Correct |
52 ms |
2048 KB |
Output is correct |
56 |
Correct |
18 ms |
2048 KB |
Output is correct |