D - 休暇の計画 / Vacation Plan Editorial by admin Editorial for printing
gpt-6-astra-mediumOverview
Choose exactly \(K\) days out of \(N\) days without choosing any adjacent days to maximize the total satisfaction.
By representing each chosen day as an “offset from the earliest possible day,” this problem can be solved efficiently using dynamic programming with prefix maximums.
Discussion
Naive Approaches
Examining all combinations of chosen days is too slow because the number of candidates is extremely large. Furthermore, a greedy approach that picks days in descending order of satisfaction does not guarantee an optimal answer, since choosing a day may prevent selecting stronger candidates adjacent to it.
A standard approach is an \(O(NK)\)-time dynamic programming algorithm with states representing “how many days have been considered” and “how many days have been chosen.” In our implementation, we transform the positions of the chosen days to keep only the necessary states.
Representing Chosen Days by “Offsets”
Let the chosen days in chronological order be \(d_1,d_2,\ldots,d_K\).
Since adjacent days cannot be chosen, we need \(d_{j+1}\ge d_j+2\). The earliest possible choices for these days are \(1,3,5,\ldots,2K-1\).
Therefore, we can express the \(j\)-th chosen day using an offset \(t_j\) as follows:
\(d_j=2j-1+t_j\)
In this case, the condition that adjacent days are not chosen is:
\(2(j+1)-1+t_{j+1}\ge 2j-1+t_j+2\)
which simplifies to:
\(t_{j+1}\ge t_j\)
In addition, since the first day must be on or after day \(1\) and the last day must be on or before day \(N\), we obtain:
\(0\le t_1\le t_2\le\cdots\le t_K\le N-2K+1\)
Conversely, any set of offsets satisfying this condition always forms a valid set of outing days. That is, the original problem is rephrased as choosing \(K\) non-decreasing offsets.
For example, if \(N=7,\ K=3\) and the outing days are \((1,4,7)\), the offsets from the baseline \((1,3,5)\) are \((0,1,2)\).
Letting the number of candidate offsets be \(W=N-2K+2\), each \(t_j\) satisfies \(0\) or more and \(W-1\) or less.
Algorithm
DP State and Transitions
Let \(D_j[t]\) be the following value:
- Exactly \(j\) days have been chosen
- The \(j\)-th offset is \(t\)
- The maximum total satisfaction obtainable under these conditions
For the first outing:
\(D_1[t]=A_{1+t}\)
When \(j\ge 2\), the previous offset \(s\) must satisfy \(s\le t\). Therefore, the transition is:
\(D_j[t]=A_{2j-1+t}+\max_{0\le s\le t}D_{j-1}[s]\)
Any final offset is valid, so the answer is:
\(\max_{0\le t<W}D_K[t]\)
Accelerating Transitions with Prefix Maximums
Finding \(\max_{0\le s\le t}D_{j-1}[s]\) from scratch for each \(t\) would take \(O(KW^2)\) time in total.
However, by processing \(t\) in increasing order, this maximum can be maintained using a variable best.
- Update
bestwith the maximum of its current value and \(D_{j-1}[t]\). - Set \(D_j[t]\) to the sum of
bestand the satisfaction of the day chosen this time.
This reduces the computation of each state to \(O(1)\) time.
Reusing the Array
Since transitions only depend on the immediately preceding stage, we can reuse and overwrite the array dp.
The inner loop of the code does the following:
if dp[t] > best:
best = dp[t]
dp[t] = best + a[base + t]
Before overwriting dp[t], we incorporate its value into best. This ensures that best is always the maximum from dp[0] through dp[t] from the previous stage.
By initializing all elements to \(0\), the first iteration yields dp[t] = a[t], which correctly computes \(D_1[t]\).
Complexity
Let \(W=N-2K+2\).
- Time complexity: \(O(KW)=O(K(N-2K+2))\)
- Space complexity: \(O(W)\) for the DP array; \(O(N)\) for the entire program including storing the input data.
When \(K\) is small, the number of iterations is small; when \(K\) is large, the range of possible offsets is narrow. Thus, the complexity is kept low in either case.
Implementation Notes
Be careful with index mapping.
The \(j\)-th outing day is the \((2j-1+t)\)-th day. Since Python arrays are 0-indexed, the position to access is2 * (j - 1) + t. Here,basein the code corresponds to2 * (j - 1).Initialize
bestat the start of each iteration.
Under the constraints of this problem, satisfaction values are all positive, so settingbest = 0is sufficient.Use the value before overwriting.
If you updatebestafter modifyingdp[t], it would incorporate the value from the current stage, leading to an incorrect transition.Use large integers for the answer.
Since the sum can reach up to \(K\times 10^9\), a 64-bit integer type is required in other languages. Python’s integers can handle this natively.Source Code
import sys
def main():
data = list(map(int, sys.stdin.buffer.read().split()))
n, k = data[0], data[1]
a = data[2:]
width = n - 2 * k + 2
dp = [0] * width
for base in range(0, 2 * k, 2):
best = 0
for t in range(width):
if dp[t] > best:
best = dp[t]
dp[t] = best + a[base + t]
print(max(dp))
if __name__ == "__main__":
main()
This editorial was generated by gpt-6-astra-medium.
posted:
last update: