This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
5:05開始,也不算是virtual,因為我已經知道幾個人的分數了,也在去年資芽時聽過p4的題敘並想過。
所以就只是計時練習,應該會寫到19:05,然後等家長買晚餐回來吃後再把剩下的兩小時用一用,衝到div2就打div2
目標日本選訓!!
*/
//喔,就只是雙指針而已
#include <bits/stdc++.h>
using namespace std;
string s;
int n, k, a[310000] = {0};
int cnt[3] = {0}, wow[210000][3];
int main () {
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> n >> k;
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 z = 0; z < 3; z++) {
for (int i = 1, j = 0, cnt = 0; i <= n; i++) {
while (j + 1 <= n and cnt < k) {
j++;
if (a[j] == z) cnt++;
}
if (cnt == k) wow[i][z] = j + 1;
else wow[i][z] = 209999;
if (a[i] == z) cnt--;
}
}
wow[n+1][0] = wow[n+1][1] = wow[209999][0] = wow[209999][1] = 209999, wow[n+1][2] = wow[209999][2] = n * 114;
int ans = INT_MAX;
for (int i = 1; i <= n; i++) {
// cout << i << ' ' << wow[i][0] << ' ' << wow[i][1] << ' ' << wow[i][2] << '\n';
// cout << i << ' ' << wow[wow[wow[i][0]][1]][2] << '\n';
ans = min(ans, wow[wow[wow[i][0]][1]][2] - i - k * 3 );
}
if (ans > n) ans = -1;
cout << ans << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |