Submission #934508

# Submission time Handle Problem Language Result Execution time Memory
934508 2024-02-27T13:24:51 Z boris_mihov Chorus (JOI23_chorus) C++17
100 / 100
1661 ms 44052 KB
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>
#include <queue>

typedef long long llong;
const int MAXN = 1000000 + 10;
const llong INF = 1e18;

int n, k;
struct ConvexHullTrick
{
    struct Line
    {
        llong a, b;
        int cntPenalties;
        llong to;

        std::pair <llong,int> at(llong x)
        {
            return {x * a + b, cntPenalties};
        }
    };

    std::deque <Line> cht;
    llong intersect(Line x, Line y)
    {
        assert(x.a > y.a);
        assert(x.b < y.b);  
        return (y.b - x.b) / (x.a - y.a) - (((y.b - x.b) % (y.a - x.a) == 0) && x.cntPenalties < y.cntPenalties);
    }

    void push(Line l)
    {
        while (cht.size() && cht.back().at(cht.back().to) > l.at(cht.back().to))
        {
            cht.pop_back();
        }

        if (cht.empty()) cht.push_back(l);
        else if (cht.back().at(0) > l.at(0)) cht.push_back({l.a, l.b, l.cntPenalties, intersect(l, cht.back())});
    }
    
    std::pair <llong,int> query(llong x)
    { 
        assert(cht.size());
        while (cht.size() > 1 && x <= cht[1].to)
        {
            cht.pop_front();
        }

        return cht[0].at(x);
    }
};

int h[MAXN];
int next[MAXN];
llong prefix[MAXN];
std::pair <llong,int> dp[MAXN];
char s[2 * MAXN];

std::pair <llong,int> calc(llong penalty)
{
    dp[n + 1] = {0, 0};
    ConvexHullTrick cht;
    cht.push({-(n + 1), dp[n + 1].first + prefix[n], dp[n + 1].second, n});
    int ptr = n + 1;

    for (int pos = n ; pos >= 1 ; --pos)
    {
        dp[pos] = {INF, 0};
        if (pos < next[pos])
        {
            dp[pos] = dp[next[pos]];
            dp[pos].first += penalty;
            dp[pos].second++;
        }

        while (ptr - 1 > std::max(pos, next[pos]))
        {
            ptr--;
            cht.push({-ptr, prefix[ptr - 1] + dp[ptr].first, dp[ptr].second, n});
        }

        llong addedValue = 1LL * (next[pos] - pos + 1) * (pos - 1) + 1LL * pos * (pos - 1) - prefix[next[pos]] + penalty;
        std::pair <llong,int> res = cht.query(pos - 1);
        res.first += addedValue;
        res.second += 1;

        dp[pos] = std::min(dp[pos], res);
    }

    return dp[1];
}

void solve()
{
    int cntA = 0;
    int cntB = 0;
    for (int i = 1 ; i <= 2 * n ; ++i)
    {
        if (s[i] == 'B')
        {
            cntB++;
        } else
        {
            cntA++;
            h[cntA] = cntB;
        }
    }

    for (int i = 1 ; i <= n ; ++i)
    {
        prefix[i] = prefix[i - 1] + h[i];
    }

    next[n + 1] = n + 1;
    h[n + 1] = n + 1;
    for (int i = n ; i >= 1 ; --i)
    {
        next[i] = next[i + 1];
        while (next[i] >= i && h[next[i]] >= i)
        {
            next[i]--;
        }
    }

    llong l = -1, r = 1LL * n * n, mid;
    while (l < r - 1)
    {
        mid = (l + r) / 2;
        if (calc(mid).second > k) l = mid;
        else r = mid;
    }

    auto [fL, groupsL] = calc(l);
    auto [fR, groupsR] = calc(r); 
    fL -= groupsL * l;
    fR -= groupsR * r;

    if (groupsR == k)
    {
        std::cout << fR << '\n';
        return;
    }

    std::cout << fL + (fR - fL) * (groupsL - k) / (groupsL - groupsR) << '\n';
}

void input()
{
    std::cin >> n >> k;
    std::cin >> s + 1;
}

void fastIOI()
{
    std::ios_base :: sync_with_stdio(0);
    std::cout.tie(nullptr);
    std::cin.tie(nullptr);
}

int main()
{
    fastIOI();
    input();
    solve();

    return 0;
}

Compilation message

chorus.cpp: In function 'void input()':
chorus.cpp:155:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  155 |     std::cin >> s + 1;
      |                 ~~^~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6744 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Correct 1 ms 6744 KB Output is correct
7 Correct 1 ms 6492 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 2 ms 6492 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
15 Correct 1 ms 6492 KB Output is correct
16 Correct 1 ms 6492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6744 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Correct 1 ms 6744 KB Output is correct
7 Correct 1 ms 6492 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 2 ms 6492 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
15 Correct 1 ms 6492 KB Output is correct
16 Correct 1 ms 6492 KB Output is correct
17 Correct 1 ms 6492 KB Output is correct
18 Correct 1 ms 6488 KB Output is correct
19 Correct 2 ms 6488 KB Output is correct
20 Correct 1 ms 6492 KB Output is correct
21 Correct 1 ms 6492 KB Output is correct
22 Correct 1 ms 6492 KB Output is correct
23 Correct 1 ms 6492 KB Output is correct
24 Correct 1 ms 6492 KB Output is correct
25 Correct 1 ms 6492 KB Output is correct
26 Correct 1 ms 6492 KB Output is correct
27 Correct 2 ms 6492 KB Output is correct
28 Correct 1 ms 6492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6744 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Correct 1 ms 6744 KB Output is correct
7 Correct 1 ms 6492 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 2 ms 6492 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
15 Correct 1 ms 6492 KB Output is correct
16 Correct 1 ms 6492 KB Output is correct
17 Correct 1 ms 6492 KB Output is correct
18 Correct 1 ms 6488 KB Output is correct
19 Correct 2 ms 6488 KB Output is correct
20 Correct 1 ms 6492 KB Output is correct
21 Correct 1 ms 6492 KB Output is correct
22 Correct 1 ms 6492 KB Output is correct
23 Correct 1 ms 6492 KB Output is correct
24 Correct 1 ms 6492 KB Output is correct
25 Correct 1 ms 6492 KB Output is correct
26 Correct 1 ms 6492 KB Output is correct
27 Correct 2 ms 6492 KB Output is correct
28 Correct 1 ms 6492 KB Output is correct
29 Correct 5 ms 6492 KB Output is correct
30 Correct 6 ms 6720 KB Output is correct
31 Correct 6 ms 6492 KB Output is correct
32 Correct 3 ms 6492 KB Output is correct
33 Correct 4 ms 6492 KB Output is correct
34 Correct 4 ms 6492 KB Output is correct
35 Correct 4 ms 6492 KB Output is correct
36 Correct 6 ms 6492 KB Output is correct
37 Correct 6 ms 6492 KB Output is correct
38 Correct 4 ms 6492 KB Output is correct
39 Correct 5 ms 6492 KB Output is correct
40 Correct 5 ms 6492 KB Output is correct
41 Correct 4 ms 6668 KB Output is correct
42 Correct 4 ms 6492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6744 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Correct 1 ms 6744 KB Output is correct
7 Correct 1 ms 6492 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 2 ms 6492 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
15 Correct 1 ms 6492 KB Output is correct
16 Correct 1 ms 6492 KB Output is correct
17 Correct 1 ms 6492 KB Output is correct
18 Correct 1 ms 6488 KB Output is correct
19 Correct 2 ms 6488 KB Output is correct
20 Correct 1 ms 6492 KB Output is correct
21 Correct 1 ms 6492 KB Output is correct
22 Correct 1 ms 6492 KB Output is correct
23 Correct 1 ms 6492 KB Output is correct
24 Correct 1 ms 6492 KB Output is correct
25 Correct 1 ms 6492 KB Output is correct
26 Correct 1 ms 6492 KB Output is correct
27 Correct 2 ms 6492 KB Output is correct
28 Correct 1 ms 6492 KB Output is correct
29 Correct 5 ms 6492 KB Output is correct
30 Correct 6 ms 6720 KB Output is correct
31 Correct 6 ms 6492 KB Output is correct
32 Correct 3 ms 6492 KB Output is correct
33 Correct 4 ms 6492 KB Output is correct
34 Correct 4 ms 6492 KB Output is correct
35 Correct 4 ms 6492 KB Output is correct
36 Correct 6 ms 6492 KB Output is correct
37 Correct 6 ms 6492 KB Output is correct
38 Correct 4 ms 6492 KB Output is correct
39 Correct 5 ms 6492 KB Output is correct
40 Correct 5 ms 6492 KB Output is correct
41 Correct 4 ms 6668 KB Output is correct
42 Correct 4 ms 6492 KB Output is correct
43 Correct 74 ms 9668 KB Output is correct
44 Correct 126 ms 10500 KB Output is correct
45 Correct 128 ms 10488 KB Output is correct
46 Correct 54 ms 9816 KB Output is correct
47 Correct 70 ms 10524 KB Output is correct
48 Correct 97 ms 10496 KB Output is correct
49 Correct 89 ms 10500 KB Output is correct
50 Correct 152 ms 10500 KB Output is correct
51 Correct 139 ms 10496 KB Output is correct
52 Correct 92 ms 10500 KB Output is correct
53 Correct 87 ms 10496 KB Output is correct
54 Correct 134 ms 10508 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6744 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
6 Correct 1 ms 6744 KB Output is correct
7 Correct 1 ms 6492 KB Output is correct
8 Correct 1 ms 6492 KB Output is correct
9 Correct 1 ms 6492 KB Output is correct
10 Correct 2 ms 6492 KB Output is correct
11 Correct 1 ms 6492 KB Output is correct
12 Correct 1 ms 6492 KB Output is correct
13 Correct 1 ms 6492 KB Output is correct
14 Correct 1 ms 6492 KB Output is correct
15 Correct 1 ms 6492 KB Output is correct
16 Correct 1 ms 6492 KB Output is correct
17 Correct 1 ms 6492 KB Output is correct
18 Correct 1 ms 6488 KB Output is correct
19 Correct 2 ms 6488 KB Output is correct
20 Correct 1 ms 6492 KB Output is correct
21 Correct 1 ms 6492 KB Output is correct
22 Correct 1 ms 6492 KB Output is correct
23 Correct 1 ms 6492 KB Output is correct
24 Correct 1 ms 6492 KB Output is correct
25 Correct 1 ms 6492 KB Output is correct
26 Correct 1 ms 6492 KB Output is correct
27 Correct 2 ms 6492 KB Output is correct
28 Correct 1 ms 6492 KB Output is correct
29 Correct 5 ms 6492 KB Output is correct
30 Correct 6 ms 6720 KB Output is correct
31 Correct 6 ms 6492 KB Output is correct
32 Correct 3 ms 6492 KB Output is correct
33 Correct 4 ms 6492 KB Output is correct
34 Correct 4 ms 6492 KB Output is correct
35 Correct 4 ms 6492 KB Output is correct
36 Correct 6 ms 6492 KB Output is correct
37 Correct 6 ms 6492 KB Output is correct
38 Correct 4 ms 6492 KB Output is correct
39 Correct 5 ms 6492 KB Output is correct
40 Correct 5 ms 6492 KB Output is correct
41 Correct 4 ms 6668 KB Output is correct
42 Correct 4 ms 6492 KB Output is correct
43 Correct 74 ms 9668 KB Output is correct
44 Correct 126 ms 10500 KB Output is correct
45 Correct 128 ms 10488 KB Output is correct
46 Correct 54 ms 9816 KB Output is correct
47 Correct 70 ms 10524 KB Output is correct
48 Correct 97 ms 10496 KB Output is correct
49 Correct 89 ms 10500 KB Output is correct
50 Correct 152 ms 10500 KB Output is correct
51 Correct 139 ms 10496 KB Output is correct
52 Correct 92 ms 10500 KB Output is correct
53 Correct 87 ms 10496 KB Output is correct
54 Correct 134 ms 10508 KB Output is correct
55 Correct 958 ms 32764 KB Output is correct
56 Correct 1273 ms 43792 KB Output is correct
57 Correct 1554 ms 43792 KB Output is correct
58 Correct 622 ms 35664 KB Output is correct
59 Correct 1014 ms 43796 KB Output is correct
60 Correct 1046 ms 43804 KB Output is correct
61 Correct 1034 ms 43908 KB Output is correct
62 Correct 1545 ms 43808 KB Output is correct
63 Correct 1661 ms 43804 KB Output is correct
64 Correct 1107 ms 44052 KB Output is correct
65 Correct 1072 ms 43816 KB Output is correct
66 Correct 1620 ms 43792 KB Output is correct
67 Correct 1076 ms 43804 KB Output is correct