公式

O - プレイリストの最大スコア / Maximum Score of a Playlist 解説 by admin

gpt-5.5-high

Overview

This problem asks us to find the maximum total score of a subsequence of songs.
We formulate a dynamic programming approach based on the “last chosen song” and accelerate the transition’s maximum value calculation using a Li Chao Tree.

Observation

We process the songs from left to right.

Let \(dp[i]\) be the “maximum total score of a subsequence that ends with song \(i\)”.

If we choose only song \(i\), the score is \(A_i\).

On the other hand, if we append song \(i\) to a subsequence that ended with song \(j\) (where \(j < i\)), the additional score obtained is

\(A_i + B_j \times B_i\).

Therefore,

\(dp[i] = A_i + \max\left(0, \max_{j < i}(dp[j] + B_j \times B_i)\right)\).

Here, \(\max(0, \cdots)\) represents the case where we start a new subsequence from song \(i\) without selecting any previous songs.

The answer is the maximum value of \(dp[i]\) over all \(i\).


Naively trying all \(j < i\) for each \(i\) would result in a time complexity of \(O(N^2)\).
Since \(N \leq 10^5\), this will exceed the time limit.

Thus, we focus on the transition expression:

\(dp[j] + B_j \times B_i\).

If we treat \(B_i\) as a variable \(x\), each \(j\) forms a linear function:

\(f_j(x) = B_j x + dp[j]\).

In other words, for each \(i\), what we need is to find the maximum value of

\(f_j(B_i)\)

among all the lines that have already been added.

This is a problem of processing “line additions” and “maximum value queries at a given point”, which can be computed efficiently using a Li Chao Tree.

Algorithm

  1. Collect all values of \(B_i\) and perform coordinate compression.

    • Since queries are only performed for \(x = B_i\), we do not need to cover the entire range of real numbers.
  2. Prepare a Li Chao Tree.

    • It supports adding a line \(y = mx + b\).
    • It supports querying the maximum value at a specified \(x\).
  3. Process the songs from \(1\) to \(N\) in order.

For song \(i\), query the maximum value of the lines

\(f_j(x) = B_j x + dp[j]\)

(created from already processed songs \(j < i\)) at \(x = B_i\).

Letting this maximum value be \(q\), we have:

\(dp[i] = A_i + \max(0, q)\).

After that, for future songs, add the line representing the case where song \(i\) is chosen last:

\(f_i(x) = B_i x + dp[i]\)

to the Li Chao Tree.

  1. Output the maximum value among all \(dp[i]\) as the answer.

For example, if song \(j\) has \(B_j = 3\) and \(dp[j] = 10\), the line created from this song is

\(y = 3x + 10\).

For a later song \(i\) with \(B_i = 5\), the value obtained when transitioning from this \(j\) would be

\(3 \times 5 + 10 = 25\).

The Li Chao Tree allows us to find the maximum of such values efficiently.

Complexity

  • Time Complexity: \(O(N \log N)\)
  • Space Complexity: \(O(N)\)

For each song, we perform a query and a line addition on the Li Chao Tree, each taking \(O(\log N)\) time.

Implementation Points

  • Since the values of \(dp[i]\) and \(B_j \times B_i\) can become very large, a data type of long long or larger is required for calculations.

    • In the code, __int128_t is used for safety.
  • Implement the Li Chao Tree to find the maximum value.

    • If you are adapting an implementation designed for finding minimum values, pay close attention to the direction of the inequality signs.
  • Querying when no lines have been added yet should return \(-\infty\).

    • Even in this case, by taking \(\max(0, q)\), we can correctly handle the case of starting the subsequence with song \(i\) alone.
  • Since the answer is the maximum score of a non-empty subsequence, initialize the answer to a sufficiently small value and update it with each \(dp[i]\).

    Source Code

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using i128 = __int128_t;

const i128 NEG_INF = -((i128)1 << 120);

struct Line {
    ll m;
    i128 b;
    Line(ll m_ = 0, i128 b_ = 0) : m(m_), b(b_) {}
};

struct LiChaoTree {
    int n;
    vector<ll> xs;
    vector<Line> seg;
    vector<char> has;

    LiChaoTree(const vector<ll>& coords) : xs(coords) {
        n = (int)xs.size();
        seg.assign(4 * n + 5, Line());
        has.assign(4 * n + 5, 0);
    }

    i128 eval(const Line& line, ll x) const {
        return (i128)line.m * x + line.b;
    }

    void add_line(Line nw, int v, int l, int r) {
        if (!has[v]) {
            seg[v] = nw;
            has[v] = 1;
            return;
        }

        int mid = (l + r) / 2;
        ll xl = xs[l];
        ll xm = xs[mid];

        bool left_better = eval(nw, xl) > eval(seg[v], xl);
        bool mid_better = eval(nw, xm) > eval(seg[v], xm);

        if (mid_better) {
            swap(seg[v], nw);
        }

        if (r - l == 1) return;

        if (left_better != mid_better) {
            add_line(nw, v * 2, l, mid);
        } else {
            add_line(nw, v * 2 + 1, mid, r);
        }
    }

    void add_line(ll m, i128 b) {
        add_line(Line(m, b), 1, 0, n);
    }

    i128 query_idx(int idx, int v, int l, int r) const {
        i128 res = NEG_INF;
        if (has[v]) res = eval(seg[v], xs[idx]);

        if (r - l == 1) return res;

        int mid = (l + r) / 2;
        i128 child;
        if (idx < mid) {
            child = query_idx(idx, v * 2, l, mid);
        } else {
            child = query_idx(idx, v * 2 + 1, mid, r);
        }
        return max(res, child);
    }

    i128 query(ll x) const {
        int idx = lower_bound(xs.begin(), xs.end(), x) - xs.begin();
        return query_idx(idx, 1, 0, n);
    }
};

void print_i128(i128 x) {
    if (x == 0) {
        cout << 0;
        return;
    }
    if (x < 0) {
        cout << '-';
        x = -x;
    }
    string s;
    while (x > 0) {
        s.push_back('0' + (int)(x % 10));
        x /= 10;
    }
    reverse(s.begin(), s.end());
    cout << s;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N;
    cin >> N;

    vector<ll> A(N), B(N), xs;
    xs.reserve(N);

    for (int i = 0; i < N; i++) {
        cin >> A[i] >> B[i];
        xs.push_back(B[i]);
    }

    sort(xs.begin(), xs.end());
    xs.erase(unique(xs.begin(), xs.end()), xs.end());

    LiChaoTree cht(xs);

    i128 ans = NEG_INF;

    for (int i = 0; i < N; i++) {
        i128 q = cht.query(B[i]);
        i128 best_prev = max((i128)0, q);
        i128 dp = (i128)A[i] + best_prev;

        ans = max(ans, dp);
        cht.add_line(B[i], dp);
    }

    print_i128(ans);
    cout << '\n';

    return 0;
}

This editorial was generated by gpt-5.5-high.