#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 (i=0;i<=n+1;i++) {
for (j=1;j<=3;j++) {
for (l=2;l<=20;l++) {
nextp[i][j][l]=nextp[nextp[i][j][l-1]][j][l-1];
}
}
}
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));
}
}
if (res==n) {
cout<<-1;
return 0;
}
cout<<res<<'\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
324 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
324 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
324 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |