Submission #974914

#TimeUsernameProblemLanguageResultExecution timeMemory
974914berrJJOOII 2 (JOI20_ho_t2)C++17
100 / 100
8 ms5636 KiB
#include <bits/stdc++.h>
using namespace std; 
#define int long long
const int N=2e5+30, INF=1e18;
int pref[3][N];
signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    
    int n, k; cin >> n >> k;
    
    string s; cin >> s;

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

    auto val=[&](int l, int r, int type){
        if(l>r) return 0LL;
        if(r==n) return 0LL;
        if(l==n) return 0LL;

        if(l==0) return pref[type][r];
        return pref[type][r]-pref[type][l-1];
    };


    int posj=0, poso=0, posi=0, ans=n+1;
    for(int i=0; i<n; i++){
        while(posj<n&&val(i, posj, 0)<k) posj++;

        while(poso<n&&val(posj+1, poso, 1)<k) poso++;
        while(posi<n&&val(poso+1, posi, 2)<k) posi++;

        if(val(i, posj, 0)==k && val(posj+1, poso, 1)==k && val(poso+1, posi, 2)==k){
            ans=min(ans, posi-i+1-k*3);
        }
    }

    if(ans==n+1) cout<<-1;
    else cout<<ans;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...