Submission #1279117

#TimeUsernameProblemLanguageResultExecution timeMemory
1279117hiepsimauhongJJOOII 2 (JOI20_ho_t2)C++20
100 / 100
9 ms4624 KiB
#include <bits/stdc++.h>

using namespace std;

//#define int long long

#define FOR(I, L, R) for(int I(L) ; I <= (int)R ; ++I)
#define FOD(I, R, L) for(int I(R) ; I >= (int)L ; --I)
#define FOA(I, A) for(auto &I : A)

#define print(A,L,R) FOR(OK, L, R){if(A[OK]<=-oo / 10||A[OK]>=oo)cout<<"- ";else cout<<A[OK]<<' ';}cout<<'\n';
#define prints(A) FOA(OK, A){cout<<OK<<' ';}cout << '\n';
#define printz(A,L,R) FOR(OK, 0, L){FOR(KO, 1, R){if(A[OK][KO]>-oo&&A[OK][KO]<oo)cout<<A[OK][KO]<<' ';else cout << "- ";} cout << '\n';}cout << '\n';

#define fs first
#define sd second
#define ii pair<int,int>
#define iii pair<int, ii>
#define all(A) A.begin(), A.end()
#define quickly cin.tie(0) -> ios_base::sync_with_stdio(0);

const int N = 2e5 + 5;
const int mod = 1e9 + 7;
const int oo = 1e9;

int n, k;
int a[N], dp[4][N];
string s;

signed main(){ quickly
        cin >> n >> k >> s;

        s = " " + s;

        FOR(i, 1, n){
                if(s[i] == 'J'){
                        a[i] = 1;
                }
                else if(s[i] == 'O'){
                        a[i] = 2;
                }
                else{
                        a[i] = 3;
                }
        }

        memset(dp, 0x3f, sizeof dp);
        FOR(i, 0, n){
                dp[0][i] = 0;
        }

        FOR(p, 1, 3){
                int lastK = 1, cnt = 0;

                FOR(i, 1, n){
                        cnt += (a[i] == p);
                        while(cnt - (a[lastK] == p) >= k){
                                cnt -= (a[lastK] == p);
                                lastK++;
                        }

                        if(a[i] != p){
                                dp[p][i] = dp[p][i - 1] + 1;
                        }
                        else{
                                if(cnt == k){
                                        dp[p][i] = i - lastK + 1 - k + dp[p - 1][lastK - 1];
                                }
                        }
                }
        }

        int ans = oo;
        FOR(i, 1, n){
                ans = min(ans, dp[3][i]);
        }

        if(ans == oo){
                cout << -1;
                return 0;
        }
        cout << ans;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...