O - プレイリストの最大スコア / Maximum Score of a Playlist Editorial by admin
GLM 5.2 (High)Overview
We choose a music playlist as a subsequence, and calculate the total score as the sum of the product of compatibility values of consecutive songs and the individual scores of all chosen songs. The problem asks us to find the maximum possible total score.
Analysis
Let us consider solving this problem using DP (Dynamic Programming).
Let dp[i] be the maximum score at the point where the last chosen song is song \(i\).
If the song chosen immediately before song \(i\) is song \(j\) (\(j < i\)), the transition score from song \(j\) to song \(i\) is \(A_i + B_j \times B_i\). Therefore, dp[i] can be calculated using the following formula:
\[ \text{dp}[i] = A_i + \max(0, \max_{j < i} (\text{dp}[j] + B_j \times B_i)) \]
Here, max(0, ...) represents the case where song \(i\) is chosen as the first song of the playlist (i.e., there is no previous song).
If we calculate this DP transition naively, we need to iterate over all past \(j\) for each \(i\), resulting in a time complexity of \(O(N^2)\). Under the constraint \(N \leq 10^5\), this will lead to TLE (Time Limit Exceeded).
Therefore, let us rewrite and analyze the formula. Looking at the part \(\max_{j < i} (\text{dp}[j] + B_j \times B_i)\), we can see that this is equivalent to finding the maximum value at \(x = B_i\) for a set of lines of the form \(y = B_j \times x + \text{dp}[j]\).
In other words, if we can efficiently process “adding a line” and “querying the maximum value at a given \(x\)”, we can speed up this DP. The range of \(x\) is \(-10^6 \leq x \leq 10^6\) based on the constraints of \(B_i\).
Algorithm
In this problem, we use a data structure called the Li Chao Tree. A Li Chao Tree is a data structure that can perform line additions and minimum (or maximum) value queries in \(O(\log V)\) time each (where \(V\) is the coordinate range).
The steps of the algorithm are as follows:
1. Initialize a Li Chao Tree with the range of \(x\) from \(-10^6\) to \(10^6\).
2. As the initial state, add the line \(y = 0\) (slope \(0\), intercept \(0\)) representing the “choose nothing” state. This naturally handles the \(\max(0, \dots)\) in the calculation formula for dp[i].
3. For each song \(i\), query the maximum value at \(x = B_i\), and set dp[i] to the result plus \(A_i\).
4. If dp[i] is greater than the overall maximum value, update the answer.
5. For the subsequent songs, add the line \(y = B_i \times x + \text{dp}[i]\) to the Li Chao Tree.
Complexity
- Time Complexity: \(O(N \log V)\) We perform \(N\) queries and line additions, each taking \(O(\log V)\) time (where \(V \approx 2 \times 10^6\)).
- Space Complexity: \(O(V)\) Since the Li Chao Tree has a structure similar to a segment tree, the number of nodes is proportional to the coordinate range \(V\). This implementation uses an array of size approximately \(4 \times V\).
Implementation Details
Since \(B_i\) can take negative values, the coordinate space of the Li Chao Tree must also include negative regions, so we need to set
X_MIN = -10**6.When adding lines or querying, we traverse the tree recursively (or iteratively) using the midpoint of the interval
mid = (l + r) // 2. In this implementation, to prevent stack overflow, it is implemented iteratively (with loops).Unlike C++, recursion is slow in Python, so using iterative processing can reduce the execution time.
Source Code
import sys
def solve():
input = sys.stdin.readline
N = int(input())
A = [0] * N
B = [0] * N
for i in range(N):
a, b = map(int, input().split())
A[i] = a
B[i] = b
INF = 10**18
X_MIN = -10**6
X_MAX = 10**6 + 1
size = X_MAX - X_MIN
tree_m = [0] * (4 * size)
tree_b = [-INF] * (4 * size)
def add_line(m, b):
l = X_MIN
r = X_MAX
node = 1
while True:
cur_m = tree_m[node]
cur_b = tree_b[node]
mid = (l + r) // 2
if m * mid + b > cur_m * mid + cur_b:
tree_m[node], m = m, cur_m
tree_b[node], b = b, cur_b
if r - l == 1:
break
if m * l + b > tree_m[node] * l + tree_b[node]:
node = node * 2
r = mid
elif m * (r - 1) + b > tree_m[node] * (r - 1) + tree_b[node]:
node = node * 2 + 1
l = mid
else:
break
def query(x):
l = X_MIN
r = X_MAX
node = 1
res = -INF
while True:
val = tree_m[node] * x + tree_b[node]
if val > res:
res = val
if r - l == 1:
break
mid = (l + r) // 2
if x < mid:
node = node * 2
r = mid
else:
node = node * 2 + 1
l = mid
return res
# 初期状態として y = 0 (m=0, b=0) の直線を追加
add_line(0, 0)
ans = -INF
for i in range(N):
q = query(B[i])
dp = A[i] + q
if dp > ans:
ans = dp
add_line(B[i], dp)
print(ans)
if __name__ == '__main__':
solve()
This editorial was generated by or-glm5.2-high.