Official

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

Claude Opus 5 (High)

Overview

This problem asks us to choose exactly \(K\) days out of \(N\) days such that no two chosen days are adjacent, and maximize the total satisfaction score \(A_i\). It can be solved using a standard dynamic programming (DP) approach that processes the days from left to right.

Analysis

Why Naive Approaches Fail

  • Brute Force: There are \(\binom{N}{K}\) ways to choose \(K\) days from \(N\) days, which is far too large to finish in time for \(N=5000\).
  • Greedy Approach (taking the largest \(A_i\) first): This results in WA. Consider the counterexample:

$\(N=5,\ K=2,\ A=(1,5,6,5,1)\)$

If we greedily pick the maximum value \(A_3=6\), the \(2\)nd and \(4\)th days can no longer be chosen. For the remaining choice, we can only pick day \(1\) or day \(5\) with value \(1\), giving a total of \(7\). However, the actual optimal solution is to choose the \(2\)nd and \(4\)th days, yielding \(5+5=10\). In other words, choosing an element now affects future choices, so local greedy decisions do not lead to an optimal global solution.

How to Solve It

Consider making the decision to “choose / not choose day \(i\)” in order for \(i=1,2,\dots,N\). The only information required for subsequent decisions is:

  1. How far we have considered so far (\(i\))
  2. How many days have already been chosen (\(j\))
  3. Whether the previous day (day \(i\)) was chosen

Here, the condition “if day \(i\) is chosen, day \(i-1\) cannot be chosen” can be naturally expressed as “when choosing day \(i\), transition from the state up to day \(i-2\)”. Therefore, a two-dimensional state \((i, j)\) is sufficient, and the number of states is \(O(NK)\).

Algorithm

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

the maximum total satisfaction when considering days \(1\) through \(i\) and choosing exactly \(j\) days (such that no two are adjacent)

(or \(-\infty\) if it is impossible).

Base Cases $\(dp[0][0] = 0,\qquad dp[i][0] = 0\ (0 \le i \le N)\)$

Transitions (\(1 \le i \le N,\ 1 \le j \le K\))

  • If we do not go out on day \(i\): \(dp[i-1][j]\)
  • If we go out on day \(i\): since we cannot go out on day \(i-1\), we transition from the state up to day \(i-2\) where \(j-1\) days were chosen, giving \(dp[i-2][j-1] + A_i\) (for \(i=1\), instead of \(dp[-1][j-1]\), treat it as \(0\) if \(j-1=0\) and \(-\infty\) otherwise).

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

The answer is \(dp[N][K]\). From the problem statement, a valid selection is guaranteed to exist (\(K \le \lceil N/2 \rceil\)), so \(dp[N][K]\) will not be \(-\infty\).

Concrete Example: For \(N=5,\ K=2,\ A=(1,5,6,5,1)\): \(dp[i][1]\) is the maximum value among the first \(i\) days, which is \(1,5,6,6,6\). \(dp[i][2]\) is: - at \(i=3\): \(dp[1][1]+A_3=1+6=7\), - at \(i=4\): \(\max(7,\ dp[2][1]+A_4)=\max(7,5+5)=10\), - at \(i=5\): \(\max(10,\ dp[3][1]+A_5)=\max(10,7)=10\). Thus, the answer is \(10\), correctly finding the optimal solution that the greedy approach missed.

Complexity

  • Time Complexity: \(O(NK)\) (Since \(K \le \lceil N/2\rceil\), the worst-case is \(O(N^2) \approx 1.25\times 10^7\) operations, which runs fast enough).
  • Space Complexity: \(O(NK)\) (Can be reduced to \(O(K)\) as described below).

Implementation Notes

  • Integer Overflow: Since \(A_i \le 10^9\) and we can choose up to around \(2500\) days, the total can exceed \(10^{12}\). Use long long (64-bit integer).

  • Handling \(-\infty\): Using LLONG_MIN directly will cause an overflow/underflow when evaluating +A[i]. It is safer to use a sufficiently small value such as LLONG_MIN/4, and additionally check that the value is not \(-\infty\) before adding.

  • Index Boundary Cases: When \(i=1\), \(dp[i-2]\) does not exist, so case division is necessary. A straightforward approach is to make \(dp\) \(1\)-indexed, provide \(dp[0]\), and handle the “choose” transition for \(i=1\) as a special case (or shift the array indices by \(2\) to accommodate the equivalent of \(dp[-1]\)).

  • Memory Reduction: For \(N=5000,K=2500\), a \(2\)-dimensional array takes roughly \(10^8\) bytes, which is somewhat large. If you are concerned about memory, you can use a rolling array to keep only the last \(2\) rows for \(i\) (or update \(j\) in descending order), safely reducing the space complexity to \(O(K)\).

    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> A(N+1);
    for(int i=1;i<=N;i++) scanf("%lld", &A[i]);
    const long long NEG = LLONG_MIN/4;
    // dp[i][j]
    vector<vector<long long>> dp(N+1, vector<long long>(K+1, NEG));
    dp[0][0]=0;
    for(int i=1;i<=N;i++){
        dp[i][0]=0;
        for(int j=1;j<=K;j++){
            long long best = dp[i-1][j];
            long long prev = (i>=2 ? dp[i-2][j-1] : (j-1==0 ? 0 : NEG));
            if(prev > NEG){
                best = max(best, prev + A[i]);
            }
            dp[i][j]=best;
        }
    }
    printf("%lld\n", dp[N][K]);
    return 0;
}

This editorial was generated by claude5opus-high.