-2

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.

One Lyner
  • 1,964
  • 1
  • 6
  • 8
Anon
  • 381
  • 1
  • 2
  • 13
  • Can you fix the formatting? – Pham Trung Oct 18 '19 at 04:35
  • I can't understand why it's the software is not recognizing latex code – Anon Oct 18 '19 at 04:38
  • https://meta.stackexchange.com/questions/30559/latex-on-stack-overflow , latex is not supported on stackoverflow, only on mathoverflow – Pham Trung Oct 18 '19 at 05:28
  • 1
    Hint: this is basically the half plane intersection problem (consider the lines `y = S[i] + V[i]*x`), which can be seen as the dual of convex hull. There are many different `n log n` algorithms for this, I'll let you google for them :) – One Lyner Oct 18 '19 at 12:46

1 Answers1

0

Consider you have two subproblem solved. You have already computed times where winner are changing for your subproblems:

0-----t0------------t1----t2-----------t3------
0--t4-----------t5------------t6---------------

Now to merge this: each time an event occurs, compare the current winner from the two lists, check their crossing time, if it's in the current interval add it to the event list.

The proof that this gives O(n log n) complexity is the same as the classic merg sort.

One Lyner
  • 1,964
  • 1
  • 6
  • 8