Submission #1184544

#TimeUsernameProblemLanguageResultExecution timeMemory
1184544jerzykJJOOII 2 (JOI20_ho_t2)C++20
100 / 100
3 ms3340 KiB
#include <bits/stdc++.h>

using namespace std;
#define pb push_back
#define st first
#define nd second
typedef long long ll;
typedef long double ld;
const ll I = 10'000'000'000'000'000LL;
const int II = 2'000'000'000;
const ll M = 1000'000'007LL;
const int N = 200'007;
vector<int> cur[3];
int gj[N], go[N];
int tp[200];

int Get(int r, int k)
{
    if((int)cur[r].size() < k) return -1;
    return cur[r][(int)cur[r].size() - k];
}

void Solve()
{
    int n, k, ans = II;
    string s;
    cin >> n >> k;
    cin >> s;
    tp['J'] = 0; tp['O'] = 1; tp['I'] = 2;
    for(int i = 0; i < n; ++i)
    {
        cur[tp[(int)s[i]]].pb(i);
        gj[i] = Get(0, k);
        go[i] = Get(1, k);
        int l = Get(2, k);
        if(l <= 0) continue;
        l = go[l - 1];
        if(l <= 0) continue;
        l = gj[l - 1];
        if(l == -1) continue;
        ans = min(ans, (i - l + 1) - 3 * k);
    }
    if(ans < II)
        cout << ans << "\n";
    else
        cout << -1 << "\n";
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    //int t; cin >> t;
    //while(t--)
        Solve();

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