E - 伐採の連鎖 / Chain of Felling Editorial by admin Editorial for printing
gpt-5.5-highOverview
This problem asks us to maximize the total profit when choosing and cutting exactly \(K\) trees, including the decision of the cutting order.
The key point is that when cutting both of two adjacent trees, the loss can be minimized to \(\min(D_i, D_{i+1})\) by choosing the cutting order optimally.
Analysis
First, let’s consider the case where we cut both tree \(i\) and tree \(i+1\).
- If we cut tree \(i\) first, the yield of tree \(i+1\) decreases by \(D_i\).
- If we cut tree \(i+1\) first, the yield of tree \(i\) decreases by \(D_{i+1}\).
Therefore, by choosing the order, the loss from this adjacent pair can be made:
\[ \min(D_i, D_{i+1}) \]
For example, if \(D_i = 3\) and \(D_{i+1} = 10\), cutting tree \(i\) first keeps the loss at \(3\).
But can we choose the minimum value independently for each adjacent pair even when there are multiple adjacent pairs?
The adjacency relationships of the chosen trees form a subgraph on a line, which decomposes into several paths. Since paths do not contain cycles, defining the direction for each edge as “cut the one with the smaller \(D\) first” will not produce any contradictory order.
Therefore, the maximum profit for a chosen set of trees \(S\) can be expressed as:
\[ \sum_{i \in S} R_i - \sum_{\substack{i, i+1 \in S}} \min(D_i, D_{i+1}) \]
In other words, the problem of the cutting order disappears, and it reduces to:
- Choosing tree \(i\) yields \(R_i\).
- Choosing both of two adjacent trees incurs a penalty of \(\min(D_i, D_{i+1})\).
A naive search over all ways to choose the trees would take \(\binom{N}{K}\) possibilities, and considering the order would make it even larger, which is impossible.
Therefore, we use dynamic programming, processing the trees from left to right.
Algorithm
We process the trees from left to right.
Define \(dp0[j]\) as follows:
- The maximum profit when choosing \(j\) trees from the prefix processed so far, such that the immediately preceding tree is not chosen.
Define \(dp1[j]\) as follows:
- The maximum profit when choosing \(j\) trees from the prefix processed so far, such that the immediately preceding tree is chosen.
When processing tree \(i\), we consider the following transitions:
Case where tree \(i\) is not chosen
Since the immediately preceding tree may or may not be chosen, we have:
\[ new0[j] = \max(dp0[j], dp1[j]) \]
Case where tree \(i\) is chosen
If tree \(i-1\) is not chosen, there is no adjacency penalty:
\[ dp0[j-1] + R_i \]
If tree \(i-1\) is chosen, a penalty for the adjacent pair \((i-1, i)\) is incurred:
\[ dp1[j-1] + R_i - \min(D_{i-1}, D_i) \]
Therefore,
\[ new1[j] = \max( dp0[j-1] + R_i, dp1[j-1] + R_i - \min(D_{i-1}, D_i) ) \]
The initial state is:
\[ dp0[0] = 0 \]
and all other states are initialized to a very small value.
Finally, since we must choose exactly \(K\) trees, the answer is:
\[ \max(dp0[K], dp1[K]) \]
Complexity
- Time Complexity: \(O(NK)\)
- Space Complexity: \(O(K)\)
Since the constraints guarantee \(N \times K \leq 2 \times 10^7\), this complexity is well within the time limit.
Implementation Details
Instead of creating a new DP array for each \(i\), we can reuse the two arrays \(dp0\) and \(dp1\).
However, when updating the same array in-place, if we update \(j\) in ascending order, we risk using states where the same tree is chosen multiple times.
Therefore, in the code, we update \(j\) in descending order (from largest to smallest) like this:
j = upper
while j:
...
j -= 1
Also, since the answer can be negative, we use a sufficiently small value (e.g., \(-10^{30}\)) for unreachable states.
As a special case:
When \(K = 1\), no adjacency penalty can occur, so the answer is \(\max R_i\).
When \(K = N\), we choose all trees, so we can directly compute:
$\(\sum R_i - \sum_{i=1}^{N-1} \min(D_i, D_{i+1})\)$Source Code
import sys
def main():
data = list(map(int, sys.stdin.buffer.read().split()))
N = data[0]
K = data[1]
if K == 1:
ans = -10**30
idx = 2
for _ in range(N):
r = data[idx]
if r > ans:
ans = r
idx += 2
print(ans)
return
if K == N:
total = 0
penalty = 0
idx = 2
prev_d = 0
for i in range(N):
r = data[idx]
d = data[idx + 1]
total += r
if i:
penalty += prev_d if prev_d < d else d
prev_d = d
idx += 2
print(total - penalty)
return
NEG = -10**30
dp0 = [NEG] * (K + 1)
dp1 = [NEG] * (K + 1)
dp0[0] = 0
idx = 2
prev_d = 0
kk = K
for i in range(1, N + 1):
r = data[idx]
d = data[idx + 1]
idx += 2
w = prev_d if prev_d < d else d
rw = r - w
upper = i if i < kk else kk
j = upper
while j:
old1 = dp1[j]
if old1 > dp0[j]:
dp0[j] = old1
v = dp0[j - 1] + r
t = dp1[j - 1] + rw
if t > v:
v = t
dp1[j] = v
j -= 1
prev_d = d
ans = dp0[K]
if dp1[K] > ans:
ans = dp1[K]
print(ans)
if __name__ == "__main__":
main()
This editorial was generated by gpt-5.5-high.
posted:
last update: