// #include <bits/stdc++.h>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <map>
#include <unordered_map>
#include <vector>
#include <iomanip>
#include <string>
#include <queue>
#include <set>
#include <deque>
using namespace std;
#define int long long
#define endl "\n"
#define fi first
#define se second
const int M=1203465797;
const int inf = 1e14;
const int LOG=18;
const int N=50+5;
int n , m , c , w , k , t=1 , q=1 , x , y , z , l , r;
int cn[3];
void solve(){
cin >> n >> k;
string s;
cin >> s;
int j=0;
string o="JOI";
for (int i=0;i<n;i++){
if (s[i]==o[j]){
cn[j]++;
if (cn[j]==k){
j++;
}
}
if (j==3)break;
}
if (j<3){
cout << -1 << endl;
return;
}
vector<vector<int>>cnt(n+1,vector<int>(3));
s='I'+s;
for (int i=1;i<=n;i++){
for (int j=0;j<3;j++){
if (s[i]==o[j])cnt[i][j]++;
cnt[i][j]+=cnt[i-1][j];
}
}
int i=0;
j=1;
int ans=n;
while (j<=n){
if (cnt[j][1]-cnt[i][1]<k){
j++;
}
else{
int l=0;
int h=i-1;
int m=(l+h+1)/2;
while (l<h){
if (cnt[i][0]-cnt[m][0]<k){
h=m-1;
}
else{
l=m;
}
m=(l+h+1)/2;
}
int li=j+1;
int hi=n;
int mi=(li+hi)/2;
while (li<hi){
if (cnt[mi][2]-cnt[j][2]<k){
li=mi+1;
}
else{
hi=mi;
}
mi=(li+hi)/2;
}
if (cnt[i][0]-cnt[m][0]>=k and cnt[mi][2]-cnt[j][2]>=k){
ans=min(ans,mi-m-3*k);
}
i++;
}
}
cout << ans << endl;
}
signed main()
{
ios::sync_with_stdio(0);//DO NOT USE IN INTERACTIVE
cin.tie(0), cout.tie(0);//DO NOT USE IN INTERACTIVE
cout << fixed << setprecision(9);
srand(time(0));
// int t=1;
// cin >> t;
for (int _=1;_<=t;_++){
solve();
q++;
}
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |