#include <bits/stdc++.h>
using namespace std;
constexpr int MAXN = 200007;
int J[MAXN];
int O[MAXN];
int I[MAXN];
int main()
{
int n, k;
cin >> n >> k;
string s;
cin >> s;
vector<int> vJ;
vector<int> vO;
vector<int> vI;
for(int i = 0; i < n; ++i)
{
if(s[i] == 'J')
vJ.push_back(i);
if(s[i] == 'O')
vO.push_back(i);
if(s[i] == 'I')
vI.push_back(i);
}
for(int i = 0; i < (int)vJ.size() - k + 1; ++i)
J[vJ[i]] = vJ[i+k-1];
for(int i = 0; i < (int)vO.size() - k + 1; ++i)
O[vO[i]] = vO[i+k-1];
for(int i = 0; i < (int)vI.size() - k + 1; ++i)
I[vI[i]] = vI[i+k-1];
int naso[n];
int nasi[n];
int oo = n;
int ii = n;
for(int i = n-1; i >= 0; --i)
{
naso[i] = oo;
nasi[i] = ii;
if(s[i] == 'O')
oo = i;
if(s[i] == 'I')
ii = i;
}
int odp = MAXN;
for(int i = 0; i < n; ++i)
{
if(s[i] != 'J')
continue;
int a = J[i];
if(k != 1 && a == 0) continue;
int b = naso[a];
if(b == n) continue;
int c = O[b];
if(c == 0) continue;
int d = nasi[c];
if(d == n) continue;
int e = I[d];
if(e == 0) continue;
odp = min(odp, e-i+1);
}
if(odp == MAXN)
{
cout << "-1";
return 0;
}
cout << odp - k*3;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
2384 KB |
Output is correct |
3 |
Correct |
1 ms |
2384 KB |
Output is correct |
4 |
Correct |
1 ms |
2384 KB |
Output is correct |
5 |
Correct |
1 ms |
2384 KB |
Output is correct |
6 |
Correct |
1 ms |
2384 KB |
Output is correct |
7 |
Correct |
1 ms |
2384 KB |
Output is correct |
8 |
Correct |
1 ms |
2384 KB |
Output is correct |
9 |
Correct |
1 ms |
2384 KB |
Output is correct |
10 |
Correct |
1 ms |
2384 KB |
Output is correct |
11 |
Correct |
1 ms |
2384 KB |
Output is correct |
12 |
Correct |
1 ms |
2556 KB |
Output is correct |
13 |
Correct |
1 ms |
2384 KB |
Output is correct |
14 |
Correct |
1 ms |
2384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
2384 KB |
Output is correct |
3 |
Correct |
1 ms |
2384 KB |
Output is correct |
4 |
Correct |
1 ms |
2384 KB |
Output is correct |
5 |
Correct |
1 ms |
2384 KB |
Output is correct |
6 |
Correct |
1 ms |
2384 KB |
Output is correct |
7 |
Correct |
1 ms |
2384 KB |
Output is correct |
8 |
Correct |
1 ms |
2384 KB |
Output is correct |
9 |
Correct |
1 ms |
2384 KB |
Output is correct |
10 |
Correct |
1 ms |
2384 KB |
Output is correct |
11 |
Correct |
1 ms |
2384 KB |
Output is correct |
12 |
Correct |
1 ms |
2556 KB |
Output is correct |
13 |
Correct |
1 ms |
2384 KB |
Output is correct |
14 |
Correct |
1 ms |
2384 KB |
Output is correct |
15 |
Correct |
1 ms |
2384 KB |
Output is correct |
16 |
Correct |
1 ms |
2492 KB |
Output is correct |
17 |
Correct |
1 ms |
2384 KB |
Output is correct |
18 |
Correct |
1 ms |
2384 KB |
Output is correct |
19 |
Correct |
1 ms |
2384 KB |
Output is correct |
20 |
Correct |
1 ms |
2384 KB |
Output is correct |
21 |
Correct |
1 ms |
2384 KB |
Output is correct |
22 |
Correct |
1 ms |
2384 KB |
Output is correct |
23 |
Correct |
1 ms |
2384 KB |
Output is correct |
24 |
Correct |
1 ms |
2384 KB |
Output is correct |
25 |
Correct |
1 ms |
2640 KB |
Output is correct |
26 |
Correct |
1 ms |
2384 KB |
Output is correct |
27 |
Correct |
1 ms |
2384 KB |
Output is correct |
28 |
Correct |
1 ms |
2496 KB |
Output is correct |
29 |
Correct |
2 ms |
2384 KB |
Output is correct |
30 |
Correct |
2 ms |
2560 KB |
Output is correct |
31 |
Correct |
1 ms |
2384 KB |
Output is correct |
32 |
Correct |
2 ms |
2384 KB |
Output is correct |
33 |
Correct |
1 ms |
2384 KB |
Output is correct |
34 |
Correct |
1 ms |
2384 KB |
Output is correct |
35 |
Correct |
1 ms |
2384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
2384 KB |
Output is correct |
3 |
Correct |
1 ms |
2384 KB |
Output is correct |
4 |
Correct |
1 ms |
2384 KB |
Output is correct |
5 |
Correct |
1 ms |
2384 KB |
Output is correct |
6 |
Correct |
1 ms |
2384 KB |
Output is correct |
7 |
Correct |
1 ms |
2384 KB |
Output is correct |
8 |
Correct |
1 ms |
2384 KB |
Output is correct |
9 |
Correct |
1 ms |
2384 KB |
Output is correct |
10 |
Correct |
1 ms |
2384 KB |
Output is correct |
11 |
Correct |
1 ms |
2384 KB |
Output is correct |
12 |
Correct |
1 ms |
2556 KB |
Output is correct |
13 |
Correct |
1 ms |
2384 KB |
Output is correct |
14 |
Correct |
1 ms |
2384 KB |
Output is correct |
15 |
Correct |
1 ms |
2384 KB |
Output is correct |
16 |
Correct |
1 ms |
2492 KB |
Output is correct |
17 |
Correct |
1 ms |
2384 KB |
Output is correct |
18 |
Correct |
1 ms |
2384 KB |
Output is correct |
19 |
Correct |
1 ms |
2384 KB |
Output is correct |
20 |
Correct |
1 ms |
2384 KB |
Output is correct |
21 |
Correct |
1 ms |
2384 KB |
Output is correct |
22 |
Correct |
1 ms |
2384 KB |
Output is correct |
23 |
Correct |
1 ms |
2384 KB |
Output is correct |
24 |
Correct |
1 ms |
2384 KB |
Output is correct |
25 |
Correct |
1 ms |
2640 KB |
Output is correct |
26 |
Correct |
1 ms |
2384 KB |
Output is correct |
27 |
Correct |
1 ms |
2384 KB |
Output is correct |
28 |
Correct |
1 ms |
2496 KB |
Output is correct |
29 |
Correct |
2 ms |
2384 KB |
Output is correct |
30 |
Correct |
2 ms |
2560 KB |
Output is correct |
31 |
Correct |
1 ms |
2384 KB |
Output is correct |
32 |
Correct |
2 ms |
2384 KB |
Output is correct |
33 |
Correct |
1 ms |
2384 KB |
Output is correct |
34 |
Correct |
1 ms |
2384 KB |
Output is correct |
35 |
Correct |
1 ms |
2384 KB |
Output is correct |
36 |
Correct |
10 ms |
5336 KB |
Output is correct |
37 |
Correct |
11 ms |
5644 KB |
Output is correct |
38 |
Correct |
11 ms |
5716 KB |
Output is correct |
39 |
Correct |
10 ms |
5560 KB |
Output is correct |
40 |
Correct |
12 ms |
5592 KB |
Output is correct |
41 |
Correct |
15 ms |
5528 KB |
Output is correct |
42 |
Correct |
12 ms |
5908 KB |
Output is correct |
43 |
Correct |
8 ms |
4564 KB |
Output is correct |
44 |
Correct |
9 ms |
5032 KB |
Output is correct |
45 |
Correct |
15 ms |
5836 KB |
Output is correct |
46 |
Correct |
11 ms |
5568 KB |
Output is correct |
47 |
Correct |
11 ms |
5576 KB |
Output is correct |
48 |
Correct |
11 ms |
5444 KB |
Output is correct |
49 |
Correct |
8 ms |
4532 KB |
Output is correct |
50 |
Correct |
11 ms |
5340 KB |
Output is correct |
51 |
Correct |
12 ms |
5584 KB |
Output is correct |
52 |
Correct |
10 ms |
5204 KB |
Output is correct |
53 |
Correct |
10 ms |
5568 KB |
Output is correct |
54 |
Correct |
8 ms |
5216 KB |
Output is correct |
55 |
Correct |
10 ms |
5216 KB |
Output is correct |
56 |
Correct |
9 ms |
5228 KB |
Output is correct |