公式

D - 休暇の計画 / Vacation Plan 解説 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, maximizing the sum of satisfaction values. We solve this using interval DP (more precisely, a DP with states: day \(\times\) number of chosen days \(\times\) whether the previous day was chosen).

Analysis

Issues with the Naive Approach

The first idea that comes to mind might be to “greedily pick days with larger \(A_i\)”, but this fails.

Example: \(N=3, K=2, A=(3,4,3)\) - If we greedily choose the maximum value \(A_2=4\), both day \(1\) and day \(3\) become unusable, leaving only day \(2\) selected and failing to satisfy \(K=2\). - The correct answer is to choose day \(1\) and day \(3\) for a total of \(3+3=6\).

Because “taking a large value makes adjacent days unusable”, local decisions cannot guarantee optimality.

Furthermore, brute-forcing all possible selections is impossible because the number of combinations grows exponentially (\(\binom{N-K+1}{K}\) possibilities).

Key Observation

When processing the days in order from day \(1\), the only information needed to decide whether to choose day \(i\) consists of the following two pieces:

  1. How many days have been chosen so far (we need to track this count because we want exactly \(K\) days)
  2. Whether the previous day (day \(i-1\)) was chosen (required to check the non-adjacency constraint)

How choices were made on day \(i-2\) and earlier has no impact on whether day \(i\) can be chosen. Therefore, the number of states is bounded by \(O(N \times K \times 2)\), allowing us to use dynamic programming.

Algorithm

Consider the following DP. After processing up to day \(i\):

  • \(\mathrm{take}[j]\): the maximum total satisfaction when day \(i\) is chosen, with a total of \(j\) days chosen
  • \(\mathrm{notake}[j]\): the maximum total satisfaction when day \(i\) is not chosen, with a total of \(j\) days chosen

(Unreachable states are set to \(-\infty\) (a sufficiently small value).)

Initial state (\(i=0\), before considering any days): $\(\mathrm{notake}[0]=0,\quad \text{and } -\infty \text{ otherwise}\)$

Transitions (let the value on day \(i\) be \(a=A_i\)):

  • If day \(i\) is chosen, day \(i-1\) must not have been chosen, so: $\(\mathrm{take}_{\text{new}}[j] = \mathrm{notake}_{\text{old}}[j-1] + a \qquad (1 \le j \le K)\)\( \)\mathrm{take}_{\text{new}}[0] = -\infty\( (if day \)i\( is chosen, then \)j \ge 1$)

  • If day \(i\) is not chosen, day \(i-1\) could be either chosen or not, so: $\(\mathrm{notake}_{\text{new}}[j] = \max(\mathrm{take}_{\text{old}}[j],\ \mathrm{notake}_{\text{old}}[j])\)$

Answer: \(\max(\mathrm{take}[K], \mathrm{notake}[K])\) after processing all \(N\) days.

The problem statement guarantees that a valid selection always exists, so this value will never be \(-\infty\).

Example: \(N=3, K=2, A=(3,4,3)\)

After processing take notake
Initial \((-\infty,-\infty,-\infty)\) \((0,-\infty,-\infty)\)
Day 1 (\(a=3\)) \((-\infty,3,-\infty)\) \((0,-\infty,-\infty)\)
Day 2 (\(a=4\)) \((-\infty,4,-\infty)\) \((0,3,-\infty)\)
Day 3 (\(a=3\)) \((-\infty,3,6)\) \((0,4,-\infty)\)

\(\max(\mathrm{take}[2], \mathrm{notake}[2]) = 6\), which gives the correct answer.

Complexity

  • Time complexity: \(O(NK)\) (since \(N \le 5000\) and \(K \le \lceil N/2 \rceil \le 2500\), at most around \(1.25 \times 10^7\) operations)
  • Space complexity: \(O(K)\) (by reusing arrays; storing the entire table would take \(O(NK)\))

Implementation Details

  • Array reuse (order of in-place updates): \(\mathrm{take}_{\text{new}}\) is constructed from the “pre-update \(\mathrm{notake}\)”, while updating \(\mathrm{notake}\) uses the “pre-update \(\mathrm{take}\)”. In code, we construct new_take first, then update notake, and finally replace take with take = new_take, preventing confusion between old and new values.

  • Value of \(-\infty\): We use -10**18. Since the maximum total satisfaction is at most around \(2500 \times 10^9 = 2.5\times10^{12}\), adding to \(-\infty\) multiple times will never cause it to mix with valid values (and since Python supports arbitrary-precision integers, overflow will not occur).

  • Optimizations: In Python, running naive nested loops can be slow, so we speed up the inner loop with techniques such as:

    • Performing the shift and addition together using a list comprehension like new_take = [NEG] + [x + a for x in notake[0:k]]
    • Taking the maximum using notake = [b if b > c else c for b, c in zip(take, notake)]

Additionally, input is read all at once using sys.stdin.buffer.read().split() (repeatedly calling input() for \(N\) lines of input is slow).

  • Do not forget to set take[0] to \(-\infty\). A state where “the current day is chosen, but the total number of chosen days is \(0\)” cannot exist.

    Source Code

import sys

def main():
    data = sys.stdin.buffer.read().split()
    n = int(data[0]); k = int(data[1])
    A = data[2:2+n]
    NEG = -10**18
    take = [NEG] * (k + 1)
    notake = [NEG] * (k + 1)
    notake[0] = 0
    for i in range(n):
        a = int(A[i])
        new_take = [NEG]
        new_take.extend([x + a for x in notake[0:k]])
        notake = [b if b > c else c for b, c in zip(take, notake)]
        take = new_take
    ans = take[k] if take[k] > notake[k] else notake[k]
    sys.stdout.write(str(ans) + "\n")

main()

This editorial was generated by claude5opus-high.