Submission #879451

# Submission time Handle Problem Language Result Execution time Memory
879451 2023-11-27T13:09:29 Z frostray8653 JJOOII 2 (JOI20_ho_t2) C++17
100 / 100
8 ms 5980 KB
#include <bits/stdc++.h>
#define int long long
#define IO ios::sync_with_stdio(0), cin.tie(0)
#define FOR(i, a, b) for (int i = a, I = b; i <= I; i++)
using namespace std;
using pii = pair<int, int>;
void dbg() {;}
template<class T, class ...U>
void dbg(T a, U ...b) { cout << a << " "; dbg(b...); }
void ent() { cout << "\n"; }
const int INF = 1e16;

const int N = 200005;
char a[N];
int cstj[N], csti[N];
int preo[N];

signed main() {
    IO;

    int n, k;
    cin >> n >> k;
    FOR(i, 1, n) cin >> a[i];

    fill(cstj, cstj + N, -1);
    fill(csti, csti + N, -1);
    deque<int> dq;
    for (int i = 1; i <= n; i++) {
        if (a[i] == 'J') {
            dq.push_back(i);
            while (dq.size() > k)
                dq.pop_front();
            if (dq.size() >= k)
                cstj[i] = (i - dq.front() + 1) - k;
        }
    }
    while (!dq.empty()) dq.pop_back();
    for (int i = n; i >= 1; i--) {
        if (a[i] == 'I') {
            dq.push_back(i);
            while (dq.size() > k)
                dq.pop_front();
            if (dq.size() >= k)
                csti[i] = (dq.front() - i + 1) - k;
        }
    }
    
    for (int i = 1; i <= n; i++)
        preo[i] = preo[i - 1] + (a[i] == 'O');
    
    int ans = INF;
    int r = 1;
    /// J O I
    /// 1 2 3
    for (int l = 1; l <= n - 2 * k; l++) {
        if (cstj[l] != -1) {
            r = max(r, l + 1);
            while (r <= n && (csti[r] == -1 || preo[r - 1] - preo[l] < k))
                r += 1;
            if (r <= n && csti[r] != -1 && preo[r - 1] - preo[l] >= k) {
                ans = min(ans, cstj[l] + csti[r] + (r - l - 1) - k);
            }
            // dbg(l, r); ent();
        }
    }
    cout << (ans == INF ? -1ll : ans) << "\n";

    return 0;
}

Compilation message

ho_t2.cpp: In function 'int main()':
ho_t2.cpp:31:30: warning: comparison of integer expressions of different signedness: 'std::deque<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
   31 |             while (dq.size() > k)
      |                    ~~~~~~~~~~^~~
ho_t2.cpp:33:27: warning: comparison of integer expressions of different signedness: 'std::deque<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
   33 |             if (dq.size() >= k)
      |                 ~~~~~~~~~~^~~~
ho_t2.cpp:41:30: warning: comparison of integer expressions of different signedness: 'std::deque<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
   41 |             while (dq.size() > k)
      |                    ~~~~~~~~~~^~~
ho_t2.cpp:43:27: warning: comparison of integer expressions of different signedness: 'std::deque<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
   43 |             if (dq.size() >= k)
      |                 ~~~~~~~~~~^~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4952 KB Output is correct
2 Correct 1 ms 5208 KB Output is correct
3 Correct 1 ms 4956 KB Output is correct
4 Correct 1 ms 4956 KB Output is correct
5 Correct 1 ms 4956 KB Output is correct
6 Correct 1 ms 4956 KB Output is correct
7 Correct 1 ms 4956 KB Output is correct
8 Correct 1 ms 4812 KB Output is correct
9 Correct 1 ms 4956 KB Output is correct
10 Correct 1 ms 4956 KB Output is correct
11 Correct 1 ms 4956 KB Output is correct
12 Correct 1 ms 4956 KB Output is correct
13 Correct 1 ms 4956 KB Output is correct
14 Correct 1 ms 4956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4952 KB Output is correct
2 Correct 1 ms 5208 KB Output is correct
3 Correct 1 ms 4956 KB Output is correct
4 Correct 1 ms 4956 KB Output is correct
5 Correct 1 ms 4956 KB Output is correct
6 Correct 1 ms 4956 KB Output is correct
7 Correct 1 ms 4956 KB Output is correct
8 Correct 1 ms 4812 KB Output is correct
9 Correct 1 ms 4956 KB Output is correct
10 Correct 1 ms 4956 KB Output is correct
11 Correct 1 ms 4956 KB Output is correct
12 Correct 1 ms 4956 KB Output is correct
13 Correct 1 ms 4956 KB Output is correct
14 Correct 1 ms 4956 KB Output is correct
15 Correct 1 ms 4956 KB Output is correct
16 Correct 1 ms 4956 KB Output is correct
17 Correct 2 ms 5108 KB Output is correct
18 Correct 1 ms 4956 KB Output is correct
19 Correct 1 ms 4952 KB Output is correct
20 Correct 1 ms 4960 KB Output is correct
21 Correct 1 ms 4960 KB Output is correct
22 Correct 1 ms 4956 KB Output is correct
23 Correct 1 ms 4960 KB Output is correct
24 Correct 1 ms 4960 KB Output is correct
25 Correct 1 ms 4956 KB Output is correct
26 Correct 1 ms 4960 KB Output is correct
27 Correct 1 ms 4816 KB Output is correct
28 Correct 1 ms 5208 KB Output is correct
29 Correct 1 ms 4956 KB Output is correct
30 Correct 1 ms 4960 KB Output is correct
31 Correct 1 ms 4964 KB Output is correct
32 Correct 1 ms 4964 KB Output is correct
33 Correct 1 ms 4964 KB Output is correct
34 Correct 1 ms 4964 KB Output is correct
35 Correct 1 ms 4964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4952 KB Output is correct
2 Correct 1 ms 5208 KB Output is correct
3 Correct 1 ms 4956 KB Output is correct
4 Correct 1 ms 4956 KB Output is correct
5 Correct 1 ms 4956 KB Output is correct
6 Correct 1 ms 4956 KB Output is correct
7 Correct 1 ms 4956 KB Output is correct
8 Correct 1 ms 4812 KB Output is correct
9 Correct 1 ms 4956 KB Output is correct
10 Correct 1 ms 4956 KB Output is correct
11 Correct 1 ms 4956 KB Output is correct
12 Correct 1 ms 4956 KB Output is correct
13 Correct 1 ms 4956 KB Output is correct
14 Correct 1 ms 4956 KB Output is correct
15 Correct 1 ms 4956 KB Output is correct
16 Correct 1 ms 4956 KB Output is correct
17 Correct 2 ms 5108 KB Output is correct
18 Correct 1 ms 4956 KB Output is correct
19 Correct 1 ms 4952 KB Output is correct
20 Correct 1 ms 4960 KB Output is correct
21 Correct 1 ms 4960 KB Output is correct
22 Correct 1 ms 4956 KB Output is correct
23 Correct 1 ms 4960 KB Output is correct
24 Correct 1 ms 4960 KB Output is correct
25 Correct 1 ms 4956 KB Output is correct
26 Correct 1 ms 4960 KB Output is correct
27 Correct 1 ms 4816 KB Output is correct
28 Correct 1 ms 5208 KB Output is correct
29 Correct 1 ms 4956 KB Output is correct
30 Correct 1 ms 4960 KB Output is correct
31 Correct 1 ms 4964 KB Output is correct
32 Correct 1 ms 4964 KB Output is correct
33 Correct 1 ms 4964 KB Output is correct
34 Correct 1 ms 4964 KB Output is correct
35 Correct 1 ms 4964 KB Output is correct
36 Correct 6 ms 5476 KB Output is correct
37 Correct 7 ms 5476 KB Output is correct
38 Correct 7 ms 5472 KB Output is correct
39 Correct 7 ms 5476 KB Output is correct
40 Correct 6 ms 5600 KB Output is correct
41 Correct 7 ms 5468 KB Output is correct
42 Correct 7 ms 5556 KB Output is correct
43 Correct 4 ms 5468 KB Output is correct
44 Correct 5 ms 5544 KB Output is correct
45 Correct 6 ms 5844 KB Output is correct
46 Correct 6 ms 5724 KB Output is correct
47 Correct 8 ms 5712 KB Output is correct
48 Correct 6 ms 5720 KB Output is correct
49 Correct 4 ms 5468 KB Output is correct
50 Correct 6 ms 5724 KB Output is correct
51 Correct 6 ms 5724 KB Output is correct
52 Correct 5 ms 5832 KB Output is correct
53 Correct 6 ms 5964 KB Output is correct
54 Correct 5 ms 5468 KB Output is correct
55 Correct 4 ms 5880 KB Output is correct
56 Correct 5 ms 5980 KB Output is correct