Submission #201408

#TimeUsernameProblemLanguageResultExecution timeMemory
201408karmaJJOOII 2 (JOI20_ho_t2)C++14
100 / 100
16 ms2948 KiB
#include <bits/stdc++.h>
#define pb          emplace_back
#define ll          long long
#define fi          first
#define se          second
#define mp          make_pair
//#define int         int64_t

using namespace std;

const int N = int(2e5) + 5;

typedef pair<int, int> pii;
string s;
int n, k, jump[N][3], cnt;

int32_t main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    #define Task        "test"
    if(fopen(Task".inp", "r")) {
        freopen(Task".inp", "r", stdin);
        freopen(Task".out", "w", stdout);
    }
    cin >> n >> k >> s;
    for(int i = 0, j = -1; i < n; ++i) {
        while(cnt < k && j + 1 < n) {
            ++j;
            if(s[j] == 'J') ++cnt;
        }
        if(cnt >= k) jump[i][0] = j;
        else jump[i][0] = n + n;
        if(s[i] == 'J') --cnt;
    } cnt = 0;
    for(int i = 0, j = -1; i < n; ++i) {
        while(cnt < k && j + 1 < n) {
            ++j;
            if(s[j] == 'O') ++cnt;
        }
        if(cnt >= k) jump[i][1] = j;
        else jump[i][1] = n + n;
        if(s[i] == 'O') --cnt;
    } cnt = 0;
    for(int i = 0, j = -1; i < n; ++i) {
        while(cnt < k && j + 1 < n) {
            ++j;
            if(s[j] == 'I') ++cnt;
        }
        if(cnt >= k) jump[i][2] = j;
        else jump[i][2] = n + n;
        if(s[i] == 'I') --cnt;
    }
    int ans = n + n;
    for(int i = 0, j; i < n; ++i) {
        j = jump[i][0];
        if(j >= n) continue;
        j = jump[j][1];
        if(j >= n) continue;
        j = jump[j][2];
        if(j >= n) continue;
        ans = min(ans, j - i + 1 - k * 3);
    }
    if(ans == n + n) ans = -1;
    cout << ans;
}

Compilation message (stderr)

ho_t2.cpp: In function 'int32_t main()':
ho_t2.cpp:22:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
         freopen(Task".inp", "r", stdin);
         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
ho_t2.cpp:23:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
         freopen(Task".out", "w", stdout);
         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...