#P2390. Maximizing Landmark Visits
Maximizing Landmark Visits
Maximizing Landmark Visits
Bessie is traveling on a road which can be viewed as a number line. There are \( n \) distinct landmarks, each located at coordinate \( x_i \) (\( |x_i| \le 10^5 \)). Bessie starts at the origin and has \( T \) minutes (with \( 1 \le T \le 10^9 \)) before sunset. Bessie travels at a speed of 1 unit per minute. She wants to visit as many landmarks as possible before sunset.
If Bessie chooses to visit a set of landmarks, she can plan an optimal route. Notice that an optimal strategy is to visit a contiguous block of landmarks on the left (negative side) and a contiguous block on the right (non-negative side), because skipping a closer landmark to reach a farther one cannot reduce travel time.
Let the landmarks on the left of the origin (i.e. \( x_i<0 \)) be sorted in increasing order of their absolute distance from the origin, and the landmarks on the right (i.e. \( x_i \ge 0 \)) be sorted in increasing order. Suppose Bessie decides to visit the first \( k \) landmarks on the left and the first \( j \) landmarks on the right. Then:
- If \( k=0 \), the travel time is simply \( x_j \), where \( x_j \) is the farthest visited landmark on the right.
- If \( j=0 \), the travel time is \( |x_k| \) where \( x_k \) is the landmark farthest from the origin on the left.
- If both sides are visited (i.e. \( k > 0 \) and \( j > 0 \)), the minimum travel time is given by \[ \min\Big(\text{(right farthest)} + 2\times\text{(left farthest)},\; 2\times\text{(right farthest)} + \text{(left farthest)}\Big) \le T. \] Here, the numbers represent the absolute values of the coordinates of the farthest visited landmark on that side.
Your task is to determine the maximum number of landmarks Bessie can visit within \( T \) minutes.
inputFormat
The first line contains two integers \( n \) and \( T \) (\( 1 \le n \le 5\times10^4 \), \( 1 \le T \le 10^9 \)), representing the number of landmarks and the available time in minutes.
The second line contains \( n \) space-separated integers \( x_i \) (\( |x_i| \le 10^5 \)), the coordinates of the landmarks. It is guaranteed that all coordinates are distinct.
outputFormat
Output a single integer — the maximum number of landmarks that Bessie can visit within \( T \) minutes.
sample
3 5
-2 1 3
2