Interesting Sequence
Time limit1sMemory limit128 MB
For each starting index, find the maximum even-length window whose first half sum and second half sum are both at most S, using binary search and prefix sums.
- Level
Medium6 of 10
- Topics
- Binary search, Prefix sum, Two pointers
- Solved
- No attempts yet
Problem
Consider a sequence of length 2K, where K is a positive integer. If both the sum of the first K elements and the sum of the last K elements are at most S, call the sequence interesting.
Given a sequence A of length N, find, for every starting position, the maximum length of a contiguous subsequence that starts there and is interesting.
Input
The first line contains N and S. (2 <= N <= 100,000, 1 <= S <= 2 * 10^9)
Each of the next N lines contains one element of the sequence A. Every element is positive, and the sum of all elements of A is at most 2 * 10^9.
Output
Print N lines. On the i-th line, print the length of the longest interesting contiguous subsequence that starts at the i-th element. If no such subsequence exists, print 0.