865. The Best Pair Under a Distance Cap
n points are given as pairs (x, y), listed in strictly increasing order of x.
For every pair of points i < j whose horizontal gap satisfies x[j] - x[i] <= k, the pair is
worth
y[i] + y[j] + ( x[j] - x[i] )
Print the largest value over all such pairs. At least one valid pair is guaranteed to exist.
k = 1, points (1,3) (2,0) (5,10) (6,-10) -> 4
k = 3, points (0,0) (3,0) (9,2) -> 3
In the first example only neighbouring points are within reach, and (1,3) with (2,0) gives
3 + 0 + 1 = 4.
## Splitting the expression so one point can be summarised
The expression mixes both points, which is why the obvious double loop looks necessary, and that loop is too slow. Regroup
it so that everything about i sits in one bracket:
y[i] + y[j] + x[j] - x[i] = ( y[i] - x[i] ) + ( y[j] + x[j] )
The second bracket depends only on j. So when j is fixed, the best partner is whichever
earlier point maximises y[i] - x[i], subject to x[j] - x[i] <= k.
That regrouping is the entire idea, and it is available only because the points are sorted by x,
which lets |x[j] - x[i]| be written without the absolute value.
## The window is a value range, not an index range
The admissible i are those with x[i] >= x[j] - k. Since x increases, they form a contiguous
suffix of the points before j, so it IS a sliding window, but its left edge moves according to
the x values rather than by a fixed count of positions.
## The deque
Hold indices in a deque, with y - x decreasing from front to back.
for each j:
while the deque is non-empty and x[j] - x[front] > k: drop the front
if the deque is non-empty:
best = max( best, y[front] - x[front] + y[j] + x[j] )
while the deque is non-empty and y[back] - x[back] <= y[j] - x[j]: drop the back
push j at the back
Dropping from the back is the domination argument: an earlier index with a smaller or equal
y - x is worse on value and expires sooner, so it can never be the answer to any query that j
cannot answer. Dropping from the front is expiry, which is the reason this needs two ends rather
than one.
## The order of operations
Expire the front, then read, then push. Pushing j before reading would let a point pair with
itself, which scores 2 * y[j] and is not a legal pair.
## What to be careful about
The answer may be negative, so no accumulator may start at 0; track whether any pair has been seen, or seed from the first valid pair.
The gap test is <=, so points exactly k apart are allowed. The input where every gap is
exactly k is the one that separates <= from <.
Coordinates reach 10^6 in absolute size, so every intermediate value stays small, but the
comparison must be on y - x rather than on y alone.
## Related problems
Problem 863 uses the same deque with a window measured in positions rather than in coordinate distance. Problem 193 is the plainest form of the same structure.
## Cost
Time O(n): each index enters and leaves the deque once. Space O(n).
Constraints - `2 ≤ n ≤ 100000` - `1 ≤ k ≤ 2 * 10^6` - `-10^6 ≤ x, y ≤ 10^6`, with `x` strictly increasing - at least one valid pair is guaranteed
Input
The first line contains two integers n and k.
Each of the next n lines contains two integers x and y, the points in strictly increasing order of x.
Output
Print one integer alone on a line: the largest value of y[i] + y[j] + x[j] - x[i] over pairs i < j whose horizontal gap is at most k. Print no other words.
Hints
Four rungs, in order. The last two open once you have submitted an attempt. A wrong one counts.