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>
using namespace std;
#define INIT ios_base :: sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define mp make_pair
#define pb push_back
#define ft first
#define sc second
#define ll long long
#define pii pair<int, int>
#define count_bits __builtin_popcount
#define int ll
int t, n, m, k, a[300010], q, l, r;
int ord[300010][3], p[300010][3];
char s[200010];
int32_t main(){
INIT
cin>>n>>k;
for(int i=1; i<=(2*n); i++){
ord[i][0]=ord[i][1]=ord[i][2]=0;
p[i][0]=p[i][1]=p[i][2]=0;
}
for(int i=1; i<=n; i++){
p[i][0]=p[i-1][0];
p[i][1]=p[i-1][1];
p[i][2]=p[i-1][2];
cin>>s[i];
if(s[i]=='J'){
p[i][0]++;
ord[p[i][0] ][0]=i;
}
if(s[i]=='O'){
p[i][1]++;
ord[p[i][1] ][1]=i;
}
if(s[i]=='I'){
p[i][2]++;
ord[p[i][2] ][2]=i;
}
}
int res=-1;
for(int i=1; i<=n; i++){
int r0=ord[p[i-1][0]+k ][0];
int r1=ord[p[r0][1]+k ][1];
int r2=ord[p[r1][2]+k ][2];
//int r=ord[p[ord[p[ord[p[i-1][0]+k ][0] ][1]+k ][1] ][2]+k ][2];
//cout<<r2<<" "<<res<<"\n";
//cout<<r0<<" "<<r1<<" "<<r2<<"\n";
if( (r2!=0) && (r1!=0) && (r0!=0) ){
if(res==-1){
res=r2-i+1-3*k;
}
else{
res=min(res, r2-i+1-3*k);
}
}
}
cout<<res;
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... |