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 training (effect \(A_i\))” or “hard training (effect \(B_i\))”, subject to the constraint that hard training cannot be chosen on two consecutive days. It can be solved using dynamic programming (DP), which is typical for problems where “the previous choice affects the next choice.”
Analysis
Naive Approaches and Their Issues
First, let us consider trying “all possible combinations.” Since there are 2 choices for each day, there are up to \(2^N\) ways to choose. Because \(N \leq 2 \times 10^5\), this is an astronomical number, so an exhaustive search will definitely not run in time.
Next, let us consider a “greedy approach.” A simple greedy strategy such as “choose hard training whenever \(A_i < B_i\)” fails. For example:
3
1 10
1 10
1 10
In this case, we would want to choose hard training every day, but doing so on two consecutive days is prohibited, making this impossible. The actual optimal solution is “Hard, Regular, Hard”, yielding \(10 + 1 + 10 = 21\).
Furthermore, a greedy strategy like “assign hard training in descending order of the difference \(B_i - A_i\)” also fails due to the adjacency constraint (e.g., if the differences are 5, 6, 5, choosing the middle one prevents taking both ends, yielding 6, whereas taking both ends yields 10). Therefore, greedy approaches will result in WA.
Key Observation
The constraint depends solely on “whether hard training was done on the previous day.” In other words, once we decide the choices up to day \(i\), the only information that affects subsequent choices is:
- Whether we did regular or hard training on day \(i\)
There are only these 2 possibilities. Details such as when hard training was chosen earlier in the past have no bearing on subsequent decisions.
Therefore, by maintaining the state “considering up to day \(i\), the maximum total effect when the choice on day \(i\) is regular/hard training,” we can update the answer while advancing through the days from \(1\) to \(N\). This gives us the DP formulation.
Algorithm
We define the following DP:
- \(dp[i][0]\): The maximum total effect when making decisions up to day \(i\), with regular training chosen on day \(i\)
- \(dp[i][1]\): The maximum total effect when making decisions up to day \(i\), with hard training chosen on day \(i\)
Initialization (\(i=0\), representing a fictitious day 0)
\[dp[0][0] = 0,\quad dp[0][1] = -\infty\]
Since we must not act as if hard training was done on day \(0\) (which would prevent choosing hard training on day \(1\)), we set \(dp[0][1]\) to a sufficiently small value representing an “unreachable state.”
Transitions (\(i = 1, 2, \dots, N\))
- When choosing regular training on day \(i\): any choice on the previous day is allowed, so $\(dp[i][0] = \max(dp[i-1][0],\ dp[i-1][1]) + A_i\)$
- When choosing hard training on day \(i\): the previous day must not be hard training, so $\(dp[i][1] = dp[i-1][0] + B_i\)$
Answer
\[\max(dp[N][0],\ dp[N][1])\]
Walkthrough with a Concrete Example
Let us trace through the earlier example (\(A = (1,1,1)\), \(B = (10,10,10)\)).
| \(i\) | \(dp[i][0]\) | \(dp[i][1]\) |
|---|---|---|
| 0 | 0 | \(-\infty\) |
| 1 | \(\max(0,-\infty)+1 = 1\) | \(0+10 = 10\) |
| 2 | \(\max(1,10)+1 = 11\) | \(1+10 = 11\) |
| 3 | \(\max(11,11)+1 = 12\) | \(11+10 = 21\) |
The answer is \(\max(12,21) = 21\), which matches our manual calculation.
Complexity
- Time complexity: \(O(N)\) (only a constant number of comparisons and additions per day)
- Space complexity: \(O(1)\) (it suffices to keep only the two values from the previous day; even using an array is \(O(N)\))
Implementation Notes
Beware of overflow: Since \(A_i, B_i \leq 10^9\) and \(N \leq 2\times 10^5\), the sum can reach around \(2\times 10^{14}\), which overflows a 32-bit integer. Be sure to use 64-bit integers (
long longin C++).Choosing the value for \(-\infty\): Setting \(dp[0][1]\) directly to
LLONG_MINcarries the risk of signed integer overflow (undefined behavior) when adding \(+A_i\) or similar during subsequent transitions. It is safe to use a value with some margin, such asLLONG_MIN/4, as in the code. Alternatively, you can treat \(i=1\) as a base case and initialize directly with \(dp[1][0]=A_1,\ dp[1][1]=B_1\).No array needed: Since transitions only depend on the immediately preceding day, it is sufficient to reuse variables
dp0anddp1. When doing so, it is important to temporarily store the new values inn0andn1before updating them together. If you overwritedp0first, you will end up using the wrong value when calculating \(dp[i][1] = dp[i-1][0] + B_i\).Large input size: Since \(N\) is up to \(2\times 10^5\), when using
cinin C++, make sure to includeios::sync_with_stdio(false); cin.tie(nullptr);, or usescanfas in this code to be safe.The DP above works correctly as-is even when \(N = 1\), giving the answer \(\max(A_1, B_1)\).
Source Code
#include <bits/stdc++.h>
using namespace std;
int main(){
int n;
if(scanf("%d",&n)!=1) return 0;
const long long NEG = LLONG_MIN/4;
long long dp0 = 0, dp1 = NEG; // dp0: last day normal, dp1: last day hard
for(int i=0;i<n;i++){
long long a,b;
scanf("%lld %lld",&a,&b);
long long n0 = max(dp0, dp1) + a;
long long n1 = dp0 + b;
dp0 = n0; dp1 = n1;
}
printf("%lld\n", max(dp0, dp1));
return 0;
}
This editorial was generated by claude5opus-high.
posted:
last update: