D - 夏休みの集中特訓 / Intensive Summer Vacation Training Editorial by admin Editorial for printing
Gemini 3.8 Flash (High)Overview
Given a sequence of positive integers \(A\) of length \(N\), this problem asks us to find the number of contiguous subarrays \((l, r)\) such that their length is at most \(K\) and the sum of their elements is at least \(X\). By utilizing the property that all elements are positive, we can solve this problem efficiently using the two-pointer technique.
Analysis
Naive Approach and Its Limitations
Consider checking whether the condition is satisfied for all intervals \((l, r)\) (\(1 \le l \le r \le N\)). There are approximately \(\frac{N^2}{2}\) possible intervals. Since \(N \le 10^6\) in this problem, the total number of intervals is about \(5 \times 10^{11}\). Checking every interval would far exceed the execution time limit (typically around 2 seconds), resulting in TLE (Time Limit Exceeded). Therefore, we need to count valid intervals more efficiently.
Utilizing Monotonicity
A key point in this problem is that all elements are positive (\(A_i \ge 1\)). Since every element is positive, if we fix the starting position \(l\), the interval sum increases monotonically as the ending position \(r\) extends to the right.
Therefore, for a fixed \(l\), let \(r_{\min}\) be the minimum right endpoint such that the sum is at least \(X\): - When \(r < r_{\min}\): The sum is less than \(X\) (does not satisfy the condition) - When \(r \ge r_{\min}\): The sum is at least \(X\) (satisfies the condition)
Furthermore, according to the problem statement, the length must be at most \(K\), so the upper bound for \(r\) is \(r_{\max} = \min(N, l + K - 1)\). Thus, for a fixed \(l\), the range of valid right endpoints \(r\) is \(r_{\min} \le r \le r_{\max}\), and the count can be determined immediately as \(\max(0, r_{\max} - r_{\min} + 1)\).
Speeding Up with the Two-Pointer Technique
When the starting position \(l\) is advanced by one (\(l \to l + 1\)), \(A_l\) is removed from the interval, causing the sum to decrease. Consequently, the minimum right endpoint \(r_{\min}'\) for \(l+1\) will never move to the left of the previous \(r_{\min}\) (\(r_{\min}' \ge r_{\min}\)).
By exploiting this monotonicity, we can apply the two-pointer technique, advancing the left endpoint \(l\) from \(1\) to \(N\) while also moving the right endpoint \(r\) only to the right. As a result, each element is visited at most twice (once added by \(r\) and once removed by \(l\)), allowing us to compute the total count in \(O(N)\) time.
Algorithm
We describe the algorithm using 0-based indexing (\(0\) to \(N-1\)).
- Initialize the variable
current_sum(current interval sum) to \(0\), the right pointerrto \(0\), and the answer counteransto \(0\). - Iterate the left pointer
lfrom \(0\) to \(N-1\):- While
current_sum < Xandr < N, addA[r]tocurrent_sum(current_sum += A[r]) and advancerby \(1\). - Upon exiting the loop:
- If
current_sum >= X, the minimum right endpoint index where the sum is at least \(X\) ismin_r = r - 1. - Due to the length limit, the maximum allowed right endpoint is
max_r = min(N - 1, l + K - 1). - If
min_r <= max_r, there aremax_r - min_r + 1valid right endpoints, so add this amount toans. - If
rreaches the end whilecurrent_sum < X, it is impossible to achieve a sum of at least \(X\) for any subsequentl, so terminate the search.
- If
- In preparation for the next iteration, remove the current left element from the interval by executing
current_sum -= A[l].
- While
- Output the final value of
ans.
Complexity
- Time Complexity: \(O(N)\)
- Both the left pointer
land the right pointerrincrease monotonically from \(0\) to \(N\), meaning the algorithm finishes in at most \(2N\) steps across the entire loop.
- Both the left pointer
- Space Complexity: \(O(N)\)
- Only memory to store the sequence \(A\) of length \(N\) is required.
Implementation Notes
Beware of Overflow:
- The sum of \(A_i\) can reach up to \(N \times \max(A_i) = 10^6 \times 10^9 = 10^{15}\). In addition, the maximum possible answer is \(\frac{N(N+1)}{2} \approx 5 \times 10^{11}\), which will overflow a 32-bit integer type (
int). Use a 64-bit integer type (long longin C++) for the interval sum and the answer counter.
- The sum of \(A_i\) can reach up to \(N \times \max(A_i) = 10^6 \times 10^9 = 10^{15}\). In addition, the maximum possible answer is \(\frac{N(N+1)}{2} \approx 5 \times 10^{11}\), which will overflow a 32-bit integer type (
Index Handling:
Notice that immediately after exiting the
whileloop,rhas been incremented by \(1\) right after the sum became at least \(X\), meaning the actual minimum right endpoint isr - 1.Source Code
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
long long k, x;
if (!(cin >> n >> k >> x)) return 0;
vector<long long> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
long long ans = 0;
long long current_sum = 0;
int r = 0;
for (int l = 0; l < n; ++l) {
while (r < n && current_sum < x) {
current_sum += a[r];
r++;
}
if (current_sum >= x) {
int min_r = r - 1;
long long max_r = min((long long)n - 1, (long long)l + k - 1);
if (min_r <= max_r) {
ans += (max_r - min_r + 1);
}
} else {
break;
}
current_sum -= a[l];
}
cout << ans << "\n";
return 0;
}
This editorial was generated by gemini-3.8-flash-high.
posted:
last update: