Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
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.

What I have tried:

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 - Codeforces[^]


red coder solution(same as above mentioned)
https://codeforces.com/contest/29/submission/31114802

Note-> the above accepted solution giving 7 as output But I think it should be 5.So this can't be wrong since codeforces accepted it.
Posted
Updated 10-May-19 5:08am
Comments
Richard MacCutchan 24-Apr-19 10:21am    
This is not really a programming issue, rather it is one of mathematics.
kavinderrana121 24-Apr-19 11:31am    
Sir Please help I request
Richard MacCutchan 24-Apr-19 11:47am    
Help with what? This is mathematics, not programming.
Christian Graus 25-Apr-19 0:15am    
Tell your teacher you're lost, so he knows

1 solution

You fail to distinguish the cases where the traffic light has turned red and green again a few times before the car arrives. The car may travel at full speed the whole time if the traffic light is green at the time the car arrives, even if it has turned red intermittently while the car was still approaching.

Also, I'm not sure that the expression ceil(d/v/g+r) you posted evaluates in the way you expect: for one, I suspect you meant to divide by (g+r), not divide by g and then add r. Second, the order of evaluation of (d/v/g) is not obvious: it could be ((d/v)/g) or (d/(v/g)) - which did you intend?

I did not bother to check if either interpretation of your formula would give you the correct result. Instead I will point out how to write code that directly follows the description to the problem and is easy to read and understand:

Step 1: determine the time it takes the car to arrive at the traffic light:
C++
time_to_traffic_light = d/v;


Step 2: determine the number of green-red cycles that have passed before then (the time for one cycle is g+r):
C++
lights_cycle_time = g + r;
number_of_cycles = floor(time_to_traffic_light/lights_cycle_time);


Step 3: determine the time that has passed since the end of the last cycle:
C++
current_cycle_time = time_to_traffic_light - number_of_cycles*lights_cycle_time;


Step 4: check whether the traffic light shows green:
C++
if (current_cycle_time < g) /* use direct travel time */


Step 5: if showing red, determine for how long the car must wait (until the end of the cycle):
C++
else {
wait_time = lights_cycle_time - current_cycle_time;
/* now add this waiting time to the direct travel time */
}

This may not be the shortest code to arrive at the solution, but unless you're participating in a short-code-contest this should never be a criterion. ;-)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900