제출 #1345674

#제출 시각아이디문제언어결과실행 시간메모리
1345674chithanhnguyenJJOOII 2 (JOI20_ho_t2)C++20
100 / 100
8 ms6676 KiB
/*
Author: Nguyen Chi Thanh - High School for the Gifted - VNU.HCM (i2528)
*/
#include <bits/stdc++.h>
using namespace std;

/* START OF TEMPALTE */

// #define int long long
#define ll long long
#define ull unsigned long long
#define ld long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define fi first
#define se second
#define __builtin_popcount __builtin_popcountll
#define all(x) (x).begin(), (x).end()
#define BIT(x, i) (((x) >> (i)) & 1)
#define MASK(x) (1ll << (x))
#define SZ(a) ((int32_t)a.size())

#define debug(a, l, r) {for (int _i = (l); _i <= (r); ++_i) cout << (a)[_i] << ' '; cout << '\n';}

template<class X, class Y>
bool minimize(X &x, const Y &y) {
    if (x > y) {
        x = y;
        return true;
    } else return false;
}

template<class X, class Y>
bool maximize(X &x, const Y &y) {
    if (x < y) {
        x = y;
        return true;
    } else return false;
}

/* END OF TEMPALTE */

const int MAXN = 2e5 + 5;
int n, k, a[MAXN];

void init() {
    string s;
    cin >> n >> k >> s;
    s = ' ' + s;

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

int jump[3][MAXN], nxt[3][MAXN];

void build_jump(int x) {
    for (int i = 1; i <= n; ++i) {
        nxt[x][i]  = n + 1;
        jump[x][i] = n + 1;
    }
    
    vector<int> pos;
    for (int i = 1; i <= n; ++i)
        if (a[i] == x) pos.push_back(i);

    for (int i = 0; i <= SZ(pos) - k; ++i)
        jump[x][pos[i]] = pos[i + k - 1];
    
    for (int i = n - 1; i >= 1; --i)
        if (a[i + 1] == x) nxt[x][i] = i + 1;
        else nxt[x][i] = nxt[x][i + 1];
}

void solve() {
    for (int i = 0; i < 3; ++i)
        build_jump(i);

    int minlen = n + 5;
    for (int i = 1; i <= n; ++i) {
        if (a[i] != 0) continue;
        
        int ptr = jump[0][i];
        if (ptr > n) continue;
        ptr = nxt[1][ptr];
        if (ptr > n) continue;
        ptr = jump[1][ptr];
        if (ptr > n) continue;
        ptr = nxt[2][ptr];
        if (ptr > n) continue;
        ptr = jump[2][ptr];
        if (ptr > n) continue;

        int len = ptr - i + 1;
        minimize(minlen, len);
    } 

    if (minlen > n) cout << -1;
    else cout << minlen - 3 * k;
}

signed main() {
    #ifdef NCTHANH
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    ios_base::sync_with_stdio(0);
    cin.tie(nullptr); cout.tie(nullptr);

    init();
    solve();

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