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 int long long
using namespace std;
const int INF = 1e9;
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0);
int n, k;
cin >> n >> k;
string s;
cin >> s;
vector<vector<int>> pref(3, vector<int>(n));
for (int i = 0; i < n; ++i) {
if (i)
for (int j = 0; j < 3; ++j)
pref[j][i] = pref[j][i - 1];
if (s[i] == 'J')
pref[0][i] += 1;
else if (s[i] == 'O')
pref[1][i] += 1;
else
pref[2][i] += 1;
}
int ans = INF;
for (int i = 0; i + 3 * k <= n; ++i) {
int needid = i;
for (int j = 0; j < 3; ++j) {
int prval = (needid ? pref[j][needid - 1] : 0);
int curid = lower_bound(pref[j].begin(), pref[j].end(), prval + k) - pref[j].begin();
needid = max(needid, curid);
}
if (needid < n)
ans = min(ans, (needid - i + 1) - 3 * k);
}
cout << (ans == INF ? -1 : ans);
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |