#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pll pair<ll, ll>
#define pii pair<int, int>
#define fs first
#define sc second
#define pb push_back
void solve() {
int n,k;
cin>>n>>k;
string s;
cin>>s;
int pre[n];
int idx=0,cnt=0;
for(int i=0;i<n;i++){
cnt+=s[i]=='J';
while(cnt-(s[idx]=='J')>=k){
cnt-=s[idx]=='J';
idx++;
}
if(cnt==k)pre[i]=i-idx+1-k;
else pre[i]=1e9;
}
int suf[n];
idx=n-1;
cnt=0;
for(int i=n-1;i>-1;i--){
cnt+=s[i]=='I';
while(cnt-(s[idx]=='I')>=k){
cnt-=s[idx]=='I';
idx--;
}
if(cnt==k)suf[i]=idx-i+1-k;
else suf[i]=1e9;
}
int ans=1e9;
idx=0;
cnt=0;
for(int i=0;i<n;i++){
cnt+=s[i]=='O';
while(cnt-(s[idx]=='O')>=k){
cnt-=s[idx]=='O';
idx++;
}
if(cnt==k){
int x=i-idx+1-k;
if(idx)x+=pre[idx-1];
if(i<n-1)x+=suf[i+1];
ans=min(ans,x);
}
}
if(ans==1e9)ans=-1;
cout<<ans;
}
int main() {
#ifdef FPO
freopen("in","r",stdin);
freopen("out","w",stdout);
#endif
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Incorrect |
1 ms |
508 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Incorrect |
1 ms |
508 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Incorrect |
1 ms |
508 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |