Problem Statement
You are given two lists of integers, T
and A
, where:
t[i]
represents the launch time of the i-th missile.a[i]
represents the position where you need to be to watch the i-th missile launch.
Starting at position 0
, you can move up to X
units per second. Your objective is to maximize the number of launches you can watch. You can watch a launch if you are at the required position a[i]
exactly at the time t[i]
.
Input Format
The first line contains two integers N
and X
—the number of missile launches and your maximum speed.
The second line contains N
integers t1, t2, ..., tn
— the times of the launches in the mission in increasing order.
The third line contains N
integers a1, a2, ..., an
— the positions along the boundary where you need to be to watch each launch closely.
Output Format
Print the maximum number of launches that you can watch closely.
Constraints
1<=N<=2*1e5
1<=X<=1e6
1<=t[i]<=1e9
-1e9<=a[i]<=1e9
Sample Testcase 1
Input
1 2
3
7
Output
0
Explanation
He cannot reach any missile launch positions on time, so he can't watch any missile attacks.
Sample Testcase 2
Input
3 2
5 10 15
7 17 29
Output
2
Explanation
- Initial Position: Start at position
0
. - At time 5: Go to
7
with speed2
for3.5
seconds to reach there just in time to watch the missile launch. - At time 10: Immediately move to
17
from7
, traveling at your maximum speed of2
units per second, arriving precisely as the second launch occurs. - Therefore, the maximum number of launches you can monitor closely is
2
.
Auto comment: topic has been updated by Ranchoddas_Chanchad (previous revision, new revision, compare).
Disclaimer: not tested
Since we start at position 0 we can immediately remove any launches that are not reachable at all, then we can pretend we can start anywhere
Being able to move X distance per second is equivalent to being able to move 1 per second and all time values are multiplied by X, I'll describe how to solve the problem if X=1
Plot the points on a graph where x-axis is time and y-axis is position. Imagine for point (t1, a1) the region of all possible launches you could visit after this one. Its boundaries should form a "<" shape going out of its right. Then, the problem is to find the maximum length subsequence such that the "<"-shaped region for each point contains the one after it
After rotating the graph 45 degrees so the boundaries are horizontal/vertical instead of diagonal, this problem can easily be solved with a sweepline approach
Yupp! It's cool I think it will work as evry case is being covered. And yeah that's a nice way to approach this problem geometrically.... actually what I was thinking was too complicated as I never thought like we can rotate the axes so some sort of fenwick tree or treaps were must to solve it but now...thanks a lot man!