#include <bits/stdc++.h>
using namespace std;
#define oo 1e15
#define fi first
#define se second
#define sp(iiii) setprecision(iiii)
#define IO ios_base::sync_with_stdio(false); cin.tie(0)
#define ms(aaaa,xxxx) memset(aaaa,xxxx,sizeof(aaaa))
#define cntbit(xxxx) __builtin_popcount(xxxx)
#define getbit(xxxx,aaaa) (((xxxx)>>(aaaa-1))&1)
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<pair<int,int>,int> piii;
typedef pair<pair<int,int>,pair<int,int>> piiii;
typedef pair<long long,long long> pll;
typedef pair<pair<long long,long long>,long long> plll;
typedef pair<pair<long long,long long>,pair<long long,long long>> pllll;
typedef pair<pair<long long,long long>,bool> pllb;
typedef pair<long double,long double> pld;
typedef pair<pair<long double,long double>,long double> plld;
const ll base=2e9+4257;
//const ll mod=1e9+7;
const ll mod2=1e9+4233;
const ld eps=1e-12;
const ll maxn=1e16;
const ld pi=acos(-1);
const ll delta=1e5+7;
const int dx[4]={1,-1,0,0};
const int dy[4]={0,0,1,-1};
int n,k,i,j,l,p,tp,res,nextp[200009][4][23];
string s;
int main(){
IO;
cin>>n>>k;
res=n;
cin>>s;
s=" "+s;
nextp[n][1][1]=n+1;
nextp[n][2][1]=n+1;
nextp[n][3][1]=n+1;
nextp[n+1][1][1]=n+1;
nextp[n+1][2][1]=n+1;
nextp[n+1][3][1]=n+1;
for (i=n;i>=1;i--) {
for (j=1;j<=3;j++) {
nextp[i-1][j][1]=nextp[i][j][1];
}
if (s[i]=='J') {
nextp[i-1][1][1]=i;
}
else if (s[i]=='O') {
nextp[i-1][2][1]=i;
}
else if (s[i]=='I') {
nextp[i-1][3][1]=i;
}
}
for (l=2;l<=20;l++) {
for (j=1;j<=3;j++) {
for (i=0;i<=n+1;i++) {
//cout<<i<<' '<<j<<' '<<l<<' '<<nextp[i][j][l-1]<<' '<<nextp[nextp[i][j][l-1]][j][l-1]<<'\n';
nextp[i][j][l]=nextp[nextp[i][j][l-1]][j][l-1];
}
}
}
//for (i=0;i<=n+1;i++) {
// for (j=1;j<=20;j++) {
// cout<<nextp[i][1][j]<<' ';
// }
// cout<<'\n';
//}
//cout<<nextp[1][1][2]<<'\n';
//cout<<nextp[8][1][2]<<'\n';
//cout<<nextp[1][1][3]<<'\n';
k--;
for (i=0;i<=n;i++) {
p=nextp[i][1][1];
tp=p;
//cout<<p<<' ';
for (j=20;j>=1;j--) {
if (getbit(k,j)==1) {
p=nextp[p][1][j];
}
}
//cout<<p<<' ';
p=nextp[p][2][1];
//cout<<p<<' ';
for (j=20;j>=1;j--) {
if (getbit(k,j)==1) {
p=nextp[p][2][j];
}
}
//cout<<p<<' ';
p=nextp[p][3][1];
//cout<<p<<' ';
for (j=20;j>=1;j--) {
if (getbit(k,j)==1) {
p=nextp[p][3][j];
}
}
//cout<<p<<' ';
//cout<<'\n';
if (p!=n+1) {
res=min(res,p-tp+1-(3*k+3));
}
//cout<<p<<' '<<tp<<'\n';
}
if (res==n) {
cout<<-1;
return 0;
}
cout<<res<<'\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
1228 KB |
Output is correct |
16 |
Correct |
2 ms |
1356 KB |
Output is correct |
17 |
Correct |
2 ms |
1356 KB |
Output is correct |
18 |
Correct |
1 ms |
972 KB |
Output is correct |
19 |
Correct |
2 ms |
1356 KB |
Output is correct |
20 |
Correct |
2 ms |
1356 KB |
Output is correct |
21 |
Correct |
2 ms |
1356 KB |
Output is correct |
22 |
Correct |
2 ms |
1228 KB |
Output is correct |
23 |
Correct |
1 ms |
844 KB |
Output is correct |
24 |
Correct |
2 ms |
1356 KB |
Output is correct |
25 |
Correct |
2 ms |
1356 KB |
Output is correct |
26 |
Correct |
2 ms |
1356 KB |
Output is correct |
27 |
Correct |
2 ms |
1356 KB |
Output is correct |
28 |
Correct |
2 ms |
1356 KB |
Output is correct |
29 |
Correct |
2 ms |
1356 KB |
Output is correct |
30 |
Correct |
2 ms |
1356 KB |
Output is correct |
31 |
Correct |
1 ms |
716 KB |
Output is correct |
32 |
Correct |
2 ms |
1356 KB |
Output is correct |
33 |
Correct |
2 ms |
1356 KB |
Output is correct |
34 |
Correct |
2 ms |
1356 KB |
Output is correct |
35 |
Correct |
2 ms |
1356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
1228 KB |
Output is correct |
16 |
Correct |
2 ms |
1356 KB |
Output is correct |
17 |
Correct |
2 ms |
1356 KB |
Output is correct |
18 |
Correct |
1 ms |
972 KB |
Output is correct |
19 |
Correct |
2 ms |
1356 KB |
Output is correct |
20 |
Correct |
2 ms |
1356 KB |
Output is correct |
21 |
Correct |
2 ms |
1356 KB |
Output is correct |
22 |
Correct |
2 ms |
1228 KB |
Output is correct |
23 |
Correct |
1 ms |
844 KB |
Output is correct |
24 |
Correct |
2 ms |
1356 KB |
Output is correct |
25 |
Correct |
2 ms |
1356 KB |
Output is correct |
26 |
Correct |
2 ms |
1356 KB |
Output is correct |
27 |
Correct |
2 ms |
1356 KB |
Output is correct |
28 |
Correct |
2 ms |
1356 KB |
Output is correct |
29 |
Correct |
2 ms |
1356 KB |
Output is correct |
30 |
Correct |
2 ms |
1356 KB |
Output is correct |
31 |
Correct |
1 ms |
716 KB |
Output is correct |
32 |
Correct |
2 ms |
1356 KB |
Output is correct |
33 |
Correct |
2 ms |
1356 KB |
Output is correct |
34 |
Correct |
2 ms |
1356 KB |
Output is correct |
35 |
Correct |
2 ms |
1356 KB |
Output is correct |
36 |
Correct |
212 ms |
67736 KB |
Output is correct |
37 |
Correct |
226 ms |
72716 KB |
Output is correct |
38 |
Correct |
227 ms |
72780 KB |
Output is correct |
39 |
Correct |
243 ms |
72076 KB |
Output is correct |
40 |
Correct |
259 ms |
72816 KB |
Output is correct |
41 |
Correct |
252 ms |
72804 KB |
Output is correct |
42 |
Correct |
243 ms |
72744 KB |
Output is correct |
43 |
Correct |
152 ms |
45520 KB |
Output is correct |
44 |
Correct |
187 ms |
56408 KB |
Output is correct |
45 |
Correct |
251 ms |
72716 KB |
Output is correct |
46 |
Correct |
256 ms |
72716 KB |
Output is correct |
47 |
Correct |
252 ms |
72720 KB |
Output is correct |
48 |
Correct |
257 ms |
72776 KB |
Output is correct |
49 |
Correct |
157 ms |
48468 KB |
Output is correct |
50 |
Correct |
250 ms |
72808 KB |
Output is correct |
51 |
Correct |
250 ms |
72716 KB |
Output is correct |
52 |
Correct |
214 ms |
70052 KB |
Output is correct |
53 |
Correct |
233 ms |
72712 KB |
Output is correct |
54 |
Correct |
214 ms |
72712 KB |
Output is correct |
55 |
Correct |
224 ms |
72732 KB |
Output is correct |
56 |
Correct |
222 ms |
72800 KB |
Output is correct |