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;
typedef long long ll;
const int N = 2e5 + 1;
const char c[3] = {'J', 'O', 'I'};
int n, k, cnt[N][3];
string s;
void ReadInput(){
cin >> n >> k;
cin >> s;
s = '*' + s;
}
void Solve(){
for(int i = 1; i <= n; i++)
for(int j = 0; j < 3; j++)
cnt[i][j] = cnt[i - 1][j] + (s[i] == c[j]);
int res = 1e9;
int ptr[4] = {1, 1, 1, 1};
for(ptr[0] = 0; ptr[0] < n; ptr[0]++){
for(int j = 1; j <= 3; j++)
while (ptr[j] < n && cnt[ptr[j]][j - 1] - cnt[ptr[j - 1]][j - 1] < k)
ptr[j]++;
if (cnt[ptr[3]][2] - cnt[ptr[2]][2] >= k)
res = min(res, (ptr[3] - ptr[0]) - 3 * k);
}
cout << (res == 1e9? -1 : res);
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
ReadInput();
Solve();
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... |