公式

D - 休暇の計画 / Vacation Plan 解説 by admin

Gemini 3.8 Flash (High)

Overview

This problem asks us to find the maximum possible satisfaction (the sum of \(A_i\) for the chosen days) obtained by choosing exactly \(K\) days to go out from an \(N\)-day vacation such that no two chosen days are consecutive.

Analysis

Naive Approaches

  • Brute-force search: Checking all possible combinations of choosing \(K\) days out of \(N\) days yields up to \(\binom{N}{K}\) combinations (an astronomical number when \(N=5000, K=2500\)), which will exceed the time limit (TLE).
  • Greedy approach: Trying to greedily choose days in descending order of satisfaction \(A_i\) fails because of the constraint that “no two consecutive days can be chosen.” Choosing a particular day might prevent us from choosing days before or after it with higher total value, resulting in a suboptimal solution (WA).

Solution (Dynamic Programming)

Consider deciding whether to go out or not, day by day, from day \(1\) to day \(N\). Whether we can go out on day \(i\) depends solely on whether we went out on the previous day (day \(i-1\)). Furthermore, since we must choose exactly \(K\) days in total, we also need to keep track of how many days we have chosen so far.

By maintaining the “previous state” and the “number of chosen days” as our DP states, we can solve this problem efficiently using Dynamic Programming (DP).

Algorithm

We perform DP with states separated by the action taken on the previous day.

DP Table Definition

  • \(dp0[j]\): Maximum satisfaction when not going out on the previous day and having gone out for a total of \(j\) days so far
  • \(dp1[j]\): Maximum satisfaction when going out on the previous day and having gone out for a total of \(j\) days so far

Initial State

  • \(dp0[0] = 0\) (\(0\) days gone out, satisfaction is \(0\))
  • All other entries in \(dp0[j]\) and \(dp1[j]\) are initialized to \(-\infty\), representing unreachable states

Transitions

For day \(i\) (with satisfaction \(A_i\)), update the next day’s state next_dp for each \(j = 0, 1, \dots, K\) as follows:

  1. When not going out on day \(i-1\) (transitions from \(dp0[j]\))

    • Do not go out on day \(i\): The number of days out remains \(j\). $\(\text{next\_dp0}[j] = \max(\text{next\_dp0}[j], dp0[j])\)$
    • Go out on day \(i\): The number of days out becomes \(j + 1\), and we gain satisfaction \(A_i\) (valid transition only when \(j + 1 \le K\)). $\(\text{next\_dp1}[j + 1] = \max(\text{next\_dp1}[j + 1], dp0[j] + A_i)\)$
  2. When going out on day \(i-1\) (transitions from \(dp1[j]\))

    • Do not go out on day \(i\): The number of days out remains \(j\). $\(\text{next\_dp0}[j] = \max(\text{next\_dp0}[j], dp1[j])\)$
    • Go out on day \(i\): Since going out on consecutive days is prohibited, this transition does not exist.

Answer

After processing all \(N\) days, the answer is \(\max(dp0[K], dp1[K])\), which is the maximum satisfaction among states where exactly \(K\) days were chosen.

Complexity

  • Time Complexity: \(O(N \times K)\) We iterate \(i\) from \(1\) to \(N\), and at each step, we iterate \(j\) from \(0\) to \(K\). Given \(N \le 5000, K \le 2500\), the number of operations is at most around \(5000 \times 2500 = 1.25 \times 10^7\), which easily fits within the time limit (typically around 2 seconds).
  • Space Complexity: \(O(K)\) By using the “rolling array” technique to only keep the previous day’s state and the current day’s state (arrays of size \(K+1\)), memory usage can be kept small.

Implementation Notes

  • Handling Overflow: The maximum possible satisfaction can reach \(K \times \max(A_i) \approx 2500 \times 10^9 = 2.5 \times 10^{12}\), which exceeds the range of a 32-bit integer (about \(2 \times 10^9\)). Therefore, 64-bit integers (long long in C++) must be used for the DP table and the answer variable.

  • Choosing Initial Values: To represent the unreachable state \(-\infty\), setting it to around \(-10^{18}\) is safe to prevent overflow (or underflow) when adding \(A_i\).

    Source Code

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int N, K;
    if (!(cin >> N >> K)) return 0;

    vector<long long> A(N);
    for (int i = 0; i < N; ++i) {
        cin >> A[i];
    }

    const long long INF = 1e18;
    vector<long long> dp0(K + 1, -INF);
    vector<long long> dp1(K + 1, -INF);

    dp0[0] = 0;

    for (int i = 0; i < N; ++i) {
        vector<long long> next_dp0(K + 1, -INF);
        vector<long long> next_dp1(K + 1, -INF);

        for (int j = 0; j <= K; ++j) {
            // dp0[j] からの遷移
            if (dp0[j] != -INF) {
                // i日目に外出しない
                next_dp0[j] = max(next_dp0[j], dp0[j]);
                // i日目に外出する
                if (j + 1 <= K) {
                    next_dp1[j + 1] = max(next_dp1[j + 1], dp0[j] + A[i]);
                }
            }
            // dp1[j] からの遷移
            if (dp1[j] != -INF) {
                // i日目に外出しない(連続外出は不可なので、外出する遷移はなし)
                next_dp0[j] = max(next_dp0[j], dp1[j]);
            }
        }

        dp0 = move(next_dp0);
        dp1 = move(next_dp1);
    }

    long long ans = max(dp0[K], dp1[K]);
    cout << ans << "\n";

    return 0;
}

This editorial was generated by gemini-3.8-flash-high.