제출 #369295

#제출 시각아이디문제언어결과실행 시간메모리
369295phathnvJJOOII 2 (JOI20_ho_t2)C++11
100 / 100
10 ms3276 KiB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 2e5 + 1;
const char c[3] = {'J', 'O', 'I'};

int n, k, cnt[N][3];
string s;

void ReadInput(){
    cin >> n >> k;
    cin >> s;
    s = '*' + s;
}

void Solve(){
    for(int i = 1; i <= n; i++)
        for(int j = 0; j < 3; j++)
            cnt[i][j] = cnt[i - 1][j] + (s[i] == c[j]);

    int res = 1e9;
    int ptr[4] = {1, 1, 1, 1};
    for(ptr[0] = 0; ptr[0] < n; ptr[0]++){
        for(int j = 1; j <= 3; j++)
            while (ptr[j] < n && cnt[ptr[j]][j - 1] - cnt[ptr[j - 1]][j - 1] < k)
                ptr[j]++;
        if (cnt[ptr[3]][2] - cnt[ptr[2]][2] >= k)
            res = min(res, (ptr[3] - ptr[0]) - 3 * k);
    }
    cout << (res == 1e9? -1 : res);
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    ReadInput();
    Solve();
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...