Submission #1293093

#TimeUsernameProblemLanguageResultExecution timeMemory
1293093thaibeo123JJOOII 2 (JOI20_ho_t2)C++20
100 / 100
5 ms2004 KiB
#include <bits/stdc++.h>
using namespace std;

#define NAME "A"
#define ll long long
#define fi first
#define se second
#define pb push_back
#define all(x) x.begin(), x.end()
#define MASK(x) (1ll << (x))
#define BIT(x, i) (((x) >> (i)) & 1)

const int N = 2e5 + 5;
const int inf = 1e9;

int n, k;
string s;
deque<int> J, O, I;

void input() {
    cin >> n >> k >> s;
    s = " " + s;
}

void solve() {
    int ans = inf;
    for (int i = 1; i <= n; i++) {
        if (s[i] == 'J') J.pb(i);
        else if (s[i] == 'O') O.pb(i);
        else I.pb(i);
    }
    while ((int)J.size() >= k) {
        while (O.size() && O.front() <= J[k - 1]) {
            O.pop_front();
        }
        if ((int)O.size() < k) break;
        while (I.size() && I.front() <= O[k - 1]) {
            I.pop_front();
        }
        if ((int)I.size() < k) break;
        ans = min(ans, I[k - 1] - J[0] - 3 * k + 1);
        J.pop_front();
    }
    if (ans == inf) {
        ans = -1;
    }
    cout << ans;
}

signed main() {
    if (fopen(NAME".INP", "r")) {
        freopen(NAME".INP", "r", stdin);
        freopen(NAME".OUT", "w", stdout);
    }
    cin.tie(0)->sync_with_stdio(0);

    input();
    solve();

    return 0;
}

Compilation message (stderr)

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