D - 夏祭りの出店計画 / Summer Festival Booth Planning Editorial by admin Editorial for printing
GPT 5.2 HighOverview
Select two contiguous intervals of length \(K\) such that there is at least one day gap between them (i.e., the intervals are separated by at least \(K\) days), and maximize the total sales.
Analysis
Key Insight
Define the sum of an interval of length \(K\) as: $\(w_j=\sum_{i=j}^{j+K-1}A_i\quad (j=1,2,\dots,N-K+1)\)$
Then what we want to find is the maximum value of \(w_s+w_t\) where: - The starting days of the two intervals are \(s,t\) with \(t\ge s+K+1\) - In other words, in terms of interval sum indices, \(t \ge s+(K+1)\)
If we fix a particular \(t\), the valid values of \(s\) satisfy: $\(s \le t-(K+1)\)$
So the expression becomes: $\(\max_{s \le t-(K+1)} w_s + w_t\)$
This means we just need to compute “the maximum interval sum sufficiently far to the left” + “the current interval sum” at each step.
Why the Naive Approach Fails
- Exhaustively searching all pairs \((s,t)\) results in \(O(N^2)\) combinations, which is far too slow for \(N\le 10^6\).
- Recomputing the interval sum \(w_j\) from scratch each time costs \(O(K)\), leading to \(O(NK)\) overall.
Solution Strategy
- Compute the interval sums \(w_j\) using a sliding window with \(O(1)\) updates, enumerating all values in \(O(N)\) total.
- For each \(t\), we need “the maximum \(w\) value at or before index \(t-(K+1)\)”, so we maintain the left-side maximum while scanning.
However, simply storing all \(w\) values in an array uses \(O(N)\) memory. The code goes further by keeping only the most recent \(K+1\) interval sums in a ring buffer, incorporating “the interval sum that just became eligible” into the maximum value, thereby reducing memory to \(O(K)\).
Algorithm
1. Build Interval Sums \(w\) with a Sliding Window
Read the dates from left to right while maintaining the sum of the most recent \(K\) elements, sumK.
- For the first \(K\) elements, simply accumulate the sum.
- After that, update in \(O(1)\) by “adding the new element and subtracting the element leaving the window.”
This way, we obtain the interval sums \(w_0,w_1,\dots\) (using 0-indexing) one by one.
2. Maintain the Maximum of “Left Intervals Separated by at Least \(K+1\)”
Let \(j\) (0-indexed) be the index of the interval sum. To use \(w_j\) as the right interval, the left interval must satisfy: $\(\text{left} \le j-(K+1)\)$
We maintain the following:
- best_first: the maximum interval sum among those eligible as a left interval so far
- A ring buffer buf of size \(K+1\) holding the most recent \(K+1\) interval sums
For the current \(j\):
- If \(j \ge K+1\), then the interval sum newly becoming eligible as a left interval is \(w_{j-(K+1)}\)
- This can be retrieved from the ring buffer, and we use it to update best_first
- Then we update the answer with the candidate best_first + w_j
Why a Ring Buffer Works
To retrieve \(w_{j-(K+1)}\), we only need “the interval sum from \(K+1\) steps ago.” Therefore, it suffices to always remember only the most recent \(K+1\) values.
(Small Example)
For example, when \(K=2\), the two intervals’ starting positions must be at least \(3\) days apart, and the index difference of the interval sums must also be at least \(3\) (\(K+1=3\)). If the right interval is \(w_5\), then the left interval can be any of \(w_0,w_1,w_2\), and if we have their maximum value, the best pair with \(w_5\) can be determined immediately.
Complexity
- Time complexity: \(O(N)\)
(Each day involves a constant number of read/update operations) - Space complexity: \(O(K)\)
(\(K\) for the sliding window, \(K+1\) for the ring buffer)
Implementation Notes
The constraints are large (\(N\le 10^6\)), so in Python, I/O can easily become a bottleneck. The code uses
sys.stdin.buffer.read()with a custom parser for speed.The timing for “when an interval sum becomes eligible as a left interval” has a delay of \(K+1\). So we insert interval sums into the ring buffer, and when the current \(j\) advances, we retrieve \(w_{j-(K+1)}\) to update
best_first.Initially, when \(j < K+1\), no left interval exists, so the answer update only begins after that point (the
if j >= buf_size:part in the code).Source Code
import sys
def main():
data = sys.stdin.buffer.read()
ndata = len(data)
idx = 0
def next_int():
nonlocal idx
while idx < ndata and data[idx] <= 32:
idx += 1
num = 0
while idx < ndata and data[idx] > 32:
num = num * 10 + (data[idx] - 48)
idx += 1
return num
N = next_int()
K = next_int()
window_buf = [0] * K
wptr = 0
sumK = 0
buf_size = K + 1
buf = [0] * buf_size
ptr = 0
best_first = 0
ans = 0
j = -1 # window index
for i in range(N):
a = next_int()
if i < K:
window_buf[i] = a
sumK += a
if i == K - 1:
j = 0
w = sumK
buf[0] = w
ptr = 1
if ptr == buf_size:
ptr = 0
else:
sumK += a - window_buf[wptr]
window_buf[wptr] = a
wptr += 1
if wptr == K:
wptr = 0
j += 1
w = sumK
if j >= buf_size:
old = buf[ptr] # w_{j-(K+1)}
if old > best_first:
best_first = old
cand = best_first + w
if cand > ans:
ans = cand
buf[ptr] = w
ptr += 1
if ptr == buf_size:
ptr = 0
sys.stdout.write(str(ans) + "\n")
if __name__ == "__main__":
main()
This editorial was generated by gpt-5.2-high.
posted:
last update: