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>
#define forn(i, n) for(int i = 0; i < int(n); i++)
#define ll long long
const int lmt = 200005;
using namespace std;
int prefix[3][lmt];
int op(int i, int j, int type){
return (i<=j ? prefix[type][j] - (i>0 ? prefix[type][i-1] : 0) : 0);
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int n, k; string s; cin>>n>>k>>s;
int a[3]={0,0,0};
forn(i, n){
char x = s[i];
if(x=='J'){
prefix[0][i]++;
}else if(x=='O'){
prefix[1][i]++;
}else{
prefix[2][i]++;
}
if(i>0) {
forn(j, 3){
prefix[j][i] += prefix[j][i-1];
}
}
}
int fin[3]={0,0,0};
int d = n;
for(int i = 0; i < n; i++){
int last = i;
forn(t, 3){
while(fin[t]<n && op(last,fin[t], t)!=k){
fin[t]++;
}
if(op(last, fin[t], t)!=k){
cout<<(i>0 ? d : -1);
return 0;
}
last = fin[t]+1;
}
d = min(d, (((fin[0] - i)+1) - k) + (((fin[1] - (fin[0]+1))+1) - k) + (((fin[2] - (fin[1]+1))+1) - k));
}
cout<<d;
return 0;
}
Compilation message (stderr)
ho_t2.cpp: In function 'int main()':
ho_t2.cpp:14:6: warning: unused variable 'a' [-Wunused-variable]
14 | int a[3]={0,0,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... |