#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define F first
#define S second
using namespace std;
int N, K, J[200005], O[200005], I[200005];
int ans = 1e6;
char S[200005];
signed main()
{
cin >> N >> K;
for (int i = 1; i <= N; i++)
cin >> S[i];
deque<int> dq;
J[N + 1] = O[N + 1] = I[N + 1] = N + 1;
for (int i = N; i >= 1; i--)
{
if (S[i] == 'I')
dq.emplace_front(i);
if (dq.size() > K)
dq.pop_back();
if (dq.size() == K)
I[i] = dq.back();
else
I[i] = N + 1;
I[i] = min(I[i], I[i + 1]);
}
dq.clear();
for (int i = N; i >= 1; i--)
{
if (S[i] == 'O')
dq.emplace_front(i);
if (dq.size() > K)
dq.pop_back();
if (dq.size() == K)
O[i] = I[dq.back()];
else
O[i] = N + 1;
O[i] = min(O[i], O[i + 1]);
}
dq.clear();
for (int i = N; i >= 1; i--)
{
if (S[i] == 'J')
dq.emplace_front(i);
if (dq.size() > K)
dq.pop_back();
if (dq.size() == K)
J[i] = O[dq.back()];
else
J[i] = N + 1;
if (J[i] <= N)
ans = min(ans, J[i] - i + 1 - K * 3);
}
cout << (ans > N ? -1 : ans) << '\n';
}
Compilation message
ho_t2.cpp: In function 'int main()':
ho_t2.cpp:24:23: warning: comparison of integer expressions of different signedness: 'std::deque<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
24 | if (dq.size() > K)
| ~~~~~~~~~~^~~
ho_t2.cpp:26:23: warning: comparison of integer expressions of different signedness: 'std::deque<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
26 | if (dq.size() == K)
| ~~~~~~~~~~^~~~
ho_t2.cpp:37:23: warning: comparison of integer expressions of different signedness: 'std::deque<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
37 | if (dq.size() > K)
| ~~~~~~~~~~^~~
ho_t2.cpp:39:23: warning: comparison of integer expressions of different signedness: 'std::deque<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
39 | if (dq.size() == K)
| ~~~~~~~~~~^~~~
ho_t2.cpp:50:23: warning: comparison of integer expressions of different signedness: 'std::deque<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
50 | if (dq.size() > K)
| ~~~~~~~~~~^~~
ho_t2.cpp:52:23: warning: comparison of integer expressions of different signedness: 'std::deque<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
52 | if (dq.size() == K)
| ~~~~~~~~~~^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
308 KB |
Output is correct |
7 |
Correct |
1 ms |
308 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
308 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
304 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
308 KB |
Output is correct |
7 |
Correct |
1 ms |
308 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
308 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
304 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
308 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
328 KB |
Output is correct |
21 |
Correct |
1 ms |
312 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
308 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
312 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 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 |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
308 KB |
Output is correct |
7 |
Correct |
1 ms |
308 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
308 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
304 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
308 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
328 KB |
Output is correct |
21 |
Correct |
1 ms |
312 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
308 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
312 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 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 |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
17 ms |
2740 KB |
Output is correct |
37 |
Correct |
20 ms |
3016 KB |
Output is correct |
38 |
Correct |
21 ms |
3004 KB |
Output is correct |
39 |
Correct |
20 ms |
3000 KB |
Output is correct |
40 |
Correct |
20 ms |
2984 KB |
Output is correct |
41 |
Correct |
19 ms |
3028 KB |
Output is correct |
42 |
Correct |
19 ms |
2976 KB |
Output is correct |
43 |
Correct |
12 ms |
2004 KB |
Output is correct |
44 |
Correct |
14 ms |
2468 KB |
Output is correct |
45 |
Correct |
17 ms |
3028 KB |
Output is correct |
46 |
Correct |
19 ms |
3120 KB |
Output is correct |
47 |
Correct |
21 ms |
3096 KB |
Output is correct |
48 |
Correct |
19 ms |
3016 KB |
Output is correct |
49 |
Correct |
14 ms |
2124 KB |
Output is correct |
50 |
Correct |
18 ms |
3080 KB |
Output is correct |
51 |
Correct |
20 ms |
3028 KB |
Output is correct |
52 |
Correct |
18 ms |
3056 KB |
Output is correct |
53 |
Correct |
21 ms |
3284 KB |
Output is correct |
54 |
Correct |
16 ms |
3004 KB |
Output is correct |
55 |
Correct |
15 ms |
3108 KB |
Output is correct |
56 |
Correct |
16 ms |
3264 KB |
Output is correct |