D - トレーニング計画 / Training Plan Editorial by admin Editorial for printing
gpt-6-astra-mediumOverview
We want to decide each day’s workout menu to maximize the total effectiveness without choosing hard training on consecutive days. This can be solved using dynamic programming (DP) by keeping track of which menu was chosen on the most recent day as the state.
Analysis
Since there are \(2\) choices (normal or hard) for each day, checking all combinations would result in \(2^N\) possibilities. Since \(N\) can be up to \(2 \times 10^5\), an exhaustive search will not run in time.
Additionally, a greedy approach that always chooses the menu with the larger effectiveness among the available options is also incorrect.
For example, consider the following effectiveness for \(2\) days:
| Day | Normal \(A_i\) | Hard \(B_i\) |
|---|---|---|
| \(1\) | \(1\) | \(2\) |
| \(2\) | \(1\) | \(100\) |
If we choose hard training on Day \(1\) because it has higher effectiveness, we can only choose normal training on Day \(2\), yielding a total of \(2+1=3\). On the other hand, choosing normal on Day \(1\) and hard on Day \(2\) yields a total of \(1+100=101\).
The key observation here is that whether we can choose hard training today depends solely on yesterday’s menu. We do not need to remember all the menus prior to that.
Therefore, we can manage the maximum effectiveness up to each day by dividing it into the following \(2\) cases:
- When the last day’s menu is normal training
- When the last day’s menu is hard training
If the last menu is the same, the menus that can be chosen from the next day onward are also the same. Thus, it suffices to keep only the maximum total effectiveness for each case.
Algorithm
1. DP State
For the training up to day \(i\), we define the DP states as follows:
- \(dp_{\mathrm{normal}}[i]\): The maximum total effectiveness when normal training is chosen on day \(i\)
- \(dp_{\mathrm{hard}}[i]\): The maximum total effectiveness when hard training is chosen on day \(i\)
2. Initial Values
For convenience, the state before any training has been performed is set as follows:
- \(dp_{\mathrm{normal}}[0]=0\)
- \(dp_{\mathrm{hard}}[0]=-\infty\)
This initialization reflects that the total effectiveness is \(0\) at the start, and allows either normal or hard training to be chosen on the first day.
3. Transitions
When choosing normal training on day \(i\)
The previous day can be either normal or hard. Therefore, we add \(A_i\) to the maximum value up to the previous day.
\(dp_{\mathrm{normal}}[i]=\max(dp_{\mathrm{normal}}[i-1],dp_{\mathrm{hard}}[i-1])+A_i\)
When choosing hard training on day \(i\)
Since hard training cannot be performed on consecutive days, the previous day must be normal.
\(dp_{\mathrm{hard}}[i]=dp_{\mathrm{normal}}[i-1]+B_i\)
These transitions account for all valid choices on each day. Therefore, by computing sequentially from the initial state, we can obtain the maximum value for each state.
4. Updating with Variables Only
To compute the values for day \(i\), only the values from day \(i-1\) are needed. Thus, a full DP array is unnecessary, and we can use the following \(2\) variables in the code:
normal: The maximum value when the previous day was normalhard: The maximum value when the previous day was hard
We read the input for each day and update them as follows:
long long next_normal = max(normal, hard) + A;
long long next_hard = normal + B;
normal = next_normal;
hard = next_hard;
Since either menu is allowed on the last day, the answer is max(normal, hard) after processing all \(N\) days.
Complexity
- Time Complexity: \(O(N)\)
For each day, a constant number of comparisons and additions are performed. - Space Complexity: \(O(1)\)
We process the input sequentially and maintain the DP values using only a constant number of variables.
Implementation Notes
Use
long longfor the total value.
Since the answer can be up to \(N \times 10^9 \leq 2 \times 10^{14}\), it will overflow a 32-bit integer type.Compute both next states using the values before the update.
If you overwritenormalfirst before computingnext_hard, it would incorrectly include the effect of the normal training from the same day. Always compute into temporary variables before updating.Use a sufficiently small value for unreachable states.
In the code,-(1LL << 60)is used instead of \(-\infty\). This value is sufficiently smaller than any possible total effectiveness, and it will not cause overflow during addition here.Source Code
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N;
cin >> N;
long long normal = 0;
long long hard = -(1LL << 60);
for (int i = 0; i < N; ++i) {
long long A, B;
cin >> A >> B;
long long next_normal = max(normal, hard) + A;
long long next_hard = normal + B;
normal = next_normal;
hard = next_hard;
}
cout << max(normal, hard) << '\n';
return 0;
}
This editorial was generated by gpt-6-astra-medium.
posted:
last update: