#include<bits/stdc++.h>
#define int long long
#define pb push_back
#define ALL(x) x.begin(),x.end()
#define fi first
#define se second
#define ist insert
using namespace std;
int min(int a,int b){return a<b?a:b;}
int max(int a,int b){return a>b?a:b;}
typedef long long ll;
typedef pair<ll,ll> pii;
const int N=200005;
const int MOD=1000000007;//998244353
const int INF=2147483647;//1700000000000000000
int n,k;
string s;
vector<int> site[3];
int a[N];
vector<pii> dp[3];
bool vaild(){
int cnt=0,typ=0;
for (int i=0;i<n;i++){
if (typ==0&&s[i]=='J') cnt++;
if (typ==1&&s[i]=='O') cnt++;
if (typ==2&&s[i]=='I') cnt++;
if (cnt==k) cnt=0,typ++;
}
return typ==3;
}
int srch1(int x){
int l=0,r=dp[0].size(),res=-1;
while (r>=l){
int mid=(l+r)/2;
if (dp[0][mid].fi<x){
res=mid;
l=mid+1;
}
else r=mid-1;
}
return res;
}
int srch2(int x){
int l=0,r=dp[2].size(),res=-1;
while (r>=l){
int mid=(l+r)/2;
if (dp[2][mid].fi>x){
res=mid;
r=mid-1;
}
else l=mid+1;
}
return res;
}
inline void sol(){
cin >>n>>k>>s;
if (!vaild()){
cout <<"-1\n";
return;
}
for (int i=0;i<n;i++){
if (s[i]=='J') a[i]=0;
if (s[i]=='O') a[i]=1;
if (s[i]=='I') a[i]=2;
site[a[i]].pb(i);
}
for (int i=k-1;i<site[0].size();i++){
dp[0].pb({site[0][i],site[0][i]-site[0][i-(k-1)]-(k-1)});
}
for (int i=0;i+k-1<site[2].size();i++){
dp[2].pb({site[2][i],site[2][i+k-1]-site[2][i]-(k-1)});
}
int ans=INF;
for (int i=0;i+k-1<site[1].size();i++){
int x=srch1(site[1][i]),y=srch2(site[1][i+k-1]);
if (x==-1||y==-1) continue;
ans=min(ans,dp[0][x].se+dp[2][y].se+site[1][i]-dp[0][x].fi-1+dp[2][y].fi-site[1][i+k-1]-1+site[1][i+k-1]-site[1][i]-(k-1));
}
/*for (auto [x,y]:dp[0]) cout <<x<<' '<<y<<'\n';
cout <<'\n';
for (auto [x,y]:dp[2]) cout <<x<<' '<<y<<'\n';
cout <<'\n';*/
cout <<ans<<'\n';
}
signed main(){
int _=1;
//cin >>_;
while (_--) sol();
return 0;
}
Compilation message
ho_t2.cpp: In function 'void sol()':
ho_t2.cpp:69:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
69 | for (int i=k-1;i<site[0].size();i++){
| ~^~~~~~~~~~~~~~~
ho_t2.cpp:72:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
72 | for (int i=0;i+k-1<site[2].size();i++){
| ~~~~~^~~~~~~~~~~~~~~
ho_t2.cpp:76:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
76 | for (int i=0;i+k-1<site[1].size();i++){
| ~~~~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |