제출 #1300189

#제출 시각아이디문제언어결과실행 시간메모리
1300189hynmjJJOOII 2 (JOI20_ho_t2)C++20
100 / 100
19 ms5332 KiB
#include <bits/stdc++.h>
#define int long long
using namespace std;
const long long N = 2e5 + 5;
int a[N];
int n, k;
int p1[N];
int p2[N];
int p3[N];
void solve()
{
    int n, k;
    string s;
    cin >> n >> k >> s;

    for (int i = 1; i <= n; i++)
    {
        p1[i] = p1[i - 1];
        p2[i] = p2[i - 1];
        p3[i] = p3[i - 1];
        if (s[i - 1] == 'J')
            p1[i]++;
        else if (s[i - 1] == 'O')
            p2[i]++;
        else
            p3[i]++;
    }

    int ans = 1e18;
    for (int i = 0; i <= n; i++)
    {
        int next = lower_bound(p1, p1 + n + 1, p1[i] + k) - p1;
        if (next > n)
            continue;
        int J = next;

        next = lower_bound(p2, p2 + n + 1, p2[J] + k) - p2;
        if (next > n)
            continue;
        int O = next;

        next = lower_bound(p3, p3 + n + 1, p3[O] + k) - p3;
        if (next > n)
            continue;
        int I = next;

        if (p1[J] - p1[i] >= k and p2[O] - p2[J] >= k and p3[I] - p3[O] >= k)
            ans = min(ans, I - i);
    }
    cout << ((ans == 1e18) ? -1 : ans - 3 * k);
}

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);
    int t = 1;
    for (int i = 1; i <= t; i++)
    {
        solve();
        cout << endl;
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...