Submission #367200

#TimeUsernameProblemLanguageResultExecution timeMemory
367200piddddgyJJOOII 2 (JOI20_ho_t2)C++11
13 / 100
2087 ms1016 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define cerr if(false) cerr
#define watch(x) cerr << (#x) << " is " << (x) << endl;
#define endl '\n'
#define ld long double
#define int long long
#define pii pair<int, int>
#define fi first
#define se second
#define sz(a) (int)(a).size()
#define all(x) (x).begin(), (x).end()

int n, k;
string s;



signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    cin >> n >> k;
    cin >> s;

    s = "."+s;

    int l = 1, r = n;
    int mi = 1e18;

    while(l <= r) {
        int mid = (l+r)/2;

        bool ok = false;
        cerr << "trying " << mid << endl;
        for(int i = 1; i+mid-1 <= n; i++) {
            cerr << "from " << i << endl;
            queue<char> Q;

            for(char chr: string("JOI")) {
                for(int j = 0; j < k; j++) {
                    Q.push(chr);
                }
            }

            for(int j = 0; j < mid; j++) {
                if(sz(Q) and s[i+j] == Q.front()) {
                    cerr << "on " << i+j << endl;
                    cerr << "popping " << Q.front() << endl;
                    Q.pop();
                }
            }

            if(Q.empty()) ok = true;
        }

        watch(ok)
        if(ok) {
            mi = mid;
            r = mid-1;
        } else {
            l = mid+1;
        }
    }

    // watch(mi)

    if(mi == 1e18) {
        cout << -1 << endl;
    } else {
        cout << mi-3*k << endl;
    }
}

/*

find smallest subarray with k-level JOI as subsequence

bsearch

Did you read the bounds?
Did you make typos?
Are there edge cases (N=1?)
Are array sizes proper?
Integer overflow?
DS reset properly between test cases?
Is using long longs causing TLE?
Are you using floating points?
*/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...