This page is still under construction.

Parts of this page are still being built. What you see may change.

Flowerpot

Time limit1sMemory limit128 MB

Summary
Find the minimum width interval on the x axis that captures raindrops whose heights differ by at least D.
Level

Medium6 of 10

Topics
Two pointers, Sliding window, Sorting
Solved
No attempts yet

Problem

Farmer John has been having trouble making his plants grow and needs your help to water them properly. You are given the locations of NN raindrops (1≤N≤100,0001 \le N \le 100{,}000) in the 2D plane, where yy represents the vertical height of the drop and xx represents its location along a 1D number line.

Each drop falls downward (toward the x axis) at a rate of 11 unit per second. You would like to place Farmer John's flowerpot of width WW somewhere along the x axis so that the difference in time between the first raindrop to hit the flowerpot and the last raindrop to hit the flowerpot is at least DD (so that the flowers in the pot receive plenty of water). A drop of water that lands exactly on the edge of the flowerpot counts as hitting it.

Given the value of DD and the locations of the NN raindrops, compute the minimum possible value of WW.

Input

  • Line 1: Two space-separated integers, NN and DD (1≤D≤1,000,0001 \le D \le 1{,}000{,}000).
  • Lines 2 to N+1N+1: Line i+1i+1 contains the space-separated coordinates xx and yy of raindrop ii. Each value is in the range 00 to 1,000,0001{,}000{,}000.

Output

  • Print a single integer: the minimum possible width of the flowerpot. Print −1-1 if it is not possible to build a flowerpot wide enough to capture rain for at least DD units of time.

Hint

The time it takes a raindrop to reach the x axis equals its height yy. So among the drops the flowerpot catches, the difference between the largest yy and the smallest yy must be at least DD. For example, if the raindrops are at (6,3)(6,3), (2,4)(2,4), (4,10)(4,10), and (12,15)(12,15) and rain must fall on the flowerpot for at least 55 units of time, a flowerpot of width 22 suffices: placing it from x=4x=4 to x=6x=6 catches raindrops 1 and 3, for a total rain duration of 10−3=710-3=7.

Examples1

  1. Example 1

    Input
    4 5
    6 3
    2 4
    4 10
    12 15
    
    Expected output
    2