D - 夏休みの集中特訓 / Intensive Summer Vacation Training Editorial by admin Editorial for printing
gpt-6-astra-mediumOverview
We want to find the number of contiguous subarrays of length at most \(K\) whose sum of elements is at least \(X\). Since all \(A_i\) are positive, we can use the two-pointer technique to count the number of valid left endpoints for each right endpoint.
Analysis
Checking All Subarrays Is Too Slow
Brute-forcing all pairs of left and right endpoints takes \(O(NK)\) time even if we restrict the length to at most \(K\). When \(K=N\), this becomes \(O(N^2)\), which is too slow for \(N \le 10^6\).
Using prefix sums allows us to compute range sums in \(O(1)\) time, but it does not reduce the number of subarrays we need to check.
Fixing the Right Endpoint Yields a Contiguous Range of Valid Left Endpoints
From here on, we use \(0\)-based indexing to match the code implementation.
Fix the right endpoint to right. Since all \(A_i\) are positive, moving the left endpoint to the right strictly decreases the subarray sum.
Therefore, the left endpoints whose sum is at least \(X\) can be represented using a boundary left as:
\(0,1,\ldots,\text{left}-1\)
In other words, we can maintain the boundary such that:
- If the left endpoint is less than
left, the subarray sum is at least \(X\). - If the left endpoint is greater than or equal to
left, the subarray sum is less than \(X\).
As right advances to the right, the subarray sum increases, which means this boundary never moves back to the left. Leveraging this monotonicity, we can efficiently update the boundary using the two-pointer technique.
Taking the Intersection with the Length Constraint
Let the left endpoint be \(s\). The condition on the length is
\(\text{right}-s+1 \le K\)
which rearranges to
\(s \ge \text{right}-K+1\)
Taking the array bounds into account, the earliest valid left endpoint that satisfies the length condition is
\(\text{earliest}=\max(0,\text{right}-K+1)\)
The left endpoints that satisfy both the sum and the length conditions are
\(\text{earliest},\text{earliest}+1,\ldots,\text{left}-1\)
so the number of such endpoints is
\(\max(0,\text{left}-\text{earliest})\)
Algorithm
Let sum maintain the sum of the interval [left, right]. If left = right + 1, we treat it as an empty interval with a sum of \(0\).
- Initialize
left = 0,sum = 0, andanswer = 0. - Iterate
rightfrom \(0\) to \(N-1\), performing the following:- Add
A[right]tosum. - While
sum >= X, subtractA[left]fromsumand incrementleftby \(1\). - Compute
earliest = max(0, right - K + 1). - Add
max(0, left - earliest)toanswer.
- Add
- Output
answer.
Why the Count Is Correct After the while Loop
Just before removing the element at the left endpoint inside the while loop, the subarray sum is at least \(X\). Thus, the left endpoint at that moment satisfies the sum condition.
On the other hand, once the loop terminates, the sum of the interval [left, right] is less than \(X\). Moving the left endpoint further to the right will only decrease the sum, so none of the subsequent left endpoints can satisfy the condition either.
Additionally, any left endpoint that was valid for the previous right endpoint remains valid after adding a positive element.
Therefore, the set of left endpoints satisfying the sum condition is precisely all integers from \(0\) up to (but not including) left. By filtering this range to only count those that also satisfy the length constraint, we can count all valid subarrays ending at right without any omissions or duplicates.
Concrete Example
Consider \(A=(2,3,1,4)\), \(K=3\), \(X=5\), and let right = 3.
| Left Endpoint | Subarray Elements | Sum | Length | Satisfies Condition? |
|---|---|---|---|---|
| \(0\) | \((2,3,1,4)\) | \(10\) | \(4\) | Too long |
| \(1\) | \((3,1,4)\) | \(8\) | \(3\) | Satisfied |
| \(2\) | \((1,4)\) | \(5\) | \(2\) | Satisfied |
| \(3\) | \((4)\) | \(4\) | \(1\) | Sum is insufficient |
After the while loop terminates, we have left = 3. Furthermore,
\(\text{earliest}=\max(0,3-3+1)=1\)
Thus, the number of subarrays added to the answer is \(3-1=2\).
Complexity
- Time Complexity: \(O(N)\)
- Space Complexity: \(O(N)\)
Although there is a nested while loop, left is incremented at most \(N\) times in total throughout the entire algorithm. Since right also advances \(N\) times, the overall time complexity is \(O(N)\).
The space complexity is \(O(N)\) to store the array \(A\); the additional working space required is \(O(1)\).
Implementation Notes
Use
long longfor the subarray sum and the answer.
The subarray sum can be up to \(10^{15}\), and the answer can be up to \(\frac{N(N+1)}{2}\), both of which will overflow a 32-bit integer.The condition for the
whileloop issum >= X.
Subarrays with a sum equal to \(X\) must also be counted, so usingsum > Xwill lead to incorrect answers.leftitself is the boundary where the sum condition is NOT satisfied.
Valid left endpoints only go up toleft - 1, so the count isleft - earliest. Be careful not to add \(1\).The length constraint is handled when counting the valid endpoints.
In this implementation, the two-pointer process solely manages the sum condition, while the length condition is handled separately viaearliest.Since \(X \ge 1\), the sum of an empty subarray (\(0\)) is strictly less than \(X\). Therefore, the
whileloop will never attempt to remove elements from an empty interval.Source Code
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, K;
long long X;
cin >> N >> K >> X;
vector<long long> A(N);
for (auto& a : A) cin >> a;
long long answer = 0;
long long sum = 0;
int left = 0;
for (int right = 0; right < N; ++right) {
sum += A[right];
while (sum >= X) {
sum -= A[left++];
}
int earliest = max(0, right - K + 1);
answer += max(0, left - earliest);
}
cout << answer << '\n';
return 0;
}
This editorial was generated by gpt-6-astra-medium.
posted:
last update: