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;
int pref[200005][3], last[200005][3];
int n, k; string s;
int main(){
cin >> n >> k >> s;
for(int i = 0; i < n; i ++){
for(int j = 0; j < 3; j ++)
pref[i + 1][j] = pref[i][j];
if(s[i] == 'J') pref[i + 1][0] ++;
if(s[i] == 'O') pref[i + 1][1] ++;
if(s[i] == 'I') pref[i + 1][2] ++;
}
int ans = 2e9, lf;
lf = 0;
for(int rg = 0; rg < n; rg ++){
for(;pref[rg + 1][0] - pref[lf][0] >= k; lf ++);
last[rg][0] = lf - 1;
}
lf = 0;
for(int rg = 0; rg < n; rg ++){
for(;pref[rg + 1][1] - pref[lf][1] >= k; lf ++);
last[rg][1] = lf - 1;
}
lf = 0;
for(int rg = 0; rg < n; rg ++){
for(;pref[rg + 1][2] - pref[lf][2] >= k; lf ++);
last[rg][2] = lf - 1;
}
for(int i = 0; i < n; i ++){
int nw = last[i][2] - 1;
if(nw >= 0) nw = last[nw][1] - 1;
else nw = -3;
if(nw >= 0) nw = last[nw][0] - 1;
else nw = -3;
if(nw != -3)
ans = min(ans, i - nw);
}
if(ans == 2e9) cout << -1 << endl;
else cout << ans - 3 * k << endl;
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... |