#include <bits/stdc++.h>
using namespace std;
int inf = 1e9;
int n,k;
char sir[200005];
int a[200005];
int next0[200005],next1[200005],next2[200005];
vector<int>pos0,pos1,pos2;
int p[200005];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> k;
for (int i = 1; i <= n; i++)
cin >> sir[i];
for (int i = 1; i <= n; i++)
{
if (sir[i] == 'J')
a[i] = 0;
else if (sir[i] == 'O')
a[i] = 1;
else
a[i] = 2;
}
next0[n] = next1[n] = next2[n] = n + 1;
for (int i = n - 1; i >= 1; i--)
{
next0[i] = next0[i + 1];
next1[i] = next1[i + 1];
next2[i] = next2[i + 1];
if (a[i + 1] == 0)
next0[i] = i + 1;
if (a[i + 1] == 1)
next1[i] = i + 1;
if (a[i + 1] == 2)
next2[i] = i + 1;
}
next0[n + 1] = next1[n + 1] = next2[n + 1] = n + 1;
for (int i = 1; i <= n; i++)
{
if (a[i] == 0)
pos0.push_back(i),p[i] = pos0.size() - 1;
if (a[i] == 1)
pos1.push_back(i),p[i] = pos1.size() - 1;
if (a[i] == 2)
pos2.push_back(i),p[i] = pos2.size() - 1;
}
int ans = 1e9;
p[n + 1] = n + 1;
for (int i = 1; i <= n; i++)
{
if (a[i] == 0)
{
if (p[i] + k - 1 < pos0.size())
{
int x = pos0[p[i] + k - 1];
int x0 = next1[x];
if (p[x0] + k - 1 < pos1.size())
{
int x1 = pos1[p[x0] + k - 1];
int x2 = next2[x1];
if (p[x2] + k - 1 < pos2.size())
{
int x3 = pos2[p[x2] + k - 1];
ans = min(ans,x3 - i + 1 - 3 * k);
}
}
}
}
}
if (ans == inf)
cout << -1;
else
cout << ans;
return 0;
}
Compilation message
ho_t2.cpp: In function 'int main()':
ho_t2.cpp:60:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
60 | if (p[i] + k - 1 < pos0.size())
| ~~~~~~~~~~~~~^~~~~~~~~~~~~
ho_t2.cpp:64:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
64 | if (p[x0] + k - 1 < pos1.size())
| ~~~~~~~~~~~~~~^~~~~~~~~~~~~
ho_t2.cpp:68:39: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
68 | if (p[x2] + k - 1 < pos2.size())
| ~~~~~~~~~~~~~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2520 KB |
Output is correct |
8 |
Correct |
1 ms |
2392 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2520 KB |
Output is correct |
8 |
Correct |
1 ms |
2392 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2524 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
2392 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
1 ms |
2396 KB |
Output is correct |
28 |
Correct |
1 ms |
2396 KB |
Output is correct |
29 |
Correct |
1 ms |
2396 KB |
Output is correct |
30 |
Correct |
1 ms |
2396 KB |
Output is correct |
31 |
Correct |
1 ms |
2488 KB |
Output is correct |
32 |
Correct |
1 ms |
2396 KB |
Output is correct |
33 |
Correct |
1 ms |
2396 KB |
Output is correct |
34 |
Correct |
1 ms |
2396 KB |
Output is correct |
35 |
Correct |
1 ms |
2648 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2520 KB |
Output is correct |
8 |
Correct |
1 ms |
2392 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2524 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
2392 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
1 ms |
2396 KB |
Output is correct |
28 |
Correct |
1 ms |
2396 KB |
Output is correct |
29 |
Correct |
1 ms |
2396 KB |
Output is correct |
30 |
Correct |
1 ms |
2396 KB |
Output is correct |
31 |
Correct |
1 ms |
2488 KB |
Output is correct |
32 |
Correct |
1 ms |
2396 KB |
Output is correct |
33 |
Correct |
1 ms |
2396 KB |
Output is correct |
34 |
Correct |
1 ms |
2396 KB |
Output is correct |
35 |
Correct |
1 ms |
2648 KB |
Output is correct |
36 |
Correct |
8 ms |
5260 KB |
Output is correct |
37 |
Correct |
9 ms |
5784 KB |
Output is correct |
38 |
Correct |
9 ms |
5836 KB |
Output is correct |
39 |
Correct |
8 ms |
5844 KB |
Output is correct |
40 |
Correct |
8 ms |
5852 KB |
Output is correct |
41 |
Correct |
8 ms |
5816 KB |
Output is correct |
42 |
Correct |
9 ms |
5852 KB |
Output is correct |
43 |
Correct |
6 ms |
4700 KB |
Output is correct |
44 |
Correct |
7 ms |
4988 KB |
Output is correct |
45 |
Correct |
9 ms |
5976 KB |
Output is correct |
46 |
Correct |
8 ms |
5804 KB |
Output is correct |
47 |
Correct |
9 ms |
5752 KB |
Output is correct |
48 |
Correct |
9 ms |
5976 KB |
Output is correct |
49 |
Correct |
6 ms |
4708 KB |
Output is correct |
50 |
Correct |
10 ms |
5852 KB |
Output is correct |
51 |
Correct |
9 ms |
5852 KB |
Output is correct |
52 |
Correct |
8 ms |
5464 KB |
Output is correct |
53 |
Correct |
8 ms |
5836 KB |
Output is correct |
54 |
Correct |
6 ms |
5984 KB |
Output is correct |
55 |
Correct |
5 ms |
5984 KB |
Output is correct |
56 |
Correct |
5 ms |
5984 KB |
Output is correct |