This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx2")
using namespace std;
#define ll long long
#define ld long double
#define pb push_back
#define pf push_front
#define pii pair<int,int>
#define all(v) v.begin(),v.end()
#define F first
#define S second
#define mem(a,i) memset(a,i,sizeof(a))
#define sz(s) (int)s.size()
#define int ll
#define y1 yy
#define ppb pop_back
#define gcd(a,b) __gcd(a,b)
#define in insert
const int dx[4]={-1,0,1,0};
const int dy[4]={0,-1,0,1};
const int inf=1e9;
const int N=1e6;
const int MAX=2e5+10;
const int mod=1e9+7;
int n,k;
string s;
int nxt[MAX][3];
void calc(int tp,char x){
int r=n;
int cnt=0;
for(int i=n;i>=1;i--){
cnt+=(s[i]==x);
while(cnt-(s[r]==x)>=k){
cnt-=(s[r]==x);
r--;
}
if(cnt<k)nxt[i][tp]=inf;
else nxt[i][tp]=r;
}
}
void solve(){
cin>>n>>k;
cin>>s;
s="#"+s;
calc(0,'J');
calc(1,'O');
calc(2,'I');
int ans=inf;
// cout<<nxt[4][1]<<" "<<nxt[7][2]<<"\n";
for(int i=1;i<=n;i++){
int r=nxt[i][0]+1;
if(r>n)continue;
r=nxt[r][1]+1;
if(r>n)continue;
r=nxt[r][2];
if(r>n)continue;
ans=min(ans,r-i+1-3*k);
}
if(ans!=inf)cout<<ans<<"\n";
else cout<<-1<<"\n";
}
main(){
// freopen("prizes.in", "r", stdin);
// freopen("prizes.out", "w", stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t=1;
// cin>>t;
while(t--){
solve();
}
}
Compilation message (stderr)
ho_t2.cpp:72:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
72 | main(){
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |