Submission #995261

#TimeUsernameProblemLanguageResultExecution timeMemory
995261gmroh06JJOOII 2 (JOI20_ho_t2)C++14
100 / 100
6 ms5028 KiB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using pll = pair<ll, ll>;

inline void fastio() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
}

const ll INF = 1e16;

ll n, k, ans = INF;
vector<ll> a, b, v;
string s;

int main() {
    fastio();

    cin >> n >> k >> s;

    a.resize(n + 2);
    b.resize(n + 2);

    a[0] = b[n + 1] = -1;

    for (ll i = 1; i <= n; i++) {
        if (s[i - 1] == 'J') {
            v.emplace_back(i);
        }

        if (v.size() < k) {
            a[i] = -1;
        } else {
            a[i] = *(v.end() - k);
        }
    }

    v.clear();

    for (ll i = n; i > 0; i--) {
        if (s[i - 1] == 'I') {
            v.emplace_back(i);
        }

        if (v.size() < k) {
            b[i] = -1;
        } else {
            b[i] = *(v.end() - k);
        }
    }

    v.clear();

    for (ll i = 1; i <= n; i++) {
        if (s[i - 1] == 'O') {
            v.emplace_back(i);
        }

        if (v.size() < k) {
            continue;
        }

        ll l = a[*(v.end() - k) - 1], r = b[i + 1];

        if (l > -1 and r > -1) {
            ans = min(ans, r - l + 1);
        }
    }

    cout << (ans == INF ? -1 : ans - 3 * k);

    return 0;
}

Compilation message (stderr)

ho_t2.cpp: In function 'int main()':
ho_t2.cpp:34:22: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'll' {aka 'long long int'} [-Wsign-compare]
   34 |         if (v.size() < k) {
      |             ~~~~~~~~~^~~
ho_t2.cpp:48:22: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'll' {aka 'long long int'} [-Wsign-compare]
   48 |         if (v.size() < k) {
      |             ~~~~~~~~~^~~
ho_t2.cpp:62:22: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'll' {aka 'long long int'} [-Wsign-compare]
   62 |         if (v.size() < k) {
      |             ~~~~~~~~~^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...