#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
typedef long double ld;
const ll MOD=1e9+7;
const ll N=1e6+4;
const ld pi=3.14159265359;
const ll INF=(1LL<<60);
#define REP(i,n) for(ll i=0;i<n;i++)
#define REP1(i,n) for(ll i=1;i<=n;i++)
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define setp setprecision
#define lwb lower_bound
#define SZ(a) (ll)a.size()
string s;
ll k,u[N],nxk[N],n,ans=(1LL<<60),nxt[3][N],r;
queue<ll> q[3];
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
cin>>n>>k>>s;
REP(i,SZ(s))u[i]=(s[i]=='J'?0:(s[i]=='O'?1:2));
REP(i,n+2)nxk[i]=n+1,nxt[0][i]=nxt[1][i]=nxt[2][i]=n+1;
REP(i,SZ(s)){
q[u[i]].push(i);
if(SZ(q[u[i]])>=k){
nxk[q[u[i]].front()]=i+1;
q[u[i]].pop();
}
}
for(ll i=n-1;i>=0;i--){
REP(j,3)nxt[j][i]=nxt[j][i+1];
nxt[u[i]][i]=i;
}
REP(i,n){
if(u[i]==0){
r=nxk[i];
r=nxt[1][r];
r=nxk[r];
r=nxt[2][r];
r=nxk[r];
if(r==n+1)continue;
ans=min(ans,(r-i-3*k));
}
}
cout<<(ans==(1LL<<60)?-1:ans)<<"\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
4 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
5 ms |
376 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
4 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
5 ms |
376 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
5 ms |
504 KB |
Output is correct |
16 |
Correct |
5 ms |
504 KB |
Output is correct |
17 |
Correct |
5 ms |
504 KB |
Output is correct |
18 |
Correct |
5 ms |
504 KB |
Output is correct |
19 |
Correct |
5 ms |
504 KB |
Output is correct |
20 |
Correct |
5 ms |
504 KB |
Output is correct |
21 |
Correct |
5 ms |
504 KB |
Output is correct |
22 |
Correct |
5 ms |
504 KB |
Output is correct |
23 |
Correct |
5 ms |
376 KB |
Output is correct |
24 |
Correct |
5 ms |
504 KB |
Output is correct |
25 |
Correct |
5 ms |
504 KB |
Output is correct |
26 |
Correct |
5 ms |
508 KB |
Output is correct |
27 |
Correct |
5 ms |
504 KB |
Output is correct |
28 |
Correct |
5 ms |
504 KB |
Output is correct |
29 |
Correct |
5 ms |
504 KB |
Output is correct |
30 |
Correct |
5 ms |
504 KB |
Output is correct |
31 |
Correct |
5 ms |
376 KB |
Output is correct |
32 |
Correct |
5 ms |
504 KB |
Output is correct |
33 |
Correct |
5 ms |
504 KB |
Output is correct |
34 |
Correct |
5 ms |
504 KB |
Output is correct |
35 |
Correct |
5 ms |
504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
4 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
5 ms |
376 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
5 ms |
504 KB |
Output is correct |
16 |
Correct |
5 ms |
504 KB |
Output is correct |
17 |
Correct |
5 ms |
504 KB |
Output is correct |
18 |
Correct |
5 ms |
504 KB |
Output is correct |
19 |
Correct |
5 ms |
504 KB |
Output is correct |
20 |
Correct |
5 ms |
504 KB |
Output is correct |
21 |
Correct |
5 ms |
504 KB |
Output is correct |
22 |
Correct |
5 ms |
504 KB |
Output is correct |
23 |
Correct |
5 ms |
376 KB |
Output is correct |
24 |
Correct |
5 ms |
504 KB |
Output is correct |
25 |
Correct |
5 ms |
504 KB |
Output is correct |
26 |
Correct |
5 ms |
508 KB |
Output is correct |
27 |
Correct |
5 ms |
504 KB |
Output is correct |
28 |
Correct |
5 ms |
504 KB |
Output is correct |
29 |
Correct |
5 ms |
504 KB |
Output is correct |
30 |
Correct |
5 ms |
504 KB |
Output is correct |
31 |
Correct |
5 ms |
376 KB |
Output is correct |
32 |
Correct |
5 ms |
504 KB |
Output is correct |
33 |
Correct |
5 ms |
504 KB |
Output is correct |
34 |
Correct |
5 ms |
504 KB |
Output is correct |
35 |
Correct |
5 ms |
504 KB |
Output is correct |
36 |
Correct |
15 ms |
7940 KB |
Output is correct |
37 |
Correct |
16 ms |
8452 KB |
Output is correct |
38 |
Correct |
15 ms |
8448 KB |
Output is correct |
39 |
Correct |
15 ms |
8452 KB |
Output is correct |
40 |
Correct |
15 ms |
8836 KB |
Output is correct |
41 |
Correct |
15 ms |
8580 KB |
Output is correct |
42 |
Correct |
15 ms |
8580 KB |
Output is correct |
43 |
Correct |
12 ms |
5752 KB |
Output is correct |
44 |
Correct |
13 ms |
7044 KB |
Output is correct |
45 |
Correct |
16 ms |
8964 KB |
Output is correct |
46 |
Correct |
15 ms |
8968 KB |
Output is correct |
47 |
Correct |
15 ms |
8964 KB |
Output is correct |
48 |
Correct |
16 ms |
8964 KB |
Output is correct |
49 |
Correct |
12 ms |
6148 KB |
Output is correct |
50 |
Correct |
15 ms |
8964 KB |
Output is correct |
51 |
Correct |
15 ms |
8964 KB |
Output is correct |
52 |
Correct |
15 ms |
9476 KB |
Output is correct |
53 |
Correct |
16 ms |
9860 KB |
Output is correct |
54 |
Correct |
14 ms |
8452 KB |
Output is correct |
55 |
Correct |
13 ms |
9476 KB |
Output is correct |
56 |
Correct |
14 ms |
10244 KB |
Output is correct |