Submission #746289

# Submission time Handle Problem Language Result Execution time Memory
746289 2023-05-22T07:58:04 Z baluteshih Chorus (JOI23_chorus) C++14
100 / 100
2094 ms 37488 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define X first
#define Y second
#define SZ(a) ((int)a.size())
#define ALL(v) v.begin(), v.end()
#define pb push_back

int lft[1000005], lst[1000005];
ll prv[1000005];
pll dp[1000005];

pll operator+(const pll &a, const ll &b) { return pll(a.X + b, a.Y + 1); }

int main() {
    ios::sync_with_stdio(0), cin.tie(0);
    int n, k;
    string s;
    cin >> n >> k >> s;

    int cntA = 0, cntB = 0;
    for (int i = 0; i < n + n; ++i) {
        if (s[i] == 'B') ++cntB;
        else lft[++cntA] = cntB;
    }
    for (int i = 1; i <= n; ++i)
        prv[i] = prv[i - 1] + lft[i];
    for (int i = 0; i <= n; ++i)
        lst[lft[i]] = i;
    for (int i = 1; i <= n; ++i)
        lst[i] = max(lst[i], lst[i - 1]);
    for (int i = 0; i <= n; ++i)
        lst[i] = max(i, lst[i]);

    auto line = [&](int q) {
        return pll(-q, dp[q].X - prv[lst[q]] + (ll)q * lst[q]);  
    };

    auto challenge = [&](int x, int y, int z) {
        auto [ax, bx] = line(x);
        auto [ay, by] = line(y);
        auto [az, bz] = line(z);
        return (by - bx) * (ax - az) >= (bz - bx) * (ax - ay);
    };

    auto get_val = [&](int q, int j) {
        auto [a, b] = line(q);
        return pll(a * j + b, dp[q].Y);
    };

    const ll INF = 2e12;
    auto cal = [&](ll mid) {
        fill(dp, dp + n + 1, pll(INF, INF));
        dp[0] = pll(0, 0);
        deque<int> dq;
        for (int j = 1, p = 0; j <= n; ++j) {
            while (lst[p] <= j && p < j) {
                while (SZ(dq) >= 2 && challenge(dq[SZ(dq) - 2], dq.back(), p))
                    dq.pop_back();
                dq.pb(p);
                ++p;
            }
            if (p < j) 
                dp[j] = min(dp[j], dp[p] + mid);
            while (SZ(dq) >= 2 && get_val(dq[0], j) >= get_val(dq[1], j))
                dq.pop_front();
            if (!dq.empty())
                dp[j] = min(dp[j], get_val(dq[0], j) + (mid + prv[j]));
        }
        return dp[n];
    };

    ll l = 0, r = (ll)n * n;
    while (l < r) {
        ll mid = (l + r) >> 1;
        if (cal(mid).Y <= k) r = mid;
        else l = mid + 1;
    }

    cout << cal(l).X - k * l << "\n";
}

Compilation message

chorus.cpp: In lambda function:
chorus.cpp:43:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   43 |         auto [ax, bx] = line(x);
      |              ^
chorus.cpp:44:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   44 |         auto [ay, by] = line(y);
      |              ^
chorus.cpp:45:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   45 |         auto [az, bz] = line(z);
      |              ^
chorus.cpp: In lambda function:
chorus.cpp:50:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   50 |         auto [a, b] = line(q);
      |              ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 296 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 264 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 296 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 264 KB Output is correct
29 Correct 5 ms 468 KB Output is correct
30 Correct 7 ms 468 KB Output is correct
31 Correct 7 ms 468 KB Output is correct
32 Correct 4 ms 468 KB Output is correct
33 Correct 4 ms 468 KB Output is correct
34 Correct 4 ms 468 KB Output is correct
35 Correct 4 ms 468 KB Output is correct
36 Correct 6 ms 524 KB Output is correct
37 Correct 7 ms 528 KB Output is correct
38 Correct 4 ms 468 KB Output is correct
39 Correct 6 ms 504 KB Output is correct
40 Correct 4 ms 468 KB Output is correct
41 Correct 4 ms 468 KB Output is correct
42 Correct 4 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 296 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 264 KB Output is correct
29 Correct 5 ms 468 KB Output is correct
30 Correct 7 ms 468 KB Output is correct
31 Correct 7 ms 468 KB Output is correct
32 Correct 4 ms 468 KB Output is correct
33 Correct 4 ms 468 KB Output is correct
34 Correct 4 ms 468 KB Output is correct
35 Correct 4 ms 468 KB Output is correct
36 Correct 6 ms 524 KB Output is correct
37 Correct 7 ms 528 KB Output is correct
38 Correct 4 ms 468 KB Output is correct
39 Correct 6 ms 504 KB Output is correct
40 Correct 4 ms 468 KB Output is correct
41 Correct 4 ms 468 KB Output is correct
42 Correct 4 ms 468 KB Output is correct
43 Correct 95 ms 2388 KB Output is correct
44 Correct 158 ms 3976 KB Output is correct
45 Correct 165 ms 3936 KB Output is correct
46 Correct 84 ms 4064 KB Output is correct
47 Correct 93 ms 4060 KB Output is correct
48 Correct 103 ms 4076 KB Output is correct
49 Correct 127 ms 4072 KB Output is correct
50 Correct 167 ms 3844 KB Output is correct
51 Correct 168 ms 3936 KB Output is correct
52 Correct 99 ms 4068 KB Output is correct
53 Correct 110 ms 4064 KB Output is correct
54 Correct 170 ms 4000 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 296 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 264 KB Output is correct
29 Correct 5 ms 468 KB Output is correct
30 Correct 7 ms 468 KB Output is correct
31 Correct 7 ms 468 KB Output is correct
32 Correct 4 ms 468 KB Output is correct
33 Correct 4 ms 468 KB Output is correct
34 Correct 4 ms 468 KB Output is correct
35 Correct 4 ms 468 KB Output is correct
36 Correct 6 ms 524 KB Output is correct
37 Correct 7 ms 528 KB Output is correct
38 Correct 4 ms 468 KB Output is correct
39 Correct 6 ms 504 KB Output is correct
40 Correct 4 ms 468 KB Output is correct
41 Correct 4 ms 468 KB Output is correct
42 Correct 4 ms 468 KB Output is correct
43 Correct 95 ms 2388 KB Output is correct
44 Correct 158 ms 3976 KB Output is correct
45 Correct 165 ms 3936 KB Output is correct
46 Correct 84 ms 4064 KB Output is correct
47 Correct 93 ms 4060 KB Output is correct
48 Correct 103 ms 4076 KB Output is correct
49 Correct 127 ms 4072 KB Output is correct
50 Correct 167 ms 3844 KB Output is correct
51 Correct 168 ms 3936 KB Output is correct
52 Correct 99 ms 4068 KB Output is correct
53 Correct 110 ms 4064 KB Output is correct
54 Correct 170 ms 4000 KB Output is correct
55 Correct 1209 ms 23540 KB Output is correct
56 Correct 1574 ms 36944 KB Output is correct
57 Correct 1896 ms 36796 KB Output is correct
58 Correct 1000 ms 37488 KB Output is correct
59 Correct 1143 ms 37360 KB Output is correct
60 Correct 1235 ms 37420 KB Output is correct
61 Correct 1203 ms 37376 KB Output is correct
62 Correct 2044 ms 36828 KB Output is correct
63 Correct 2091 ms 37052 KB Output is correct
64 Correct 1259 ms 37216 KB Output is correct
65 Correct 1216 ms 37160 KB Output is correct
66 Correct 2094 ms 37032 KB Output is correct
67 Correct 1305 ms 37460 KB Output is correct