Submission #202358

#TimeUsernameProblemLanguageResultExecution timeMemory
202358dolphingarlicJJOOII 2 (JOI20_ho_t2)C++14
100 / 100
14 ms3072 KiB
#include <bits/stdc++.h>
#define FOR(i, x, y) for (int i = x; i < y; i++)
typedef long long ll;
using namespace std;

int pref[4][200001], ptr[4];

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n, k;
    string s;
    cin >> n >> k >> s;
    FOR(i, 0, n) {
        FOR(j, 1, 4) pref[j][i + 1] = pref[j][i];
        if (s[i] == 'J') pref[1][i + 1]++;
        if (s[i] == 'O') pref[2][i + 1]++;
        if (s[i] == 'I') pref[3][i + 1]++;
    }

    int ans = INT_MAX;
    for (ptr[0] = 0; ptr[0] <= n; ptr[0]++) {
        FOR(i, 1, 4)
            while (ptr[i] < ptr[i - 1] || (ptr[i] <= n && pref[i][ptr[i]] - pref[i][ptr[i - 1]] < k))
                ptr[i]++;
        bool possible = true;
        FOR(i, 1, 4) if (ptr[i] > n || pref[i][ptr[i]] - pref[i][ptr[i - 1]] < k) possible = false;
        if (possible) ans = min(ans, ptr[3] - ptr[0] - 3 * k);
    }
    if (ans == INT_MAX) cout << -1;
    else cout << ans;
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...