#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>
#include <deque>
typedef long long llong;
const int MAXN = 200000 + 10;
const llong INF = 1e18;
int n, k;
char s[MAXN];
std::deque <int> byLetter[3];
int search(int idx, int pos)
{
int l = -1, r = byLetter[idx].size(), mid;
while (l < r - 1)
{
mid = (l + r) / 2;
if (byLetter[idx][mid] < pos) l = mid;
else r = mid;
}
if (l + k <= (int)byLetter[idx].size()-1) return byLetter[idx][l + k];
return -1;
}
bool check(int deleted)
{
if (n - deleted < 3*k) return false;
byLetter[0].clear();
byLetter[1].clear();
byLetter[2].clear();
for (int i = n ; i >= 1 ; --i)
{
if (s[i] == 'J') byLetter[0].push_front(i);
if (s[i] == 'O') byLetter[1].push_front(i);
if (s[i] == 'I') byLetter[2].push_front(i);
if (i <= deleted && i != 0)
{
int endLetter = n - deleted + i;
if (s[endLetter] == 'J') byLetter[0].pop_back();
if (s[endLetter] == 'O') byLetter[1].pop_back();
if (s[endLetter] == 'I') byLetter[2].pop_back();
}
if (byLetter[0].size() >= k)
{
int pos = search(1, byLetter[0][k-1]);
if (pos != -1) pos = search(2, pos);
if (pos != -1) return true;
}
}
return false;
}
void solve()
{
int l = -1, r = n + 1, mid;
while (l < r - 1)
{
mid = (l + r) / 2;
if (check(mid)) l = mid;
else r = mid;
}
if (l == -1) std::cout << -1 << '\n';
else std::cout << n - 3*k - l << '\n';
}
void read()
{
std::cin >> n >> k;
std::cin >> s + 1;
}
void fastIO()
{
std::ios_base :: sync_with_stdio(0);
std::cout.tie(nullptr);
std::cin.tie(nullptr);
}
int main()
{
fastIO();
read();
solve();
return 0;
}
Compilation message
ho_t2.cpp: In function 'bool check(int)':
ho_t2.cpp:48:32: warning: comparison of integer expressions of different signedness: 'std::deque<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
48 | if (byLetter[0].size() >= k)
| ~~~~~~~~~~~~~~~~~~~^~~~
ho_t2.cpp: In function 'void read()':
ho_t2.cpp:76:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
76 | std::cin >> s + 1;
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
336 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
328 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
304 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
336 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
328 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
304 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
2 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
340 KB |
Output is correct |
20 |
Correct |
2 ms |
340 KB |
Output is correct |
21 |
Correct |
4 ms |
284 KB |
Output is correct |
22 |
Correct |
3 ms |
340 KB |
Output is correct |
23 |
Correct |
2 ms |
340 KB |
Output is correct |
24 |
Correct |
4 ms |
340 KB |
Output is correct |
25 |
Correct |
4 ms |
332 KB |
Output is correct |
26 |
Correct |
4 ms |
340 KB |
Output is correct |
27 |
Correct |
4 ms |
328 KB |
Output is correct |
28 |
Correct |
3 ms |
468 KB |
Output is correct |
29 |
Correct |
5 ms |
340 KB |
Output is correct |
30 |
Correct |
3 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
2 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
336 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
328 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
304 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
2 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
340 KB |
Output is correct |
20 |
Correct |
2 ms |
340 KB |
Output is correct |
21 |
Correct |
4 ms |
284 KB |
Output is correct |
22 |
Correct |
3 ms |
340 KB |
Output is correct |
23 |
Correct |
2 ms |
340 KB |
Output is correct |
24 |
Correct |
4 ms |
340 KB |
Output is correct |
25 |
Correct |
4 ms |
332 KB |
Output is correct |
26 |
Correct |
4 ms |
340 KB |
Output is correct |
27 |
Correct |
4 ms |
328 KB |
Output is correct |
28 |
Correct |
3 ms |
468 KB |
Output is correct |
29 |
Correct |
5 ms |
340 KB |
Output is correct |
30 |
Correct |
3 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
2 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
0 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
600 KB |
Output is correct |
37 |
Correct |
1 ms |
596 KB |
Output is correct |
38 |
Correct |
1 ms |
596 KB |
Output is correct |
39 |
Correct |
631 ms |
872 KB |
Output is correct |
40 |
Correct |
527 ms |
1392 KB |
Output is correct |
41 |
Correct |
527 ms |
1040 KB |
Output is correct |
42 |
Correct |
477 ms |
828 KB |
Output is correct |
43 |
Correct |
291 ms |
1096 KB |
Output is correct |
44 |
Correct |
403 ms |
1372 KB |
Output is correct |
45 |
Correct |
522 ms |
1544 KB |
Output is correct |
46 |
Correct |
552 ms |
1504 KB |
Output is correct |
47 |
Correct |
509 ms |
1644 KB |
Output is correct |
48 |
Correct |
545 ms |
1604 KB |
Output is correct |
49 |
Correct |
325 ms |
1144 KB |
Output is correct |
50 |
Correct |
501 ms |
1544 KB |
Output is correct |
51 |
Correct |
496 ms |
1652 KB |
Output is correct |
52 |
Correct |
90 ms |
1504 KB |
Output is correct |
53 |
Correct |
107 ms |
1492 KB |
Output is correct |
54 |
Correct |
72 ms |
1136 KB |
Output is correct |
55 |
Correct |
62 ms |
1444 KB |
Output is correct |
56 |
Correct |
2 ms |
1492 KB |
Output is correct |