公式

C - 最も売れる期間 / The Best-Selling Period 解説 by admin 印刷ページ

gemini-3-flash-preview

Overview

This problem asks you to find the maximum sum of a contiguous subarray chosen from a given sequence of \(N\) integers. This is a well-known problem called the “Maximum Subarray Problem.”

Analysis

Naive Approach

Consider trying all combinations of \(L\) (start day) and \(R\) (end day). The number of pairs \((L, R)\) satisfying \(1 \leq L \leq R \leq N\) is approximately \(\frac{N^2}{2}\). Given the constraint \(N \leq 2 \times 10^5\), the number of combinations would be about \(2 \times 10^{10}\), which cannot be computed within the time limit (generally, about \(10^8\) operations can be processed per second).

Efficient Solution

We consider efficiently managing the maximum value of “a contiguous period ending on day \(i\).” The maximum total sales on day \(i\) is the larger of the following two values: 1. Sales on day \(i\) alone: (If the cumulative total up to day \(i-1\) is negative, it is better to start fresh from here) 2. “Maximum cumulative total up to day \(i-1\)” + sales on day \(i\)

By using this “Dynamic Programming (DP)” approach, which leverages the result from the previous day to find the optimal solution for the current day, we can significantly reduce the computational complexity.

Algorithm

Kadane’s Algorithm

This is a method to solve this problem in \(O(N)\). It scans the array once while updating the following two variables:

  1. current_max: The maximum sum of a contiguous subarray that includes the current element \(V_i\) and ends at \(V_i\).
    • Updated as current_max = max(V[i], current_max + V[i]).
  2. max_so_far: The maximum value among all subarrays found so far.
    • Updated as max_so_far = max(max_so_far, current_max).

Even when all sales are negative, by initializing max_so_far to a very small value (or to the first element), we can correctly choose, for example, “the single day with the least deficit.”

Complexity

  • Time Complexity: \(O(N)\)
    • Since the array is scanned only once, it is very fast.
  • Space Complexity: \(O(N)\)
    • Depends on the size of the list used to store the input values.

Implementation Notes

  • Initialization: Since all sales values may be negative, it is important to initialize max_so_far not with 0 but with the first element v[0].

  • Fast I/O: In Python, when the amount of data is large (\(2 \times 10^5\)), using sys.stdin.read().split() instead of input() can reduce execution time.

    Source Code

import sys

def solve():
    # 入力を高速に読み込む
    input_data = sys.stdin.read().split()
    if not input_data:
        return
    
    n = int(input_data[0])
    v = list(map(int, input_data[1:]))
    
    # カネインのアルゴリズム (Kadane's Algorithm) を用いて
    # 最大部分配列和を求める
    # 最初の要素で初期化する(全ての売上がマイナスの場合も考慮)
    max_so_far = v[0]
    current_max = v[0]
    
    for i in range(1, n):
        # 現在の要素単体か、これまでの和に現在の要素を足したものか、大きい方を選ぶ
        current_max = max(v[i], current_max + v[i])
        # これまでの最大値を更新
        if current_max > max_so_far:
            max_so_far = current_max
            
    print(max_so_far)

if __name__ == "__main__":
    solve()

This editorial was generated by gemini-3-flash-preview.

投稿日時:
最終更新: