제출 #1287362

#제출 시각아이디문제언어결과실행 시간메모리
1287362MinhKienJJOOII 2 (JOI20_ho_t2)C++20
100 / 100
25 ms3088 KiB
#include <iostream>
#include <string>

using namespace std;

const int N = 2e5 + 10;

int n, k, a[N][3];
string s;

int main() {
    cin.tie(0); cout.tie(0);
    ios_base::sync_with_stdio(false);

    cin >> n >> k >> s;

    s = " " + s;
    for (int i = 1; i <= n; ++i) {
        for (int j = 0; j < 3; ++j) a[i][j] = a[i - 1][j];
        if (s[i] == 'J') ++a[i][0];
        else if (s[i] == 'O') ++a[i][1];
        else ++a[i][2];
    }

    int ans = N, l, r, mid, nxt, last;
    for (int i = 1; i <= n; ++i) {
        last = i - 1;
        l = i, r = n, nxt = -1;
        while (l <= r) {
            mid = (l + r) >> 1;

            if (a[mid][0] - a[last][0] >= k) {
                r = mid - 1;
                nxt = mid;
            } else {
                l = mid + 1;
            }
        }

        if (nxt == -1) continue;

        last = nxt;
        l = nxt + 1, r = n, nxt = -1;

        while (l <= r) {
            mid = (l + r) >> 1;

            if (a[mid][1] - a[last][1] >= k) {
                r = mid - 1;
                nxt = mid;
            } else {
                l = mid + 1;
            }
        }

        if (nxt == -1) continue;

        last = nxt;
        l = nxt + 1, r = n, nxt = -1;

        while (l <= r) {
            mid = (l + r) >> 1;

            if (a[mid][2] - a[last][2] >= k) {
                r = mid - 1;
                nxt = mid;
            } else {
                l = mid + 1;
            }
        }

        if (nxt == -1) continue;

        ans = min(ans, nxt - i + 1 - 3 * k);
    }

    if (ans == N) ans = -1;
    cout << ans << "\n";

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...