This is a problem from a past exam paper:
Let there be $n$ trains X_1,X_2 ... X_n
all which run along parallel tracks. Train $i$ starts from position S[i] >= 0
and runs at constant speed V[i]
. A train is said to be a winner if there exists a time interval [t_1,t_2]
with t_2 - t_1 < delta
where the train is ahead of all other trains. You need to output all the winner trains. Design an O(n log n)
algorithm for this.
As a O(n log n)
is required, I was thinking of some divide and conquer approach, but couldn't find the appropriate combine subroutine.