Official

D - 休暇の計画 / Vacation Plan Editorial by admin Editorial for printing

gpt-6-astra-medium

Overview

In this problem, we want to choose exactly \(K\) days out of \(N\) days such that no two chosen days are adjacent, maximizing the total satisfaction value. We can solve this using dynamic programming (DP), where the states represent “up to which day we have considered” and “how many days we have chosen”.

Analysis

Checking all combinations of outing days would result in an enormous number of candidates, which will not run in time for \(N \le 5000\).

Additionally, a greedy approach that picks days in descending order of satisfaction is incorrect. For example, if \(N=3,\ K=2\) and the satisfaction values are \((4,7,4)\), choosing day \(2\) (the highest satisfaction) prevents us from choosing any other day. The correct choice is to pick days \(1\) and \(3\), obtaining a total of \(8\).

Therefore, when choosing exactly \(j\) days from the first \(i\) days, we divide into cases based on whether we go out on day \(i\):

  • If we do not go out on day \(i\)
    We choose exactly \(j\) days from the first \(i-1\) days.
  • If we go out on day \(i\)
    Since we cannot go out on day \(i-1\), we choose exactly \(j-1\) days from the first \(i-2\) days, and add the satisfaction value of day \(i\).

In this way, we can compute the answer using the optimal solutions of shorter subproblems.

Algorithm

1. DP State

Define \(dp[i][j]\) as follows:

The maximum total satisfaction obtained by choosing exactly \(j\) days from day \(1\) through day \(i\) without selecting adjacent days.

States that correspond to impossible choices are set to \(-\infty\).

For the base cases, since no days have been considered yet:

  • \(dp[0][0]=0\)
  • \(dp[0][j]=-\infty\) for \(j \ge 1\)

Also, for convenience so that choosing day \(1\) can be handled with the same formula, we set \(dp[-1][0]=0\) and \(dp[-1][j]=-\infty\) for \(j \ge 1\).

2. Transitions

For \(j \ge 1\), we take the maximum of the following two options:

  • Do not go out on day \(i\): \(dp[i-1][j]\)
  • Go out on day \(i\): \(dp[i-2][j-1]+A_i\)

Therefore, the transition formula is:

\(dp[i][j]=\max\left(dp[i-1][j],\ dp[i-2][j-1]+A_i\right)\)

However, if \(dp[i-2][j-1]\) is an unreachable state, the latter candidate is not used.

Furthermore, the satisfaction when choosing \(0\) days is always \(0\), so \(dp[i][0]=0\).

These cases cover all valid choices exhaustively. Since going out on day \(i\) always excludes the previous day, the condition of not going out on consecutive days is also preserved.

3. Reusing Arrays

To compute \(dp[i]\), we only need \(dp[i-1]\) and \(dp[i-2]\). Therefore, instead of storing the entire DP table, the code uses the following three arrays:

  • prev2: \(dp[i-2]\)
  • prev1: \(dp[i-1]\)
  • cur: the current \(dp[i]\) being computed

For each day, we perform the following steps:

  1. Initialize cur with unreachable values, and set cur[0] = 0.
  2. Compute cur[j] according to the transition formula.
  3. Rotate/swap the arrays to prepare for computing the next day.

Note that within \(i\) days, the maximum number of non-adjacent days that can be chosen is \(\lceil i/2 \rceil\), so it is sufficient to iterate \(j\) from \(1\) to \(\min(K,\lceil i/2 \rceil)\).

After processing up to day \(N\), prev1[K], which corresponds to \(dp[N][K]\), is the answer.

Complexity

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

For each day, we compute at most \(K\) states. Since the input satisfaction values are processed on the fly, there is no need to store all \(N\) days of data.

Implementation Notes

  • Do not use \(0\) for unreachable states
    In this problem, we must choose “exactly \(K\) days”. If unreachable states are set to \(0\), transitions may incorrectly occur from choices that do not actually exist. In the code, a sufficiently small value NEG = -(1LL << 60) is used.

  • Do not add to NEG
    Check that prev2[j - 1] != NEG before adding the satisfaction value.

  • Use 64-bit integers
    The answer can be as large as \(2500 \times 10^9 = 2.5 \times 10^{12}\), which overflows a 32-bit integer. Use long long for the DP array.

  • Reinitialize each time even after rotating arrays
    cur will still contain results from previous computations. Reinitialize it with fill every time to avoid accidentally using stale values.

  • Handling the first day
    By setting prev2[0] = prev1[0] = 0, choosing day \(1\) can be handled without special branch conditions.

    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;

    constexpr long long NEG = -(1LL << 60);
    vector<long long> prev2(K + 1, NEG);
    vector<long long> prev1(K + 1, NEG);
    vector<long long> cur(K + 1, NEG);
    prev2[0] = prev1[0] = 0;

    for (int i = 1; i <= N; ++i) {
        long long a;
        cin >> a;

        fill(cur.begin(), cur.end(), NEG);
        cur[0] = 0;

        for (int j = 1; j <= min(K, (i + 1) / 2); ++j) {
            cur[j] = prev1[j];
            if (prev2[j - 1] != NEG) {
                cur[j] = max(cur[j], prev2[j - 1] + a);
            }
        }

        prev2.swap(prev1);
        prev1.swap(cur);
    }

    cout << prev1[K] << '\n';
    return 0;
}

This editorial was generated by gpt-6-astra-medium.

posted:
last update: