#include <bits/stdc++.h>
// #define int long long
using namespace std;
using pii = pair<int, int>;
void solve()
{
int n, k;
string s;
cin >> n >> k >> s;
vector<int> pj, po, pi;
for (int i = 0; i < n; ++i)
{
if (s[i] == 'J')
pj.emplace_back(i);
else if (s[i] == 'O')
po.emplace_back(i);
else
pi.emplace_back(i);
}
int ans = INT_MAX;
for (int i = n; i >= 0; --i)
{
if (i != n)
{
if (s[i] == 'J')
pj.pop_back();
else if (s[i] == 'O')
po.pop_back();
else
pi.pop_back();
}
if ((int)pi.size() < k)
continue;
int id = pi[(int)pi.size() - k];
id = lower_bound(po.begin(), po.end(), id) - po.begin();
if (id < k)
continue;
id -= k;
id = lower_bound(pj.begin(), pj.end(), po[id]) - pj.begin();
if (id < k)
continue;
id -= k;
ans = min(ans, i - pj[id]);
}
if (ans == INT_MAX)
{
cout << "-1\n";
return;
}
cout << ans - k * 3 << '\n';
}
/*
10 2
OJIJOIOIIJ
9 3
JJJOOOIII
9 1
IIIOOOJJJ
*/
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
424 KB |
Output is correct |
4 |
Correct |
0 ms |
344 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 |
344 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 |
460 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 |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
424 KB |
Output is correct |
4 |
Correct |
0 ms |
344 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 |
344 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 |
460 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 |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
600 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
424 KB |
Output is correct |
4 |
Correct |
0 ms |
344 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 |
344 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 |
460 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 |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
600 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
9 ms |
1752 KB |
Output is correct |
37 |
Correct |
10 ms |
1832 KB |
Output is correct |
38 |
Correct |
10 ms |
2092 KB |
Output is correct |
39 |
Correct |
14 ms |
1996 KB |
Output is correct |
40 |
Correct |
11 ms |
2016 KB |
Output is correct |
41 |
Correct |
13 ms |
1944 KB |
Output is correct |
42 |
Correct |
15 ms |
1832 KB |
Output is correct |
43 |
Correct |
6 ms |
1392 KB |
Output is correct |
44 |
Correct |
8 ms |
1624 KB |
Output is correct |
45 |
Correct |
10 ms |
2016 KB |
Output is correct |
46 |
Correct |
10 ms |
2008 KB |
Output is correct |
47 |
Correct |
10 ms |
2016 KB |
Output is correct |
48 |
Correct |
10 ms |
2016 KB |
Output is correct |
49 |
Correct |
7 ms |
1472 KB |
Output is correct |
50 |
Correct |
10 ms |
2124 KB |
Output is correct |
51 |
Correct |
10 ms |
2016 KB |
Output is correct |
52 |
Correct |
5 ms |
1740 KB |
Output is correct |
53 |
Correct |
5 ms |
1748 KB |
Output is correct |
54 |
Correct |
4 ms |
2020 KB |
Output is correct |
55 |
Correct |
3 ms |
2008 KB |
Output is correct |
56 |
Correct |
3 ms |
2304 KB |
Output is correct |