//              +-- -- --++-- +-In the name of ALLAH-+ --++-- -- --+              \\
/* Some Makoto Shinkai's : 
  “Who cares if we can't see any sunshine? I want you more than any blue sky!!!”
    - Tenki no Ko
    
  "By the time the date is over, the comet will be visible in the sky."
    - Kimi no Nawa
    
  “No matter what happens, even if the stars fall, I will live.”
    - Byōsoku 5 Centimeter
  
*/
#include <bits/stdc++.h>
#define Raveluk ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ll long long
#define ld long double
#define pb push_back
#define fi first
#define se second
#define pii pair<int,int>
#define tii tuple<int,int,int>
#define g1 get<0>
#define g2 get<1>
#define g3 get<2>
#define qf q.front()
#define all(x) (x).begin(), (x).end()
using namespace std;
int main()
{
  Raveluk
  int n,k,i,j,L,R,M,id,ans=1e9,a1;
  string s;
  cin>>n>>k>>s;
  vector<int>v[3];
  for(i=0;i<n;i++){
    if(s[i] == 'J') v[0].pb(i);
    if(s[i] == 'O') v[1].pb(i);
    if(s[i] == 'I') v[2].pb(i);
  }
  int pj = v[0].size(), po = v[1].size(), pi = v[2].size();
  if(pj < k or po < k or pi < k){
    cout<<-1;
    return 0;
  }
  for(i=0;i<=pj-k;i++){
    // v[i] = J pertama
    // J terakhir di i+k-1
    id = v[0][i+k-1], L = 0, R = po-1,a1=id;
    // cari O pertama > id
    bool ada = false;
    while(L <= R){
      M = (L+R)/2;
      if(v[1][M] < id) L = M+1;
      else{
        ada = true;
        if(M == 0) break;
        if(v[1][M-1] >= id) R = M-1;
        else break;
      }
    }
    if(!ada or M+k-1 >= po) break;
    id = v[1][M+k-1];
    L = 0, R = pi-1, ada = false;
    while(L <= R){
      M = (L+R)/2;
      if(v[2][M] < id) L = M+1;
      else{
        ada = true;
        if(M == 0) break;
        if(v[2][M-1] >= id) R = M-1;
        else break;
      }
    }
    if(!ada or M+k-1 >= pi) break;
    //cout<<v[2][M]<<" "<<a1<<" "<<id<<endl;
    ans = min(ans,v[2][M]-a1-1-k+v[2][M+k-1]-v[2][M]-1-(k-2)+v[0][i+k-1]-v[0][i]-1-(k-2));
  }
  if(ans == 1e9) cout<<-1;
  else cout<<ans;
  
  return 0;
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |