D - トレーニング計画 / Training Plan Editorial by admin Editorial for printing
Gemini 3.8 Flash (High)Overview
Over a training period of \(N\) days, on each day we choose either “regular training (effectiveness \(A_i\))” or “hard training (effectiveness \(B_i\))”. Under the constraint that hard training cannot be chosen on \(2\) consecutive days, find the maximum total effectiveness that can be achieved.
Analysis
Naive Approach
Since there are \(2\) choices (“regular” or “hard”) for each day, a simple exhaustive search trying all possible combinations would examine \(2^N\) combinations. Given the constraint \(N \leq 2 \times 10^5\), this will far exceed the time limit and result in TLE (Time Limit Exceeded).
State Simplification and Dynamic Programming (DP)
The key observation here is that “the choice on day \(i\) is affected only by whether hard training was performed on day \(i-1\).” Choices made on or before day \(i-2\) do not directly affect the choice on day \(i\).
For problems with the property that “the exact history of past choices is not needed, and only the immediately preceding state is required,” Dynamic Programming (DP) is extremely effective.
For each day \(i\), consider maintaining the following \(2\) values: - \(dp[i][0]\): The maximum effectiveness achieved up to day \(i\) if “regular training” is performed on day \(i\) - \(dp[i][1]\): The maximum effectiveness achieved up to day \(i\) if “hard training” is performed on day \(i\)
Deriving State Transitions
The transitions from the state on day \(i-1\) to day \(i\) are as follows:
When performing regular training on day \(i\) (\(dp[i][0]\)):
- On the previous day (day \(i-1\)), either regular training or hard training could have been performed without issue.
- Therefore, we can simply take the maximum value from the previous day and add \(A_i\): $\(dp[i][0] = \max(dp[i-1][0], dp[i-1][1]) + A_i\)$
When performing hard training on day \(i\) (\(dp[i][1]\)):
- Since hard training cannot be performed on consecutive days, regular training must have been performed on the previous day (day \(i-1\)).
- Therefore, we add \(B_i\) to the value where regular training was performed on the previous day: $\(dp[i][1] = dp[i-1][0] + B_i\)$
Algorithm
- Initialize the values for day \(1\):
- \(dp[1][0] = A_1\)
- \(dp[1][1] = B_1\)
- For \(i = 2, 3, \dots, N\), update the values sequentially according to the transition formulas above.
- Since the computation for day \(i\) uses only the values from day \(i-1\), there is no need to allocate an array of size \(N\); it is sufficient to keep track of only the previous day’s values using variables (
dp0anddp1in the code).
- Since the computation for day \(i\) uses only the values from day \(i-1\), there is no need to allocate an array of size \(N\); it is sufficient to keep track of only the previous day’s values using variables (
- After computing up to day \(N\), output \(\max(dp[N][0], dp[N][1])\).
Complexity
- Time Complexity: \(O(N)\)
- We iterate from day \(1\) to day \(N\) once, and the operations at each step consist solely of constant-time comparisons and additions, resulting in an \(O(N)\) runtime. Even for \(N = 2 \times 10^5\), it executes in just a few milliseconds and easily runs within the time limit.
- Space Complexity: \(O(1)\)
- Because we only update two variables representing the results from the previous day, no extra memory is needed.
Implementation Notes
Beware of Integer Overflow:
The maximum possible total effectiveness can reach \(N \times \max(B_i) \approx 2 \times 10^5 \times 10^9 = 2 \times 10^{14}\).
This significantly exceeds the maximum value of a standard 32-bit signed integer (
int, approximately \(2 \times 10^9\)). Therefore, be sure to use a 64-bit integer type (such aslong longin C++) for the variables storing DP values.Source Code
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
if (!(cin >> n)) return 0;
long long dp0 = 0; // その日に通常トレーニングを行った場合の最大値
long long dp1 = 0; // その日にハードトレーニングを行った場合の最大値
for (int i = 0; i < n; ++i) {
long long a, b;
cin >> a >> b;
if (i == 0) {
dp0 = a;
dp1 = b;
} else {
long long next_dp0 = max(dp0, dp1) + a;
long long next_dp1 = dp0 + b;
dp0 = next_dp0;
dp1 = next_dp1;
}
}
cout << max(dp0, dp1) << "\n";
return 0;
}
This editorial was generated by gemini-3.8-flash-high.
posted:
last update: