제출 #974916

#제출 시각아이디문제언어결과실행 시간메모리
974916berrJJOOII 2 (JOI20_ho_t2)C++17
100 / 100
10 ms6672 KiB
#include <bits/stdc++.h>
using namespace std; 
#define int long long
const int N=5005, INF=1e18;
 
signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    
    int n, k; cin >> n >> k;
    vector<vector<int>> pref(3, vector<int>(n, 0));
 
    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==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...