Official

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

gemini-3.5-flash-high

Overview

This problem asks us to maximize the total profit obtained by choosing and cutting down exactly \(K\) trees from \(N\) trees lined up in a row. Since cutting down adjacent trees results in a penalty (reduction in harvest due to sunlight impact), we optimize the cutting order and use Dynamic Programming (DP) to find the maximum profit.

Analysis

1. Optimal Order When Cutting Two Trees

Consider the case where we cut down both of two adjacent trees \(i\) and \(i+1\). There are two possible orders to cut them down:

  1. Cut down tree \(i\) first, then tree \(i+1\)

    • When tree \(i\) is cut down, tree \(i+1\) is still standing, so the harvest of tree \(i+1\) decreases by \(D_i\).
    • The resulting profit is \(R_i + (R_{i+1} - D_i) = R_i + R_{i+1} - D_i\).
  2. Cut down tree \(i+1\) first, then tree \(i\)

    • When tree \(i+1\) is cut down, tree \(i\) is still standing, so the harvest of tree \(i\) decreases by \(D_{i+1}\).
    • The resulting profit is \(R_{i+1} + (R_i - D_{i+1}) = R_i + R_{i+1} - D_{i+1}\).

Since Takahashi wants to maximize the profit, he can freely choose the order that results in the smaller reduction (penalty). Therefore, by choosing the optimal order, the penalty when cutting down both adjacent trees \(i\) and \(i+1\) can be minimized to \(\min(D_i, D_{i+1})\).

2. Optimal Order When Cutting Three or More Trees

Even when cutting down three or more consecutive trees, if we determine the order for each adjacent pair by the rule “cut down the one with the smaller sunlight impact value \(D\) first, and the one with the larger value later”, there always exists a cutting order that minimizes the penalty to \(\min(D_x, D_{x+1})\) for all adjacent pairs \((x, x+1)\). (This is guaranteed because there are no cycles in the dependencies, allowing for a topological sort).

Therefore, this problem can be reformulated as follows:

“Choose exactly \(K\) trees to cut down from \(N\) trees. For each pair of adjacent chosen trees \((i, i+1)\), we pay a penalty of \(\min(D_i, D_{i+1})\). Find the maximum profit.”

With this reformulation, we no longer need to worry about the “order” of cutting. We can apply Dynamic Programming (DP) to decide whether to “cut or not” sequentially from left to right.

Algorithm

We process the trees from left to right and design a DP with the following states.

DP Definition

  • dp[i][j][state]: The maximum profit after considering up to the \(i\)-th tree (0-indexed), having cut down \(j\) trees so far, with the state of tree \(i\) being state.
    • state = 0: Tree \(i\) is not cut down
    • state = 1: Tree \(i\) is cut down

Transitions

When processing tree \(i\), we transition from the state of the previous tree \(i-1\) as follows:

  1. If tree \(i\) is not cut down (state = 0) No penalty occurs, regardless of whether the previous tree \(i-1\) was cut down or not. $\(dp[i][j][0] = \max(dp[i-1][j][0],\ dp[i-1][j][1])\)$

  2. If tree \(i\) is cut down (state = 1)

    • If the previous tree \(i-1\) was not cut down: Since the adjacent tree is not cut down, no penalty occurs. $\(dp[i][j][1] \leftarrow dp[i-1][j-1][0] + R_i\)$
    • If the previous tree \(i-1\) was also cut down: Since both adjacent trees are cut down, a penalty of \(\min(D_{i-1}, D_i)\) is incurred. $\(dp[i][j][1] \leftarrow dp[i-1][j-1][1] + R_i - \min(D_{i-1}, D_i)\)$
    • We take the maximum value of these two transitions.

Memory Optimization (Rolling Array)

When calculating the transitions for the \(i\)-th tree, only the results from the previous \((i-1)\)-th tree are needed. Therefore, by reusing the first dimension of the DP table (the tree position \(i\)) with an array of size 2 (alternating between cur and next), we can reduce the space complexity from \(O(NK)\) to \(O(K)\).

Complexity

  • Time Complexity: \(O(NK)\) The number of states is \(N \times K \times 2\), and the transition from each state takes \(O(1)\) time. Since the constraints guarantee \(N \times K \leq 2 \times 10^7\), this runs well within the time limit.
  • Space Complexity: \(O(K)\) By applying a rolling array, the required memory is only for \(2 \times (K+1) \times 2\) elements, which is extremely memory-efficient.

Implementation Points

  • Initialization: Since this is a DP to find the maximum value, unreachable states (for example, having cut down 1 tree before considering any trees) should be initialized with an extremely small value (\(-\infty\)).

  • Boundary Conditions: When processing tree \(0\) or in cases where the previous tree does not exist, we use conditional branching to avoid applying a penalty. In the code, this is handled as (i > 0) ? min(D[i - 1], D[i]) : 0.

    Source Code

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

using namespace std;

const long long INF = 1e18;

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), D(N);
    for (int i = 0; i < N; ++i) {
        cin >> R[i] >> D[i];
    }

    // dp[2][K + 1][2]
    // dp[cur][j][state] : i本目まで見て、j本伐採したときの最大利益
    // state = 0 : i本目を伐採していない
    // state = 1 : i本目を伐採した
    vector<vector<vector<long long>>> dp(2, vector<vector<long long>>(K + 1, vector<long long>(2, -INF)));

    dp[0][0][0] = 0;

    int cur = 0, next = 1;
    for (int i = 0; i < N; ++i) {
        for (int j = 0; j <= K; ++j) {
            dp[next][j][0] = -INF;
            dp[next][j][1] = -INF;
        }

        for (int j = 0; j <= K; ++j) {
            // state 0 からの遷移
            if (dp[cur][j][0] != -INF) {
                // i を伐採しない
                dp[next][j][0] = max(dp[next][j][0], dp[cur][j][0]);
                // i を伐採する
                if (j + 1 <= K) {
                    dp[next][j + 1][1] = max(dp[next][j + 1][1], dp[cur][j][0] + R[i]);
                }
            }
            // state 1 からの遷移
            if (dp[cur][j][1] != -INF) {
                // i を伐採しない
                dp[next][j][0] = max(dp[next][j][0], dp[cur][j][1]);
                // i を伐採する
                if (j + 1 <= K) {
                    long long penalty = (i > 0) ? min(D[i - 1], D[i]) : 0;
                    dp[next][j + 1][1] = max(dp[next][j + 1][1], dp[cur][j][1] + R[i] - penalty);
                }
            }
        }
        swap(cur, next);
    }

    long long ans = max(dp[cur][K][0], dp[cur][K][1]);
    cout << ans << "\n";

    return 0;
}

This editorial was generated by gemini-3.5-flash-high.

posted:
last update: