#include<bits/stdc++.h>
//#pragma GCC optimize("Ofast,unroll-loops")
//#pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2,fma,tune=native")
#define pii pair<int, int>
#define fi first
#define se second
#define pb push_back
#define int long long
//#define ld long double
using namespace std;
using ll=long long;
const int inf = 1e18;
const int MN =200005;
const int mod=1e9+7;
int n,k;
string s;
int posj[MN], poso[MN], posi[MN];
int lj[MN], li[MN], lo[MN],valj[MN], vali[MN], valo[MN];
void solve() {
cin>>n>>k>>s;
int cnti=0,cntj=0,cnto=0;
for (int i=0; i<n; i++) {
if(i>0) {
valo[i]=valo[i-1];
valj[i]=valj[i-1];
vali[i]=vali[i-1];
}
if(s[i]=='O') cnto++,poso[cnto]=i, valo[i]=cnto;
if(s[i]=='J') cntj++,posj[cntj]=i, valj[i]=cntj;
if(s[i]=='I') cnti++,posi[cnti]=i, vali[i]=cnti;
}
for (int i=0; i<n; i++) {
if(valj[i]<k) lj[i]=-1;
else lj[i]=posj[valj[i]-k+1];
if(valo[i]<k) lo[i]=-1;
else lo[i]=poso[valo[i]-k+1];
if(vali[i]<k) li[i]=-1;
else li[i]=posi[vali[i]-k+1];
}
// for (int i=0; i<n; i++) {
// cout<<lj[i]<<" "<<lo[i]<<" "<<li[i]<<"\n";
// }
int ans=inf;
for (int r=n-1; r>=0; r--) {
if(s[r]!='I') continue;
int l;
l=li[r];
if(l==-1) continue;
l=lo[l];
if(l==-1) continue;
l=lj[l];
if(l==-1) continue;
// cout<<l<<" "<<r<<"\n";
ans=min(ans,r-l+1-3*k);
}
if(ans==inf) cout<<-1;
else cout<<ans;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
// freopen(".inp", "r", stdin);
// freopen(".out", "w", stdout);
int t=1;
// cin>>t;
while(t--) {
solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
452 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
0 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
0 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
456 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
452 KB |
Output is correct |
25 |
Correct |
1 ms |
452 KB |
Output is correct |
26 |
Correct |
0 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
0 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
0 ms |
468 KB |
Output is correct |
35 |
Correct |
0 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
452 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
0 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
0 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
456 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
452 KB |
Output is correct |
25 |
Correct |
1 ms |
452 KB |
Output is correct |
26 |
Correct |
0 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
0 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
0 ms |
468 KB |
Output is correct |
35 |
Correct |
0 ms |
468 KB |
Output is correct |
36 |
Correct |
6 ms |
10976 KB |
Output is correct |
37 |
Correct |
7 ms |
11744 KB |
Output is correct |
38 |
Correct |
9 ms |
11744 KB |
Output is correct |
39 |
Correct |
7 ms |
11728 KB |
Output is correct |
40 |
Correct |
7 ms |
11732 KB |
Output is correct |
41 |
Correct |
7 ms |
11744 KB |
Output is correct |
42 |
Correct |
7 ms |
11776 KB |
Output is correct |
43 |
Correct |
5 ms |
7504 KB |
Output is correct |
44 |
Correct |
6 ms |
9180 KB |
Output is correct |
45 |
Correct |
9 ms |
11764 KB |
Output is correct |
46 |
Correct |
7 ms |
11744 KB |
Output is correct |
47 |
Correct |
7 ms |
11744 KB |
Output is correct |
48 |
Correct |
8 ms |
11792 KB |
Output is correct |
49 |
Correct |
5 ms |
8032 KB |
Output is correct |
50 |
Correct |
7 ms |
11728 KB |
Output is correct |
51 |
Correct |
7 ms |
11744 KB |
Output is correct |
52 |
Correct |
7 ms |
11360 KB |
Output is correct |
53 |
Correct |
7 ms |
11776 KB |
Output is correct |
54 |
Correct |
6 ms |
11744 KB |
Output is correct |
55 |
Correct |
5 ms |
11744 KB |
Output is correct |
56 |
Correct |
6 ms |
11748 KB |
Output is correct |