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;
const int maxn = 200005;
int n, k;
string s;
int pref[3][maxn];
int a[maxn];
int main(int argc, char const *argv[])
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> k;
cin >> s;
for(int i = 0; i < n; ++i){
if(s[i] == 'J')a[i] = 0;
else if(s[i] == 'O')a[i] = 1;
else a[i] = 2;
pref[a[i]][i] = 1;
}
for(int i = 0; i < 3; ++i){
for(int j = 1; j < n; ++j){
pref[i][j] += pref[i][j - 1];
}
}
int ans = n + 1;
for(int i = 0; i < n; ++i){
if(s[i] != 'J')continue;
int l = i, r = n - 1, ans1 = -1;
while(l <= r){
int mid = (l + r) >> 1;
if(pref[0][mid] - pref[0][i - 1] >= k){
ans1 = mid;
r = mid - 1;
}else {
l = mid + 1;
}
}
if(ans1 == -1)break;
int idx = ans1;
l = idx, r = n - 1, ans1 = -1;
while(l <= r){
int mid = (l + r) >> 1;
if(pref[1][mid] - pref[1][idx - 1] >= k){
ans1 = mid;
r = mid - 1;
}else {
l = mid + 1;
}
}
if(ans1 == -1)break;
idx = ans1;
l = idx, r = n - 1, ans1 = -1;
while(l <= r){
int mid = (l + r) >> 1;
if(pref[2][mid] - pref[2][idx - 1] >= k){
ans1 = mid;
r = mid - 1;
}else {
l = mid + 1;
}
}
if(ans1 == -1)break;
ans = min(ans, ans1 - i + 1 - (3 * k));
}
if(ans == n + 1)cout << "-1" << '\n';
else cout << ans << '\n';
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... |