#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) a.begin(),a.end()
#define rep(i,n) for(int i=0;i<n;i++)
#define crep(i,x,n) for(int i=x;i<n;i++)
#define drep(i,n) for(int i=n-1;i>=0;i--)
#define vec(...) vector<__VA_ARGS__>
#define _34raRxL ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
void print(){cout<<"\n";}
template<class te,class ...ti>
void print(const te&v,const ti&...nv){cout<<v;if(sizeof...(nv)){cout<<" ";print(nv...);}}
//e
typedef long long ll;
typedef long double ld;
using pii=pair<int,int>;
using vi=vector<int>;
const int inf=1e9;
int main(){
_34raRxL;
int n,k;
cin>>n>>k;
string s;
cin>>s;
vi a;
{
rep(i,n){
int x=2;
if(s[i]=='J') x=0;
else if(s[i]=='O') x=1;
a.pb(x);
}
}
vi f_l(n,inf),g(n);
{
deque<int> dq;
bool pok=0;
rep(i,n){
int x=a[i];
if(x!=0){
if(not pok){
continue;
}
}else{
pok=1;
}
if(i){
g[i]=g[i-1];
}
if(x==0){
dq.pb(i);
}else{
g[i]++;
}
if(sz(dq)>k){
int j=dq.front()+1;
dq.pop_front();
while(a[j]!=0){
g[i]--;
j=j+1;
}
}
if(sz(dq)==k){
f_l[i]=g[i];
}
}
} // rabbits from left
vi f_r(n,inf);
g=vi(n,0);
{
deque<int> dq;
bool pok=0;
drep(i,n){
int x=a[i];
if(x!=2){
if(not pok){
continue;
}
}else{
pok=1;
}
if(i!=n-1){
g[i]=g[i+1];
}
if(x==2){
dq.push_front(i);
}else{
g[i]++;
}
if(sz(dq)>k){
int j=dq.back()-1;
dq.pop_back();
while(a[j]!=2){
g[i]--;
j=j-1;
}
}
if(sz(dq)==k){
f_r[i]=g[i];
}
}
} // rabbits from right
vec(vi) psum(n,vi(3));
rep(i,n){
if(i){
psum[i]=psum[i-1];
}
psum[i][a[i]]++;
}
int ans=inf;
{
deque<int> dq;
int mi=inf,nemi=inf;
int l=0;
rep(i,n-1){
int x=a[i];
if(x==1){
dq.pb(i);
}
if(sz(dq)>=k){
if(sz(dq)>k){
dq.pop_front();
}
nemi=inf;
while(l<dq.front()){
nemi=min(nemi,f_l[l]-psum[l][0]-psum[l][2]);
l=l+1;
}
mi=min(mi+1,nemi);
ans=min(ans,mi+f_r[i+1]+psum[i][0]+psum[i][2]);
}
}
}
print((ans==inf?-1:ans),"\n");
//
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
316 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
288 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
308 KB |
Output is correct |
6 |
Correct |
1 ms |
288 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
308 KB |
Output is correct |
11 |
Correct |
0 ms |
312 KB |
Output is correct |
12 |
Correct |
0 ms |
308 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
316 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
288 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
308 KB |
Output is correct |
6 |
Correct |
1 ms |
288 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
308 KB |
Output is correct |
11 |
Correct |
0 ms |
312 KB |
Output is correct |
12 |
Correct |
0 ms |
308 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
440 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
1 ms |
460 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
444 KB |
Output is correct |
20 |
Correct |
1 ms |
460 KB |
Output is correct |
21 |
Correct |
1 ms |
460 KB |
Output is correct |
22 |
Correct |
1 ms |
460 KB |
Output is correct |
23 |
Correct |
1 ms |
308 KB |
Output is correct |
24 |
Correct |
2 ms |
460 KB |
Output is correct |
25 |
Correct |
2 ms |
460 KB |
Output is correct |
26 |
Correct |
1 ms |
460 KB |
Output is correct |
27 |
Correct |
1 ms |
460 KB |
Output is correct |
28 |
Correct |
1 ms |
436 KB |
Output is correct |
29 |
Correct |
1 ms |
460 KB |
Output is correct |
30 |
Correct |
1 ms |
460 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
460 KB |
Output is correct |
33 |
Correct |
1 ms |
436 KB |
Output is correct |
34 |
Correct |
1 ms |
460 KB |
Output is correct |
35 |
Correct |
1 ms |
460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
316 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
288 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
308 KB |
Output is correct |
6 |
Correct |
1 ms |
288 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
308 KB |
Output is correct |
11 |
Correct |
0 ms |
312 KB |
Output is correct |
12 |
Correct |
0 ms |
308 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
440 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
1 ms |
460 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
444 KB |
Output is correct |
20 |
Correct |
1 ms |
460 KB |
Output is correct |
21 |
Correct |
1 ms |
460 KB |
Output is correct |
22 |
Correct |
1 ms |
460 KB |
Output is correct |
23 |
Correct |
1 ms |
308 KB |
Output is correct |
24 |
Correct |
2 ms |
460 KB |
Output is correct |
25 |
Correct |
2 ms |
460 KB |
Output is correct |
26 |
Correct |
1 ms |
460 KB |
Output is correct |
27 |
Correct |
1 ms |
460 KB |
Output is correct |
28 |
Correct |
1 ms |
436 KB |
Output is correct |
29 |
Correct |
1 ms |
460 KB |
Output is correct |
30 |
Correct |
1 ms |
460 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
460 KB |
Output is correct |
33 |
Correct |
1 ms |
436 KB |
Output is correct |
34 |
Correct |
1 ms |
460 KB |
Output is correct |
35 |
Correct |
1 ms |
460 KB |
Output is correct |
36 |
Correct |
23 ms |
13792 KB |
Output is correct |
37 |
Correct |
30 ms |
14800 KB |
Output is correct |
38 |
Correct |
29 ms |
14692 KB |
Output is correct |
39 |
Correct |
30 ms |
14672 KB |
Output is correct |
40 |
Correct |
35 ms |
14868 KB |
Output is correct |
41 |
Correct |
28 ms |
14824 KB |
Output is correct |
42 |
Correct |
26 ms |
14776 KB |
Output is correct |
43 |
Correct |
17 ms |
9376 KB |
Output is correct |
44 |
Correct |
22 ms |
11588 KB |
Output is correct |
45 |
Correct |
29 ms |
14864 KB |
Output is correct |
46 |
Correct |
27 ms |
14816 KB |
Output is correct |
47 |
Correct |
24 ms |
14900 KB |
Output is correct |
48 |
Correct |
28 ms |
14784 KB |
Output is correct |
49 |
Correct |
19 ms |
9956 KB |
Output is correct |
50 |
Correct |
24 ms |
14904 KB |
Output is correct |
51 |
Correct |
25 ms |
14888 KB |
Output is correct |
52 |
Correct |
22 ms |
14420 KB |
Output is correct |
53 |
Correct |
29 ms |
15036 KB |
Output is correct |
54 |
Correct |
21 ms |
14760 KB |
Output is correct |
55 |
Correct |
20 ms |
14880 KB |
Output is correct |
56 |
Correct |
22 ms |
15032 KB |
Output is correct |