E - 伐採の連鎖 / Chain of Felling Editorial by admin Editorial for printing
claude4.8opus-highSummary
This problem asks us to choose and cut down exactly \(K\) trees out of \(N\) trees to maximize the total profit. The key is that by carefully choosing the “order” of cutting, we can minimize the reduction in harvest (penalty) between adjacent trees.
Analysis
Formulating the effect of order as a “penalty”
First, let’s consider how the profit is determined when the set of cut trees \(S\) is fixed.
When tree \(i\) is cut down, the harvest of its “not yet cut adjacent trees” decreases by \(D_i\). What is important here is that the reduction in harvest only matters for pairs of adjacent trees where both trees are chosen to be cut.
- If only one of the adjacent trees is in \(S\), since the tree receiving the reduction is not cut anyway, it does not affect the profit.
- If we consider an adjacent pair \((i, i+1)\) where both are in \(S\), the harvest of the tree cut later decreases by the \(D\) value of the tree cut earlier.
- Cut tree \(i\) first \(\to\) penalty \(D_i\)
- Cut tree \(i+1\) first \(\to\) penalty \(D_{i+1}\)
In other words, the penalty for this pair can be made \(\min(D_i, D_{i+1})\) by “choosing which tree to cut first”.
Can we minimize it for all pairs simultaneously?
Since the trees are arranged in a line (a path), even if we impose the order constraint “cut the one with the smaller \(D\) first” for each adjacent pair, these constraints will never form a cycle (since a path has no cycles). Therefore, by choosing an appropriate cutting order, we can achieve \(\min(D_i, D_{i+1})\) for all adjacent pairs simultaneously.
Reformulating the Problem
From the above, the answer we want to find is as follows:
\[ \max_{|S| = K} \left( \sum_{i \in S} R_i - \sum_{\substack{(i, i+1) \\ i, i+1 \in S}} \min(D_i, D_{i+1}) \right) \]
Concrete Example: When \(N=3, K=2\), \(R=[5,3,4]\), and \(D=[2,10,1]\): - \(S=\{1,3\}\) (not adjacent): \(5+4 = 9\) - \(S=\{1,2\}\): \(5+3-\min(2,10) = 6\) - \(S=\{2,3\}\): \(3+4-\min(10,1) = 6\)
Thus, the maximum is \(9\).
Issues with the Naive Approach
Trying all subsets and orders would take exponential time, which is far too slow for \(N \le 10^5\). However, from the reformulation above, we can see the structure where “a penalty occurs only when adjacent trees are chosen”. Thus, we can solve this using dynamic programming (DP) by making decisions from left to right.
Algorithm
We process the trees from left to right and perform DP with the following states:
- \(p0[j]\): Current tree is not chosen, having chosen \(j\) trees so far.
- \(p1[j]\): Current tree is chosen, having chosen \(j\) trees so far.
The transitions to tree \(i\) are as follows:
When tree \(i\) is not chosen (\(c0[j]\))
Since no penalty occurs regardless of whether the previous tree was chosen or not:
\[ c0[j] = \max(p0[j],\ p1[j]) \]
When tree \(i\) is chosen (\(c1[j]\))
When \(j \ge 1\), we add \(R_i\). We divide into cases based on whether the previous tree \(i-1\) was chosen:
- If \(i-1\) was not chosen: \(p0[j-1] + R_i\)
- If \(i-1\) was chosen: They form an adjacent pair, so a penalty occurs: \(p1[j-1] - \min(D_{i-1}, D_i) + R_i\)
We take the maximum of these two values.
\[ c1[j] = \max\bigl(p0[j-1],\ p1[j-1] - \min(D_{i-1}, D_i)\bigr) + R_i \]
The final answer is \(\max(p0[K],\ p1[K])\).
Complexity
- Time Complexity: \(O(N \times K)\) (This fits within the time limit since the constraints guarantee \(N \times K \le 2 \times 10^7\))
- Space Complexity: \(O(K)\) (We only need to maintain the DP table of the previous tree)
Implementation Details
Handling “unreachable states”: In the initial state, only \(p0[0]=0\) is valid, and all other states are unreachable. We represent these unreachable states with a sufficiently small value
NEG(e.g.,LLONG_MIN/4). To prevent overflow from adding twoNEGvalues during transitions, we check if a state is valid (e.g., checking if the value is greater thanNEG/2) before performing calculations.Reusing arrays: For each tree, we create new arrays
c0andc1, and thenswapthem withp0andp1after processing. This keeps the space complexity to \(O(K)\).Note that the harvest amount can be negative, but since the DP simply takes the maximum, no special handling is required. (Trees that cause a net loss will naturally not be chosen. However, since we are constrained to choose exactly \(K\) trees, if we are forced to choose a tree even if it leads to a loss, the DP will still correctly calculate the maximum possible value).
Source Code
#include <bits/stdc++.h>
using namespace std;
int main(){
int N, K;
if(scanf("%d %d",&N,&K)!=2) return 0;
vector<long long> R(N+1), D(N+1);
for(int i=1;i<=N;i++) scanf("%lld %lld",&R[i],&D[i]);
const long long NEG = LLONG_MIN/4;
vector<long long> p0(K+1, NEG), p1(K+1, NEG);
p0[0]=0;
vector<long long> c0(K+1), c1(K+1);
for(int i=1;i<=N;i++){
for(int j=0;j<=K;j++){
c0[j] = max(p0[j], p1[j]);
if(j>=1){
long long best = p0[j-1];
if(i>=2){
long long pen = p1[j-1];
if(pen>NEG/2) pen -= min(D[i-1],D[i]);
best = max(best, pen);
}
if(best>NEG/2) c1[j]=best+R[i];
else c1[j]=NEG;
} else {
c1[j]=NEG;
}
}
swap(p0,c0);
swap(p1,c1);
}
long long ans = max(p0[K], p1[K]);
printf("%lld\n", ans);
return 0;
}
This editorial was generated by claude4.8opus-high.
posted:
last update: