Submission #1149749

#TimeUsernameProblemLanguageResultExecution timeMemory
1149749minggaJJOOII 2 (JOI20_ho_t2)C++20
100 / 100
13 ms5460 KiB
#include "bits/stdc++.h"

using namespace std;

#define ln "\n"
#define pb push_back
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define int long long
const int mod = 1e9 + 7;
const int inf = 2e18;
const int N = 2e5 + 7;
int n, k, f[N][3];
string s;

int id[200];

signed main() {
    cin.tie(0) -> sync_with_stdio(0);
    #define task ""
    if(fopen(task ".INP", "r")) {
        freopen(task ".INP", "r", stdin);
        freopen(task ".OUT", "w", stdout);
    }
    cin >> n >> k;
    cin >> s;
    id['J'] = 0;
    id['O'] = 1;
    id['I'] = 2;
    s = ' ' + s;
    for(int i = 1; i <= n; i++) {
        int x = id[s[i]];
        for(int j = 0; j < 3; j++) {
            f[i][j] = f[i - 1][j];
        }
        f[i][x]++;
    }
    int ans = inf;
    for(int i = 1; i <= n; i++) {
        if(s[i] == 'J') {
            int l = i, r = n;
            int nxt1 = n + 1;
            while(l <= r) {
                int m = (l + r) >> 1;
                if(f[m][0] - f[i - 1][0] >= k) {
                    r = m - 1;
                    nxt1 = m + 1;
                } else l = m + 1;
            }
            if(nxt1 > n) continue;
            l = nxt1, r = n;
            int nxt2 = n + 1;
            while(l <= r) {
                int m = (l + r) >> 1;
                if(f[m][1] - f[nxt1 - 1][1] >= k) {
                    r = m - 1;
                    nxt2 = m + 1;
                } else l = m + 1;
            }
            if(nxt2 > n) continue;
            l = nxt2, r = n;
            int en = n + 1;
            while(l <= r) {
                int m = (l + r) >> 1;
                if(f[m][2] - f[nxt2 - 1][2] >= k) {
                    r = m - 1;
                    en = m;
                } else l = m + 1;
            }
            if(en > n) continue;
            ans = min(ans, (en - i + 1) - 3 * k);
        }
    }
    cout << (ans == inf ? -1 : ans) << ln;
    cerr << "\nTime: " << clock() * 1000 / CLOCKS_PER_SEC;
}

Compilation message (stderr)

ho_t2.cpp: In function 'int main()':
ho_t2.cpp:24:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   24 |         freopen(task ".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
ho_t2.cpp:25:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   25 |         freopen(task ".OUT", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...