Official

E - 伐採の連鎖 / Chain of Felling Editorial by admin Editorial for printing

GLM 5.2 (High)

Overview

This problem asks us to find the maximum total profit obtained by choosing and cutting down exactly \(K\) trees out of \(N\) trees. When a tree is cut down, the yield of adjacent uncut trees decreases. However, by optimizing the order of cutting, we can reduce this problem to a simple dynamic programming (DP) approach.

Observation

A naive approach that tries all combinations of “which \(K\) trees to choose” and “in what order to cut them” would result in TLE (Time Limit Exceeded) due to the enormous number of combinations.

Here, the key observation is to consider only the cases where two adjacent trees are both cut down. When we cut down both tree \(i\) and tree \(i+1\), the loss depends on which one is cut down first: - If tree \(i\) is cut down first, the yield of tree \(i+1\) decreases by \(D_i\). - If tree \(i+1\) is cut down first, the yield of tree \(i\) decreases by \(D_{i+1}\).

To maximize the profit, we need to minimize the loss. Thus, if we can freely decide the order of cutting these two trees, we should choose the one that results in the smaller reduction. In other words, the loss (penalty) incurred when cutting down both of two adjacent trees is fixed at \(\min(D_i, D_{i+1})\). On the other hand, if we cut down trees that are not adjacent, no penalty occurs because they do not affect each other’s sunlight.

From the above, this problem can be reformulated as: “Given \(N\) elements arranged on a line, choose \(K\) elements to maximize the sum of their yields \(R_i\) minus the sum of the penalties \(\min(D_i, D_{i+1})\) for each pair of adjacent chosen elements.”

Algorithm

Based on the observation above, we can use dynamic programming (DP).

DP State Definition: - dp0[j]: The maximum profit when we have cut down \(j\) trees so far, and the immediately preceding tree was not cut down. - dp1[j]: The maximum profit when we have cut down \(j\) trees so far, and the immediately preceding tree was cut down.

The transitions for tree \(i\) are as follows: 1. If tree \(i\) is not cut down: No penalty occurs regardless of whether the preceding tree \(i-1\) was cut down or not. next_dp0[j] = max(dp0[j], dp1[j])

  1. If tree \(i\) is cut down: Since it is cut down, the yield \(R_i\) is always added.
    • If transitioning from the state where the preceding tree \(i-1\) was not cut down (dp0[j-1]), no penalty occurs.
    • If transitioning from the state where the preceding tree \(i-1\) was cut down (dp1[j-1]), we cut down two adjacent trees, so we subtract \(\min(D_{i-1}, D_i)\) as a penalty. next_dp1[j] = max(dp0[j-1], dp1[j-1] - min(D_{i-1}, D_i)) + R[i]

We update these states from \(i = 1\) to \(N\), and the final answer will be \(\max(dp0[K], dp1[K])\).

Complexity

  • Time Complexity: \(O(NK)\)
  • Space Complexity: \(O(K)\) (Since we only need two arrays to store the state of the immediately preceding step, it does not depend on \(N\))

Key Implementation Points

  • Given the constraints, \(N \times K \leq 2 \times 10^7\), so the \(O(NK)\) DP is well within the time limit. By reusing the arrays, we can also reduce the space complexity to \(O(K)\).

  • For the initial DP values, impossible states should be set to a sufficiently small value (negative infinity, e.g., \(-10^{18}\)).

  • When \(j=0\) (no trees have been cut down yet), the transition next_dp1[0] representing cutting down a tree is impossible, so it must be set to \(-\infty\).

  • Note that for the first tree \(i=1\), the penalty for adjacent pairs \(\min(D_{i-1}, D_i)\) should not be calculated (treated as penalty 0) because there is no adjacent preceding tree.

    Source Code

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    
    int N, K;
    if (!(cin >> N >> K)) return 0;
    
    vector<long long> R(N + 1), D(N + 1);
    for (int i = 1; i <= N; ++i) {
        cin >> R[i] >> D[i];
    }
    
    const long long INF = 1e18;
    vector<long long> dp_prev_0(K + 1, -INF);
    vector<long long> dp_prev_1(K + 1, -INF);
    vector<long long> dp_curr_0(K + 1, -INF);
    vector<long long> dp_curr_1(K + 1, -INF);
    
    dp_prev_0[0] = 0;
    
    for (int i = 1; i <= N; ++i) {
        int max_j = min(i, K);
        for (int j = 0; j <= max_j; ++j) {
            dp_curr_0[j] = max(dp_prev_0[j], dp_prev_1[j]);
            if (j > 0) {
                long long v0 = dp_prev_0[j-1];
                long long v1 = -INF;
                if (i > 1) {
                    v1 = dp_prev_1[j-1] - min(D[i-1], D[i]);
                }
                dp_curr_1[j] = max(v0, v1) + R[i];
            } else {
                dp_curr_1[j] = -INF;
            }
        }
        swap(dp_prev_0, dp_curr_0);
        swap(dp_prev_1, dp_curr_1);
    }
    
    cout << max(dp_prev_0[K], dp_prev_1[K]) << endl;
    
    return 0;
}

This editorial was generated by or-glm5.2-high.

posted:
last update: