公式

E - 伐採の連鎖 / Chain of Felling 解説 by admin 印刷ページ

gpt-5.5-high

Overview

Once we decide the set of trees to cut down, the effect of the cutting order can be thought of as the “loss incurred when cutting down both adjacent trees.” By treating this loss as an edge cost, we can solve this problem using dynamic programming by processing from left to right.

Observations

First, cutting down a tree only affects its immediate neighbors.

Let’s consider the case where we cut down both tree \(i\) and tree \(i+1\).

  • If we cut down tree \(i\) first, the yield of tree \(i+1\) decreases by \(D_i\).
  • If we cut down tree \(i+1\) first, the yield of tree \(i\) decreases by \(D_{i+1}\).

In other words, the loss incurred from this adjacent pair can be minimized to

\[ \min(D_i, D_{i+1}) \]

by choosing the cutting order optimally.

The important point here is that this can be minimized independently for each adjacent pair.

Since the trees to be cut down are lined up in a straight line, their adjacency relations form a path (i.e., a graph without cycles). Therefore, even if we decide to “cut down the one with the smaller \(D\) first” for each adjacent pair, it does not cause any contradiction, and we can always achieve this order.

Therefore, when the set of trees to cut down \(S\) is fixed, the maximum profit is

\[ \sum_{i \in S} R_i - \sum_{\substack{i, i+1 \in S}} \min(D_i, D_{i+1}) \]

Thus, the problem can be rephrased as follows:

  • Choosing tree \(i\) gives a score of \(R_i\).
  • Choosing both adjacent trees \(i\) and \(i+1\) incurs a penalty of \(C_i = \min(D_i, D_{i+1})\).
  • Find the maximum score when choosing exactly \(K\) trees.

A naive search over all possible subsets and cutting orders of trees would be too slow due to the extremely large number of combinations and permutations. Instead, we can use dynamic programming (DP) by scanning from left to right, maintaining the state of “whether the previous tree was chosen.”

Algorithm

First, we precompute the adjacent penalties as

\[ C_i = \min(D_i, D_{i+1}) \]

We define the DP states as follows:

  • \(dp0[k]\): Maximum profit when choosing \(k\) trees from the prefix processed so far, without choosing the immediately preceding tree.
  • \(dp1[k]\): Maximum profit when choosing \(k\) trees from the prefix processed so far, including the immediately preceding tree.

When processing tree \(i\), we perform the following transitions:

Case 1: When tree \(i\) is not chosen

Since the state of the previous tree does not matter, we have:

\[ ndp0[k] = \max(dp0[k], dp1[k]) \]

Case 2: When tree \(i\) is chosen

Since we choose tree \(i\), the number of chosen trees increases by \(1\).

If the previous tree \(i-1\) was not chosen, no adjacency penalty is incurred:

\[ ndp1[k] = dp0[k-1] + R_i \]

If the previous tree \(i-1\) was chosen, we are choosing both tree \(i-1\) and tree \(i\), so the penalty \(C_{i-1}\) is incurred:

\[ ndp1[k] = dp1[k-1] + R_i - C_{i-1} \]

We repeat this for \(i=1\) to \(N\).

Finally, the maximum profit when choosing exactly \(K\) trees is

\[ \max(dp0[K], dp1[K]) \]

Complexity

  • Time Complexity: \(O(NK)\)
  • Space Complexity: \(O(K)\)

With the constraint \(N \times K \leq 2 \times 10^7\), an \(O(NK)\) DP is fast enough to pass within the time limit.

Key Implementation Details

  • Since the total profit or loss can be large, use long long (or 64-bit integers).

  • Initialize unreachable states with a sufficiently small value, such as -(1LL << 60).

  • Update by creating new arrays ndp0 and ndp1 at each step, and swap them after processing.

  • When \(i=1\), since there is no left neighbor, we do not apply the transition that subtracts the adjacency penalty.

    Source Code

#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N, K;
    cin >> N >> K;

    vector<long long> R(N + 1), D(N + 1);
    for (int i = 1; i <= N; i++) {
        cin >> R[i] >> D[i];
    }

    vector<long long> C(N + 1, 0);
    for (int i = 1; i < N; i++) {
        C[i] = min(D[i], D[i + 1]);
    }

    const long long NEG = -(1LL << 60);

    vector<long long> dp0(K + 1, NEG), dp1(K + 1, NEG);
    dp0[0] = 0;

    for (int i = 1; i <= N; i++) {
        vector<long long> ndp0(K + 1, NEG), ndp1(K + 1, NEG);

        for (int k = 0; k <= K; k++) {
            ndp0[k] = max(dp0[k], dp1[k]);

            if (k >= 1) {
                if (dp0[k - 1] != NEG) {
                    ndp1[k] = max(ndp1[k], dp0[k - 1] + R[i]);
                }
                if (i > 1 && dp1[k - 1] != NEG) {
                    ndp1[k] = max(ndp1[k], dp1[k - 1] + R[i] - C[i - 1]);
                }
            }
        }

        dp0.swap(ndp0);
        dp1.swap(ndp1);
    }

    cout << max(dp0[K], dp1[K]) << '\n';
    return 0;
}

This editorial was generated by gpt-5.5-high.

投稿日時:
最終更新: