A car moves from point $A$ to point $B$ at speed $v$ meters per second. The action takes place on the $X$-axis. At the distance $d$ meters from $A$ there are traffic lights. Starting from time $0$, for the first $g$ seconds the green light is on, then for the following $r$ seconds the red light is on, then again the green light is on for the $g$ seconds, and so on.
The car can be instantly accelerated from $0$ to $v$ and vice versa, can instantly slow down from the $v$ to $0$. Consider that it passes the traffic lights at the green light instantly. If the car approaches the traffic lights at the moment when the red light has just turned on, it doesn't have time to pass it. But if it approaches the traffic lights at the moment when the green light has just turned on, it can move. The car leaves point $A$ at the time $0$.
What is the minimum time for the car to get from point $A$ to point $B$ without breaking the traffic rules?
Input integers $l, d, v, g, r (1 ≤ l, d, v, g, r ≤ 1000, d < l)$ — the distance between $A$ and $B $(in meters), the distance from A to the traffic lights, car's speed, the duration of green light and the duration of red light.
solution
if(g*v>d)
ans = l/v // i got it
else
ceil(d/v/g+r)*(g+r)+(l-d)/v // i am not getting Please help
Example->suppose $l=5 ,d=4,v=1,g=2 ,r=1$
At $t=0$ car starts from $A $
At $t=2$ light become red but car is far away from light so no problem keep moving
At $t=3$ light becomes green again for $2$ sec (till $t=5$)
At $t=4$ light is green still and we reach at light
Note-> we have cross the traffic light don't worry
At $t=5$ we reach at point B
But correct ans = $7$ which is not minimum where I am doing wrong ?
Above approach was used by a red coder and I am including the his solution link below also.
Please help I am feeling sad I am trying to find the correct logic from 3 days.
Here you people are my last hope.
Problem linkproblem b
The car can move withouth any restriction until the traffic light so if we call $x$ the position of the car on the segment $AB$:
$$t[x\leq d]=\frac{d}{v}$$
Now we'll call $g+r=T$ period and define the quantity:
$$\alpha=t[x\leq d]\bmod T$$
Clearly if $\alpha\leq g$ the car can pass and the searched time $t$ is simply:
$$t=\frac{l}{v}$$
If $\alpha> g$ then the car must stop for $T-\alpha$ seconds so:
$$t=\frac{l}{v}+(T-\alpha)$$
In general rewriting it:
$$ t(l,d,v,g,r)= \left\{ \begin{array}{c} \frac lv \ \ \ \text{ if } \ \ \alpha \leq g \\ \frac{l}{v}+(T-\alpha) \ \ \ \text{ if } \ \ \alpha > g \end{array} \right.$$
$$ \Rightarrow \left\{ \begin{array}{c} \frac lv \ \ \ \text{ if } \ \ \frac dv \bmod (g+r) \leq g \\ \frac{l}{v}+\left[T-\left(\frac dv \bmod (g+r)\right)\right] \ \ \ \text{ if } \ \ \frac dv \bmod (g+r) > g \end{array} \right. $$
We can write a more simple expression using Heaviside step function
$$\frac{l}{v}+\left[T-\left(\frac dv \bmod (g+r)\right)\right]H\left(\left(\frac dv \bmod (g+r)\right)- g\right)$$
So the correct answer is $5$, the error in the program is that $ g*v>d$ is not a necessary condition because there isn't just one cycle of $g,a$ in the traffic light, the correct condition should be as said:
$$\frac dv \bmod (g+r) \leq g$$
That is equivalent to yours if and only if:
$$\frac dv<(g+r)$$
Which is not always true. In your case especially:
$$\frac 41<2+1 \Rightarrow 4<3$$
And this is why it doesn't work
:)