D - 夏祭りの出店計画 / Summer Festival Booth Planning 解説 by admin
gemini-3.1-pro-thinkingOverview
Given the projected sales for \(N\) days, the problem asks to select two consecutive periods of length \(K\) (with at least 1 day gap between them) and maximize the total sales.
Analysis
First, since computing the sum of \(K\) consecutive days repeatedly is wasteful, we precompute the sum of \(K\) days of sales starting from each day. We denote this as \(S[i]\) (\(S[i] = A_i + A_{i+1} + \dots + A_{i+K-1}\)).
Next, we consider how to choose the two periods. Let \(s\) be the starting day of the first period and \(t\) be the starting day of the second period. The problem’s condition requires \(t \geq s + K + 1\). Naively trying all combinations of \((s, t)\) would result in roughly \(\frac{N^2}{2}\) pairs, which would exceed the time limit (TLE) given the constraint \(N \leq 10^6\).
To solve this efficiently, we take the approach of “fixing the later period \(t\) and quickly finding the maximum among all valid earlier periods \(s\)”. As \(t\) moves from left to right by \(1\), the range of \(s\) satisfying \(s \leq t - K - 1\) also expands by \(1\) to the right. In other words, we only need to compare the newly added candidate \(S[t - K - 1]\) with the current maximum and update accordingly, allowing us to obtain the optimal first period’s sales in \(O(1)\) at all times.
Algorithm
Precomputation of interval sums (Sliding Window) We handle array \(A\) as 0-indexed (starting from 0). First, compute the sum of the first \(K\) days, \(S[0]\). Then, for \(i = 1, 2, \dots, N-K\), compute \(S[i] = S[i-1] - A[i-1] + A[i+K-1]\) by “subtracting the first element and adding the next element after the end.” This computes all interval sums in \(O(N)\).
Searching for the optimal solution Initialize a variable
max_sto \(0\) to hold the maximum value of the first period. Iterate over the starting day \(t\) of the second period from \(K+1\) to \(N-K\) in order.- For the current \(t\), the newly available candidate for the first period has starting day \(s\_idx = t - K - 1\).
- Update
max_swithmax(max_s, S[s_idx]). - The total sales for the current \(t\) is
max_s + S[t]. Update the overall maximumanswith this value.
Complexity
- Time complexity: \(O(N)\)
- The interval sum computation (sliding window) takes \(O(N)\), and the maximum search loop takes \(O(N)\), so the overall algorithm runs in \(O(N)\), which is very fast.
- Space complexity: \(O(N)\)
- The array \(S\) storing the interval sums from each starting day uses \(O(N)\) memory.
Implementation Notes
Be careful with index handling The problem statement uses 1-indexed (starting from 1), but converting to 0-indexed in the implementation makes index management easier. The condition \(t \geq s + K + 1\) holds as-is in 0-indexed notation.
Fast I/O Since the input size can be as large as \(N = 10^6\), in Python, using
sys.stdin.read().split()to read all input at once can significantly reduce execution time.Source Code
import sys
def main():
input_data = sys.stdin.read().split()
if not input_data:
return
N = int(input_data[0])
K = int(input_data[1])
iterator = map(int, input_data[2:])
A = list(iterator)
S = [0] * (N - K + 1)
current_sum = sum(A[:K])
S[0] = current_sum
for i in range(1, N - K + 1):
current_sum += A[i + K - 1] - A[i - 1]
S[i] = current_sum
ans = 0
max_s = 0
for t in range(K + 1, N - K + 1):
s_idx = t - K - 1
if S[s_idx] > max_s:
max_s = S[s_idx]
val = max_s + S[t]
if val > ans:
ans = val
print(ans)
if __name__ == '__main__':
main()
This editorial was generated by gemini-3.1-pro-thinking.