公式

D - 夏休みの集中特訓 / Intensive Summer Vacation Training 解説 by admin

gpt-6-astra-medium

Overview

We want to find the number of contiguous subarrays with length at most \(K\) and element sum at least \(X\). Using the fact that all \(A_i\) are positive, we can count the number of valid left endpoints for each right endpoint using the two-pointer technique.

Analysis

Checking All Subarrays Is Too Slow

If we try all possible left and right endpoints, there are up to \(O(N^2)\) subarrays. Even if we compute the sum of each subarray in \(O(1)\) using prefix sums, this will not run in time for \(N \le 10^6\).

Therefore, we consider fixing the right endpoint and counting the valid left endpoints efficiently. Below, we use 0-based indexing for the array to match code implementations.

Left Endpoints with Sum at Least \(X\) Are Grouped Towards the Front

Fix the right endpoint to right.

Since all \(A_i\) are positive, moving the left endpoint to the right strictly decreases the subarray sum. Thus, the left endpoints with a sum of at least \(X\) can be characterized using a boundary left as follows:

  • Left endpoint is \(0,1,\ldots,\text{left}-1\): the sum is at least \(X\)
  • Left endpoint is \(\text{left},\ldots,\text{right}\): the sum is less than \(X\)

In other words, once we find left, we can handle all left endpoints satisfying the sum condition at once.

Furthermore, as we move the right endpoint to the right, a positive value is added, so any interval that previously had a sum of at least \(X\) will continue to satisfy the condition. Therefore, the boundary left never needs to move back to the left. This is why the two-pointer technique can be applied.

Combining with the Length Condition

Let \(l\) be the left endpoint of the subarray. The condition that the length is at most \(K\) is:

\(\text{right}-l+1 \le K\)

which simplifies to:

\(l \ge \text{right}-K+1\)

Considering the array boundaries, the minimum valid left endpoint is:

\(\text{first}=\max(0,\text{right}-K+1)\)

Therefore, the left endpoints that satisfy both conditions are:

\(\text{first},\text{first}+1,\ldots,\text{left}-1\)

and the number of such endpoints is:

\(\max(0,\text{left}-\text{first})\)

Algorithm

Initialize left = 0, total = 0, and answer = 0.

Iterate right from \(0\) to \(N-1\), performing the following steps:

  1. Add A[right] to total.
  2. While total >= X, repeat:
    • Subtract A[left] from total.
    • Increment left by \(1\).
  3. Compute first = max(0, right - K + 1).
  4. If left > first, add left - first to answer.

Finally, output answer.

Why the Boundary Is Determined After the while Loop

total maintains the sum of the subarray \([\text{left},\text{right}]\).

In the while loop, we verify that the current sum is at least \(X\) before removing the element at left. Thus, the left endpoint immediately before removal satisfies the sum condition.

When the loop terminates, total < X, which means any subarray starting at or after the current left will have a sum less than \(X\). On the other hand, all left endpoints that have already been passed satisfy the sum condition.

Hence, left correctly serves as the desired boundary.

Concrete Example

Consider \(A=[2,3,2]\), \(K=2\), and \(X=4\), and let the right endpoint be the last element, i.e., \(\text{right}=2\).

The sums for each left endpoint are as follows:

Left Endpoint Subarray Elements Sum Length
\(0\) \([2,3,2]\) \(7\) \(3\)
\(1\) \([3,2]\) \(5\) \(2\)
\(2\) \([2]\) \(2\) \(1\)

The left endpoints satisfying the sum condition are \(0,1\), so the boundary is \(\text{left}=2\).

Meanwhile, from the length condition:

\(\text{first}=\max(0,2-2+1)=1\)

The only left endpoint satisfying both conditions is \(1\), so the number added to the answer is \(2-1=1\).

By counting in this manner for each right endpoint, every valid subarray is counted exactly once without duplicates.

Complexity

  • Time Complexity: \(O(N)\)
  • Space Complexity: \(O(N)\)

Although the while loop might look like a nested loop, left only moves to the right and increases at most \(N\) times across the entire algorithm. Since right also advances \(N\) times, the overall time complexity is \(O(N)\).

The space complexity is determined by storing the array A. The additional working memory aside from the array is \(O(1)\).

Implementation Notes

  • left itself is not included in the left endpoints that satisfy the sum condition. Since the maximum valid left endpoint is left - 1, the count is left - first, not left - first + 1.

  • If left <= first due to the length restriction, there are no valid subarrays, so we add nothing.

  • If left = right + 1, the subarray being maintained is empty and total = 0. Since \(X \ge 1\), the while loop will stop there, preventing out-of-bounds array access.

  • The answer can be up to \(\frac{N(N+1)}{2}\), and subarray sums can reach up to \(10^{15}\). While this fits within Python’s arbitrary-precision integers, a 64-bit integer type is required in languages like C++ and Java.

  • This method relies on the fact that all \(A_i\) are positive. If negative values were present, this approach could not be applied directly.

    Source Code

import sys


def main():
    read = sys.stdin.buffer
    N, K, X = map(int, read.readline().split())
    A = list(map(int, read.read().split()))

    left = 0
    total = 0
    answer = 0

    for right in range(N):
        total += A[right]

        while total >= X:
            total -= A[left]
            left += 1

        first = right - K + 1
        if first < 0:
            first = 0

        if left > first:
            answer += left - first

    print(answer)


if __name__ == "__main__":
    main()

This editorial was generated by gpt-6-astra-medium.