Submission #1037862

# Submission time Handle Problem Language Result Execution time Memory
1037862 2024-07-29T09:22:34 Z 정민찬(#10982) Chorus (JOI23_chorus) C++17
100 / 100
968 ms 132436 KB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pll;

struct Line{
    ll a, b;
    ll id;
    ll Get(ll x) { return a*x + b; }
    ld cross(Line other) {
        return (ld)(b - other.b) / (ld)(other.a - a);
    }
};

struct LineContainer{
    vector<Line> lines;
    ll idx;
    void init() {
        lines.clear();
        idx = 0;
    }
    void Push(Line p) {
        while (lines.size() >= 2 && lines[lines.size()-2].cross(lines.back()) >= lines.back().cross(p)) {
            lines.pop_back();
            if (idx >= lines.size()) idx --;
        }
        lines.push_back(p);
    }
    pll Query(ll x) {
        while (idx+1 < lines.size() && lines[idx+1].Get(x) <= lines[idx].Get(x)) {
            idx ++;
        }
        return {lines[idx].Get(x), lines[idx].id};
    }
};

ll N, k;
ll chk[2000010];
ll S[2000010], cnt[2000010];
ll dp[2000010];
ll prv[2000010];

LineContainer cht;

pll Test(ll X) {
    cht.init();
    cht.Push({-1, 0, 0});
    for (ll i=1; i<=N; i++) {
        pll ret = cht.Query(i);
        dp[i] = ret.first + S[i] + X;
        prv[i] = ret.second;
        cht.Push({-i-1, dp[i] - S[i] + cnt[i] * (i+1) - (S[cnt[i]] - S[i]), i});
    }
    ll i = N;
    ll cnt = 0;
    while (i != 0) {
        cnt ++;
        i = prv[i];
    }
    return {dp[N], cnt};
}

int main() {
    ios_base :: sync_with_stdio(false); cin.tie(NULL);
    cin >> N >> k;
    string s;
    cin >> s;
    vector<int> op;
    for (int i=0; i<2*N; i++) {
        if (s[i] == 'A') {
            op.push_back(i);
        }
    }
    reverse(op.begin(), op.end());
    ll pref = 0;
    ll add = 0;
    vector<ll> a = {0};
    for (int i=0; i<2*N; i++) {
        if (chk[i]) continue;
        if (pref == 0 && s[i] == 'B') {
            add += op.back() - (ll)a.size() + 1;
            a.push_back(1);
            chk[op.back()] = 1;
            op.pop_back();
            pref ++;
            i --;
            continue;
        }
        if (s[i] == 'A') {
            pref ++;
            a.push_back(1);
            op.pop_back();
        }
        else {
            pref --;
            a.push_back(0);
        }
    }
    vector<ll> li = {0}, ri = {0};
    for (ll i=1; i<=2*N; i++) {
        if (a[i]) li.push_back(i);
        else ri.push_back(i);
    }
    vector<ll> arr(N+1);
    arr[1] = 1;
    for (ll i=2; i<=N; i++) {
        arr[i] = arr[i-1];
        while (ri[arr[i]] < li[i]) arr[i] ++;
    }
    ll rp = 1;
    for (ll i=1; i<=N; i++) {
        while (rp+1 <= N && arr[rp+1] <= i) rp ++;
        cnt[i] = rp;
    }
    for (ll i=1; i<=N; i++) {
        S[i] += S[i-1] + arr[i];
    }
    ll lo = 0, hi = 1e12;
    ll ans = 0;
    while (lo <= hi) {
        ll mid = lo + hi >> 1;
        pll ret = Test(mid);
        ans = max(ans, ret.first - k * mid);
        if (ret.second == k) break;
        if (ret.second < k) hi = mid - 1;
        else lo = mid + 1;
    }

    cout << ans + add << '\n';
}

Compilation message

chorus.cpp: In member function 'void LineContainer::Push(Line)':
chorus.cpp:27:21: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<Line>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   27 |             if (idx >= lines.size()) idx --;
      |                 ~~~~^~~~~~~~~~~~~~~
chorus.cpp: In member function 'pll LineContainer::Query(ll)':
chorus.cpp:32:22: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<Line>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   32 |         while (idx+1 < lines.size() && lines[idx+1].Get(x) <= lines[idx].Get(x)) {
      |                ~~~~~~^~~~~~~~~~~~~~
chorus.cpp: In function 'int main()':
chorus.cpp:123:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  123 |         ll mid = lo + hi >> 1;
      |                  ~~~^~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4440 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4440 KB Output is correct
6 Correct 0 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 4444 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Correct 0 ms 4444 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 0 ms 4444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4440 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4440 KB Output is correct
6 Correct 0 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 4444 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Correct 0 ms 4444 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 0 ms 4444 KB Output is correct
17 Correct 1 ms 4444 KB Output is correct
18 Correct 1 ms 4444 KB Output is correct
19 Correct 1 ms 4440 KB Output is correct
20 Correct 1 ms 4444 KB Output is correct
21 Correct 1 ms 4444 KB Output is correct
22 Correct 1 ms 4444 KB Output is correct
23 Correct 1 ms 4444 KB Output is correct
24 Correct 1 ms 4444 KB Output is correct
25 Correct 1 ms 4444 KB Output is correct
26 Correct 1 ms 4444 KB Output is correct
27 Correct 1 ms 4444 KB Output is correct
28 Correct 1 ms 4444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4440 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4440 KB Output is correct
6 Correct 0 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 4444 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Correct 0 ms 4444 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 0 ms 4444 KB Output is correct
17 Correct 1 ms 4444 KB Output is correct
18 Correct 1 ms 4444 KB Output is correct
19 Correct 1 ms 4440 KB Output is correct
20 Correct 1 ms 4444 KB Output is correct
21 Correct 1 ms 4444 KB Output is correct
22 Correct 1 ms 4444 KB Output is correct
23 Correct 1 ms 4444 KB Output is correct
24 Correct 1 ms 4444 KB Output is correct
25 Correct 1 ms 4444 KB Output is correct
26 Correct 1 ms 4444 KB Output is correct
27 Correct 1 ms 4444 KB Output is correct
28 Correct 1 ms 4444 KB Output is correct
29 Correct 4 ms 5212 KB Output is correct
30 Correct 5 ms 4956 KB Output is correct
31 Correct 5 ms 4956 KB Output is correct
32 Correct 1 ms 4956 KB Output is correct
33 Correct 2 ms 4956 KB Output is correct
34 Correct 4 ms 5268 KB Output is correct
35 Correct 4 ms 5212 KB Output is correct
36 Correct 3 ms 4956 KB Output is correct
37 Correct 5 ms 4956 KB Output is correct
38 Correct 4 ms 5212 KB Output is correct
39 Correct 4 ms 4996 KB Output is correct
40 Correct 4 ms 4956 KB Output is correct
41 Correct 4 ms 5032 KB Output is correct
42 Correct 4 ms 4956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4440 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4440 KB Output is correct
6 Correct 0 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 4444 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Correct 0 ms 4444 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 0 ms 4444 KB Output is correct
17 Correct 1 ms 4444 KB Output is correct
18 Correct 1 ms 4444 KB Output is correct
19 Correct 1 ms 4440 KB Output is correct
20 Correct 1 ms 4444 KB Output is correct
21 Correct 1 ms 4444 KB Output is correct
22 Correct 1 ms 4444 KB Output is correct
23 Correct 1 ms 4444 KB Output is correct
24 Correct 1 ms 4444 KB Output is correct
25 Correct 1 ms 4444 KB Output is correct
26 Correct 1 ms 4444 KB Output is correct
27 Correct 1 ms 4444 KB Output is correct
28 Correct 1 ms 4444 KB Output is correct
29 Correct 4 ms 5212 KB Output is correct
30 Correct 5 ms 4956 KB Output is correct
31 Correct 5 ms 4956 KB Output is correct
32 Correct 1 ms 4956 KB Output is correct
33 Correct 2 ms 4956 KB Output is correct
34 Correct 4 ms 5268 KB Output is correct
35 Correct 4 ms 5212 KB Output is correct
36 Correct 3 ms 4956 KB Output is correct
37 Correct 5 ms 4956 KB Output is correct
38 Correct 4 ms 5212 KB Output is correct
39 Correct 4 ms 4996 KB Output is correct
40 Correct 4 ms 4956 KB Output is correct
41 Correct 4 ms 5032 KB Output is correct
42 Correct 4 ms 4956 KB Output is correct
43 Correct 56 ms 13580 KB Output is correct
44 Correct 98 ms 16848 KB Output is correct
45 Correct 92 ms 16596 KB Output is correct
46 Correct 7 ms 16600 KB Output is correct
47 Correct 20 ms 18856 KB Output is correct
48 Correct 62 ms 21704 KB Output is correct
49 Correct 61 ms 20944 KB Output is correct
50 Correct 38 ms 16424 KB Output is correct
51 Correct 90 ms 16500 KB Output is correct
52 Correct 61 ms 18468 KB Output is correct
53 Correct 62 ms 19664 KB Output is correct
54 Correct 90 ms 16596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4440 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4440 KB Output is correct
6 Correct 0 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 1 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 4444 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Correct 0 ms 4444 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 0 ms 4444 KB Output is correct
17 Correct 1 ms 4444 KB Output is correct
18 Correct 1 ms 4444 KB Output is correct
19 Correct 1 ms 4440 KB Output is correct
20 Correct 1 ms 4444 KB Output is correct
21 Correct 1 ms 4444 KB Output is correct
22 Correct 1 ms 4444 KB Output is correct
23 Correct 1 ms 4444 KB Output is correct
24 Correct 1 ms 4444 KB Output is correct
25 Correct 1 ms 4444 KB Output is correct
26 Correct 1 ms 4444 KB Output is correct
27 Correct 1 ms 4444 KB Output is correct
28 Correct 1 ms 4444 KB Output is correct
29 Correct 4 ms 5212 KB Output is correct
30 Correct 5 ms 4956 KB Output is correct
31 Correct 5 ms 4956 KB Output is correct
32 Correct 1 ms 4956 KB Output is correct
33 Correct 2 ms 4956 KB Output is correct
34 Correct 4 ms 5268 KB Output is correct
35 Correct 4 ms 5212 KB Output is correct
36 Correct 3 ms 4956 KB Output is correct
37 Correct 5 ms 4956 KB Output is correct
38 Correct 4 ms 5212 KB Output is correct
39 Correct 4 ms 4996 KB Output is correct
40 Correct 4 ms 4956 KB Output is correct
41 Correct 4 ms 5032 KB Output is correct
42 Correct 4 ms 4956 KB Output is correct
43 Correct 56 ms 13580 KB Output is correct
44 Correct 98 ms 16848 KB Output is correct
45 Correct 92 ms 16596 KB Output is correct
46 Correct 7 ms 16600 KB Output is correct
47 Correct 20 ms 18856 KB Output is correct
48 Correct 62 ms 21704 KB Output is correct
49 Correct 61 ms 20944 KB Output is correct
50 Correct 38 ms 16424 KB Output is correct
51 Correct 90 ms 16500 KB Output is correct
52 Correct 61 ms 18468 KB Output is correct
53 Correct 62 ms 19664 KB Output is correct
54 Correct 90 ms 16596 KB Output is correct
55 Correct 645 ms 84572 KB Output is correct
56 Correct 721 ms 132436 KB Output is correct
57 Correct 968 ms 107608 KB Output is correct
58 Correct 57 ms 113140 KB Output is correct
59 Correct 71 ms 116060 KB Output is correct
60 Correct 576 ms 129428 KB Output is correct
61 Correct 547 ms 129944 KB Output is correct
62 Correct 281 ms 106336 KB Output is correct
63 Correct 664 ms 107048 KB Output is correct
64 Correct 545 ms 120916 KB Output is correct
65 Correct 556 ms 121176 KB Output is correct
66 Correct 959 ms 108904 KB Output is correct
67 Correct 572 ms 121400 KB Output is correct