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;
using ll = long long;
using ii = pair<int, int>;
const int maxn = 2e5 + 5;
int n, k;
int cnt[3][maxn];
int to[3][maxn];
int a[maxn];
int main(int argc, char const *argv[])
{
#ifdef LOCAL
freopen("in", "r", stdin);
#endif
cin >> n >> k;
{
string s; cin >> s;
for(int i = 1; i <= n; i++) {
if(s[i-1] == 'J') a[i] = 0;
if(s[i-1] == 'O') a[i] = 1;
if(s[i-1] == 'I') a[i] = 2;
}
}
for(int i = 1; i <= n; i++) {
cnt[a[i]][i]++;
cnt[0][i] += cnt[0][i-1];
cnt[1][i] += cnt[1][i-1];
cnt[2][i] += cnt[2][i-1];
}
if(cnt[0][n] < k or cnt[1][n] < k or cnt[2][n] < k) {
cout << "-1" << endl;
return 0;
}
memset(to, -1, sizeof to);
for(int x = 0; x < 3; x++) {
int *cc = cnt[x];
int r = 0;
for(int i = 0; i < n; i++) {
while(r < n and cc[r] - cc[i] < k) {
r++;
}
if(cc[r] - cc[i] == k) {
to[x][i] = r;
}
}
}
int ans = INT_MAX;
for(int i = 0; i < n; i++) {
int now = i, flag = 1;
for(int x = 0; x < 3; x++) {
if(to[x][now] == -1) {
flag = 0;
break;
}
now = to[x][now];
}
if(flag) {
ans = min(ans, now - i);
}
}
if(ans == INT_MAX) {
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... |