D - トレーニング計画 / Training Plan Editorial by admin Editorial for printing
Claude Opus 5 (High)Overview
This problem asks us to maximize the total effect over \(N\) days, where on each day we choose either “Regular (effect \(A_i\))” or “Hard (effect \(B_i\))”, under the constraint that Hard cannot be chosen on two consecutive days. Rather than a typical interval DP, this can be solved using Dynamic Programming (DP) where the state keeps track of the choice made on the previous day.
Analysis
Naive Initial Thoughts
One might be tempted to consider a greedy approach: “choose the larger of \(A_i\) and \(B_i\) each day.” However, this can violate the constraint.
For example, with \(N=2\):
1 100
1 100
Choosing Hard on both days yields \(200\), but this is forbidden. The correct answer is to choose Hard on only one of the days, giving \(100 + 1 = 101\).
Furthermore, a greedy strategy of “giving up the smaller one if consecutive Hard days occur” does not work straightforwardly either, because deciding which one to give up depends on the values of the subsequent days. For example, in:
1 10
1 11
1 10
giving up the middle day and choosing Hard on days 1 and 3 (\(10+1+10=21\)) is optimal, but in:
1 10
1 100
1 10
choosing Hard only on the middle day (\(1+100+1=102\)) is optimal. In other words, decisions cannot be made based on local choices alone.
Why Brute Force Fails
Since there are two choices for each day, testing all patterns requires \(2^N\) possibilities. With \(N \leq 2\times10^5\), this is completely infeasible.
The Key to the Solution
The crucial observation here is:
“The only thing that affects the choices after day \(i\) is what was chosen on day \(i\) (Regular or Hard).”
Regardless of what was chosen from day \(1\) through day \(i\), the choices allowed from day \(i+1\) onward depend solely on whether day \(i\) was Hard or not.
Therefore, it is sufficient to maintain only two states:
- The maximum total effect up to day \(i\), given that Regular was chosen on day \(i\).
- The maximum total effect up to day \(i\), given that Hard was chosen on day \(i\).
This naturally leads to a DP formulation.
Algorithm
Let \(\mathrm{dp}[i][0]\) be the maximum effect up to day \(i\) with day \(i\) being Regular, and \(\mathrm{dp}[i][1]\) be the maximum effect up to day \(i\) with day \(i\) being Hard.
The transitions are as follows:
- When choosing Regular on day \(i\): The previous day can be either Regular or Hard. $\(\mathrm{dp}[i][0] = \max(\mathrm{dp}[i-1][0],\ \mathrm{dp}[i-1][1]) + A_i\)$
- When choosing Hard on day \(i\): The previous day must be Regular. $\(\mathrm{dp}[i][1] = \mathrm{dp}[i-1][0] + B_i\)$
The initial values are \(\mathrm{dp}[0][0] = 0\) and \(\mathrm{dp}[0][1] = -\infty\) (since the state of having chosen Hard on day \(0\) does not exist, setting this to \(-\infty\) ensures that the transition to choose Hard on day \(1\) functions correctly. Setting \(\mathrm{dp}[0][1]=0\) would only duplicate the case of choosing Regular on day \(1\) and does no harm, but \(-\infty\) is conceptually clearer and safer).
The answer is \(\max(\mathrm{dp}[N][0], \mathrm{dp}[N][1])\).
Walkthrough with a Concrete Example
Input:
3
1 10
1 11
1 10
- \(i=1\): \(\mathrm{dp}[1][0] = \max(0, -\infty) + 1 = 1\), \(\mathrm{dp}[1][1] = 0 + 10 = 10\)
- \(i=2\): \(\mathrm{dp}[2][0] = \max(1,10) + 1 = 11\), \(\mathrm{dp}[2][1] = 1 + 11 = 12\)
- \(i=3\): \(\mathrm{dp}[3][0] = \max(11,12) + 1 = 13\), \(\mathrm{dp}[3][1] = 11 + 10 = 21\)
The answer is \(\max(13, 21) = 21\), which matches our expectation.
Complexity
- Time Complexity: \(O(N)\)
- Space Complexity: \(O(1)\) (or \(O(N)\) if storing the input in arrays)
Implementation Details
Using two variables instead of an array: Because \(\mathrm{dp}[i]\) only references \(\mathrm{dp}[i-1]\), we only need to update two variables,
dp0anddp1. Overwritingdp0first preventsdp1from accessing the old value, so computendp0andndp1first before reassigning them.Magnitude of the answer: In the worst case, there are up to \(2\times 10^5\) values where \(A_i, B_i \leq 10^9\), resulting in a maximum total of around \(2\times10^{14}\). In languages like C++, a 32-bit
intwill overflow, so use 64-bit integers. In Python, arbitrary-precision integers handle this automatically.Representing \(-\infty\): Use a sufficiently small value such as
-1 << 60. Avoid excessively small values (which risk underflow/overflow upon addition) while ensuring it cannot exceed valid values.Fast I/O: Since there are up to \(2\times10^5\) lines of input, it is safer in Python to read all input at once using
sys.stdin.buffer.read()rather than repeatedly callinginput().The case where \(N=1\): The constraint of consecutive Hard days does not apply, so the answer is \(\max(A_1, B_1)\). The DP above handles this case correctly as is.
Source Code
import sys
def main():
data = sys.stdin.buffer.read().split()
n = int(data[0])
# dp0: 前日が通常, dp1: 前日がハード
dp0 = 0
dp1 = -1 << 60
idx = 1
for i in range(n):
a = int(data[idx]); b = int(data[idx+1])
idx += 2
prev_best = dp0 if dp0 > dp1 else dp1
ndp0 = prev_best + a
ndp1 = dp0 + b
dp0 = ndp0
dp1 = ndp1
print(dp0 if dp0 > dp1 else dp1)
main()
This editorial was generated by claude5opus-high.
posted:
last update: