D - 夏祭りの出店計画 / Summer Festival Booth Planning Editorial by admin
Claude 4.6 Opus (Thinking)Overview
Given an \(N\)-day festival, select two consecutive \(K\)-day periods with at least 1 day gap between them, and maximize the total expected sales. This can be solved efficiently by precomputing prefix sums and left-side maximum values.
Analysis
Problem with the Naive Approach
If we try all combinations of the starting day \(s\) of the first booth period and the starting day \(t\) of the second booth period, we need to explore \(O(N^2)\) combinations. Since \(N\) can be up to \(10^6\), this is too slow.
Key Insight
If we fix the starting position \(t\) of the second booth period, we only need to choose the one with the maximum sales among the first booth periods that “end sufficiently before \(t\).”
Specifically, when the second interval starts at position \(t\), the starting position \(s\) of the first interval must satisfy \(s \leq t - K - 1\) (at least 1 day gap is required). In other words, if we know “the maximum interval sum from position \(0\) to \(t-K-1\),” we can choose the optimal first interval in \(O(1)\) for each \(t\).
This “maximum interval sum from the left” can be obtained through precomputation.
Algorithm
Compute prefix sums: Calculate \(\text{prefix}[i] = A_0 + A_1 + \cdots + A_{i-1}\) (0-indexed).
Compute interval sums: The sales for \(K\) consecutive days starting at position \(i\) is \(\text{win}[i] = \text{prefix}[i+K] - \text{prefix}[i]\). There are \(N-K+1\) intervals in total for \(i = 0, 1, \ldots, N-K\).
Precompute left-side maximum values: Calculate \(\text{left\_max}[j] = \max(\text{win}[0], \text{win}[1], \ldots, \text{win}[j])\).
Scan the second interval to find the answer: Move the starting position \(t\) of the second interval from \(K+1\) to \(N-K\), and for each \(t\):
- The first interval has maximum sales in the range \(s \leq t - K - 1\) → \(\text{left\_max}[t - K - 1]\)
- Take the maximum of \(\text{left\_max}[t - K - 1] + \text{win}[t]\)
Concrete Example
For \(N=7, K=2, A=[3, 1, 4, 1, 5, 9, 2]\):
- Interval sums: \(\text{win} = [4, 5, 5, 6, 14, 11]\) (sum of each pair of consecutive 2 days)
- \(\text{left\_max} = [4, 5, 5, 6, 14, 14]\)
- Try \(t=3,4,5\) for the second interval:
- \(t=3\): \(\text{left\_max}[0] + \text{win}[3] = 4 + 6 = 10\)
- \(t=4\): \(\text{left\_max}[1] + \text{win}[4] = 5 + 14 = 19\)
- \(t=5\): \(\text{left\_max}[2] + \text{win}[5] = 5 + 11 = 16\)
- Answer: \(19\)
Complexity
- Time complexity: \(O(N)\) (prefix sums, interval sums, left-side maximum values, and scanning are all linear)
- Space complexity: \(O(N)\) (arrays for prefix sums, interval sums, and left-side maximum values)
Implementation Notes
Be careful with 0-indexed and 1-indexed conversion: The problem statement is 1-indexed, but using 0-indexed in the code makes prefix sum indices simpler.
Condition of at least 1 day gap: In 0-indexed terms, if the first interval is \([s, s+K-1]\) and the second is \([t, t+K-1]\), then \(t \geq s + K + 1\), i.e., \(s \leq t - K - 1\). Start scanning from \(t \geq K+1\) so that \(t - K - 1 \geq 0\).
Fast input: Since \(N\) can be up to \(10^6\), reading all input at once with
sys.stdin.buffer.read()can help avoid TLE in Python.Source Code
import sys
def main():
input_data = sys.stdin.buffer.read().split()
N = int(input_data[0])
K = int(input_data[1])
A = [int(input_data[i + 2]) for i in range(N)]
# Compute prefix sums
prefix = [0] * (N + 1)
for i in range(N):
prefix[i + 1] = prefix[i] + A[i]
# sum of window starting at index s (0-indexed): prefix[s+K] - prefix[s]
# Number of windows: N - K + 1, indexed 0 to N-K
# For the constraint: t >= s + K + 1 (1-indexed)
# In 0-indexed: if first window starts at s, second starts at t
# first window covers [s, s+K-1], second covers [t, t+K-1]
# We need t >= s + K + 1 (gap of at least 1 day)
# Compute window sums
num_windows = N - K + 1
win = [0] * num_windows
for i in range(num_windows):
win[i] = prefix[i + K] - prefix[i]
# left_max[j] = max of win[0..j]
left_max = [0] * num_windows
left_max[0] = win[0]
for i in range(1, num_windows):
left_max[i] = max(left_max[i - 1], win[i])
# For each second window starting at t (0-indexed), the first window must start at s where s + K + 1 <= t (0-indexed: s + K <= t - 1, so s <= t - K - 1)
# So the first window index s can be at most t - K - 1
# We need t - K - 1 >= 0, i.e., t >= K + 1
ans = -float('inf')
for t in range(K + 1, num_windows):
best_first = left_max[t - K - 1]
ans = max(ans, best_first + win[t])
print(ans)
main()
This editorial was generated by claude4.6opus-thinking.