제출 #890573

#제출 시각아이디문제언어결과실행 시간메모리
890573hafoJJOOII 2 (JOI20_ho_t2)C++14
100 / 100
10 ms5544 KiB
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define pb push_back
#define pa pair<int, int>
#define pall pair<ll, int>
#define fi first
#define se second
#define TASK "test"
#define Size(x) (int) x.size()
#define all(x) x.begin(), x.end()
using namespace std;

template<typename T1, typename T2> bool mini (T1 &a, T2 b) {if(a > b) a = b; else return 0; return 1;}
template<typename T1, typename T2> bool maxi (T1 &a, T2 b) {if(a < b) a = b; else return 0; return 1;}

const int MOD = 1e9 + 7;
const int LOG = 20;
const int maxn = 2e5 + 7;
const ll oo = 1e9 + 69;

int n, k, f[maxn][3], nxt[3][maxn];
char a[maxn];

int convert(char ch) {
    if(ch == 'J') return 0;
    if(ch == 'O') return 1;
    return 2;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    //freopen(TASK".inp", "r", stdin);
    //freopen(TASK".out", "w", stdout);

    cin>>n>>k;
    for(int i = 1; i <= n; i++) {
        cin>>a[i];
        for(int j = 0; j < 3; j++) f[i][j] = f[i - 1][j];
        f[i][convert(a[i])]++;
    }

    for(int t = 0; t < 3; t++) {
        int j = n;
        nxt[t][n + 1] = n + 1;
        for(int i = n; i >= 1; i--) {
            nxt[t][i] = n + 1;
            while(f[j - 1][t] - f[i - 1][t] >= k) j--;
            if(f[j][t] - f[i - 1][t] == k) nxt[t][i] = j;
        }
    }

    int ans = oo;
    for(int i = 1; i <= n; i++) {
        int p = i;
        for(int j = 0; j < 3; j++) p = nxt[j][p];
        if(p == n + 1) break;
        mini(ans, p - i + 1 - 3 * k);
    }
    cout<<(ans == oo ? -1:ans);
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...