C - 最も売れる期間 / The Best-Selling Period Editorial by admin Editorial for printing
Claude 4.5 OpusOverview
This is a problem to find the maximum sum of a contiguous subarray. This is a famous problem known as the “Maximum Subarray Problem,” which can be efficiently solved using Kadane’s Algorithm.
Analysis
Naive Approach and Its Issues
If we naively calculate for all pairs \((L, R)\), we get the following: - There are approximately \(\frac{N^2}{2}\) pairs of \((L, R)\) - Computing the sum from \(L\) to \(R\) for each pair takes \(O(N)\) - The total becomes \(O(N^3)\), which is far too slow for \(N = 2 \times 10^5\)
Using prefix sums, we can compute the sum of each interval in \(O(1)\), reducing it to \(O(N^2)\), but this is still too slow.
Key Insight
Consider the maximum sum of a contiguous subarray ending at position \(i\). For a subarray ending at position \(i\), there are two choices:
- Add \(V_i\) to the maximum subarray ending at position \(i-1\)
- Start a new subarray from position \(i\) (an array containing only \(V_i\))
We simply choose the larger of these two options.
Concrete Example
Let’s consider the array \([-2, 1, -3, 4, -1, 2, 1, -5, 4]\):
| \(i\) | \(V_i\) | Previous sum + \(V_i\) | \(V_i\) only | current_sum | max_sum |
|---|---|---|---|---|---|
| 0 | -2 | - | -2 | -2 | -2 |
| 1 | 1 | -1 | 1 | 1 | 1 |
| 2 | -3 | -2 | -3 | -2 | 1 |
| 3 | 4 | 2 | 4 | 4 | 4 |
| 4 | -1 | 3 | -1 | 3 | 4 |
| 5 | 2 | 5 | 2 | 5 | 5 |
| 6 | 1 | 6 | 1 | 6 | 6 |
| 7 | -5 | 1 | -5 | 1 | 6 |
| 8 | 4 | 5 | 4 | 5 | 6 |
The answer is \(6\) (sum from \(V_3\) to \(V_6\): \(4 + (-1) + 2 + 1 = 6\))
Algorithm
We use Kadane’s Algorithm:
current_sum: the maximum sum of a subarray ending at the current positionmax_sum: the maximum sum of a subarray found so far
At each position \(i\), we repeat the following:
current_sum = max(V[i], current_sum + V[i])
max_sum = max(max_sum, current_sum)
current_sum + V[i]: extend the previous subarrayV[i]: start a new subarray
If the sum of the previous subarray is negative, it’s better to start fresh rather than carry it over.
Complexity
- Time complexity: \(O(N)\) — we only traverse the array once
- Space complexity: \(O(N)\) — required to store the input array (the algorithm itself uses \(O(1)\) additional space)
Implementation Notes
- Set initial values as
max_sum = V[0],current_sum = V[0]. This ensures correct behavior even when all elements are negative. - Due to the problem constraints where \(V_i\) ranges from \(-10^9\) to \(10^9\), the total sum can be up to approximately \(2 \times 10^{14}\). In Python, there’s no need to worry about integer overflow, but in C++ and similar languages, you need to use
long long.
Source Code
def solve():
N = int(input())
V = list(map(int, input().split()))
# Kadane's algorithm for maximum subarray sum
# Find the maximum sum of a contiguous subarray
max_sum = V[0] # Maximum subarray sum
current_sum = V[0] # Current subarray sum
for i in range(1, N):
# For the sum of a subarray including the current element,
# choose the larger of:
# 1. Add the current element to the previous subarray
# 2. Start a new subarray from the current element
current_sum = max(V[i], current_sum + V[i])
max_sum = max(max_sum, current_sum)
print(max_sum)
solve()
This editorial was generated by claude4.5opus.
posted:
last update: