#include <bits/stdc++.h>
using namespace std;
int n,k;
int v[200005],dp[200005][4][22];
int move(int poz,int nr)
{
int rez=poz;
int need=k;
if(nr==1)
need-=1;
for(int bit=0;bit<=20;bit++)
if((need>>bit)&1)
rez=dp[rez][nr][bit];
return rez;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>n>>k;
for(int i=1;i<=n;i++)
{
char c;
cin>>c;
if(c=='J')
v[i]=1;
if(c=='O')
v[i]=2;
if(c=='I')
v[i]=3;
}
for(int i=n;i>=1;i--)
{
for(int nr=1;nr<=3;nr++)
{
if(v[i+1]==nr)
dp[i][nr][0]=i+1;
else
dp[i][nr][0]=dp[i+1][nr][0];
for(int j=1;j<=20;j++)
dp[i][nr][j]=dp[dp[i][nr][j-1]][nr][j-1];
}
}
int ans=1e9;
for(int i=1;i<=n;i++)
if(v[i]==1)
{
int poz=i;
poz=move(poz,1);
poz=move(poz,2);
poz=move(poz,3);
if(poz>0&&poz<=n)
ans=min(ans,poz-i+1-k*3);
}
if(ans==1e9)
cout<<-1;
else
cout<<ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
324 KB |
Output is correct |
5 |
Correct |
1 ms |
324 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
328 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
324 KB |
Output is correct |
5 |
Correct |
1 ms |
324 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
328 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
1108 KB |
Output is correct |
16 |
Correct |
2 ms |
1364 KB |
Output is correct |
17 |
Correct |
2 ms |
1364 KB |
Output is correct |
18 |
Correct |
2 ms |
980 KB |
Output is correct |
19 |
Correct |
2 ms |
1364 KB |
Output is correct |
20 |
Correct |
2 ms |
1364 KB |
Output is correct |
21 |
Correct |
2 ms |
1364 KB |
Output is correct |
22 |
Correct |
2 ms |
1236 KB |
Output is correct |
23 |
Correct |
1 ms |
852 KB |
Output is correct |
24 |
Correct |
2 ms |
1364 KB |
Output is correct |
25 |
Correct |
2 ms |
1364 KB |
Output is correct |
26 |
Correct |
2 ms |
1364 KB |
Output is correct |
27 |
Correct |
2 ms |
1364 KB |
Output is correct |
28 |
Correct |
2 ms |
1236 KB |
Output is correct |
29 |
Correct |
2 ms |
1364 KB |
Output is correct |
30 |
Correct |
3 ms |
1364 KB |
Output is correct |
31 |
Correct |
1 ms |
724 KB |
Output is correct |
32 |
Correct |
2 ms |
1364 KB |
Output is correct |
33 |
Correct |
2 ms |
1364 KB |
Output is correct |
34 |
Correct |
2 ms |
1348 KB |
Output is correct |
35 |
Correct |
2 ms |
1364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
324 KB |
Output is correct |
5 |
Correct |
1 ms |
324 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
328 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
1108 KB |
Output is correct |
16 |
Correct |
2 ms |
1364 KB |
Output is correct |
17 |
Correct |
2 ms |
1364 KB |
Output is correct |
18 |
Correct |
2 ms |
980 KB |
Output is correct |
19 |
Correct |
2 ms |
1364 KB |
Output is correct |
20 |
Correct |
2 ms |
1364 KB |
Output is correct |
21 |
Correct |
2 ms |
1364 KB |
Output is correct |
22 |
Correct |
2 ms |
1236 KB |
Output is correct |
23 |
Correct |
1 ms |
852 KB |
Output is correct |
24 |
Correct |
2 ms |
1364 KB |
Output is correct |
25 |
Correct |
2 ms |
1364 KB |
Output is correct |
26 |
Correct |
2 ms |
1364 KB |
Output is correct |
27 |
Correct |
2 ms |
1364 KB |
Output is correct |
28 |
Correct |
2 ms |
1236 KB |
Output is correct |
29 |
Correct |
2 ms |
1364 KB |
Output is correct |
30 |
Correct |
3 ms |
1364 KB |
Output is correct |
31 |
Correct |
1 ms |
724 KB |
Output is correct |
32 |
Correct |
2 ms |
1364 KB |
Output is correct |
33 |
Correct |
2 ms |
1364 KB |
Output is correct |
34 |
Correct |
2 ms |
1348 KB |
Output is correct |
35 |
Correct |
2 ms |
1364 KB |
Output is correct |
36 |
Correct |
137 ms |
65196 KB |
Output is correct |
37 |
Correct |
154 ms |
70092 KB |
Output is correct |
38 |
Correct |
154 ms |
70072 KB |
Output is correct |
39 |
Correct |
163 ms |
69436 KB |
Output is correct |
40 |
Correct |
173 ms |
70128 KB |
Output is correct |
41 |
Correct |
176 ms |
70172 KB |
Output is correct |
42 |
Correct |
168 ms |
70148 KB |
Output is correct |
43 |
Correct |
98 ms |
43620 KB |
Output is correct |
44 |
Correct |
124 ms |
54312 KB |
Output is correct |
45 |
Correct |
180 ms |
70072 KB |
Output is correct |
46 |
Correct |
175 ms |
70108 KB |
Output is correct |
47 |
Correct |
170 ms |
70036 KB |
Output is correct |
48 |
Correct |
177 ms |
70076 KB |
Output is correct |
49 |
Correct |
111 ms |
46884 KB |
Output is correct |
50 |
Correct |
168 ms |
70116 KB |
Output is correct |
51 |
Correct |
175 ms |
70092 KB |
Output is correct |
52 |
Correct |
145 ms |
67404 KB |
Output is correct |
53 |
Correct |
165 ms |
70092 KB |
Output is correct |
54 |
Correct |
114 ms |
70080 KB |
Output is correct |
55 |
Correct |
117 ms |
70148 KB |
Output is correct |
56 |
Correct |
118 ms |
70120 KB |
Output is correct |