RACETIME Editorial



Author: Baban Gain
Tester: Baban Gain
Editorialist: Baban Gain




Basic Maths, High school Physics


Given the ground friction G and length of a racing track L (in meters) and points (1 for braking point, 2 for Acceleration point, 3 for steady point) of a track and Acceleration A and braking B of a car, find the time required to finish one lap.


Two forumulas should come handy to solve the problem,
v^2 - u^2 = 2as
t = (v-u) /a

Consider You are starting from point X and the next point is at Y after s meters.

Let the acceleration/declaration from X and Y is a
Then a = A (for acceleration point),
a = -B (for braking point),
a = -G (for steady point)

Then, the final velocity when reaching Y is, v = sqrt(u*u+2*a*s)
Where u=initial velocity. While starting the race, the initial velocity is 0. After reaching every point, the final velocity becomes the next segment’s initial velocity.

Now, we have u,v, and acceleration a of a segment i, we need to find the time taken.
t_i = (v_i-u_i)/a_i. Then time taken for all segments are added and printed.

total = \sum_{i=i}^{segments} t_i

However, what if the car stop while braking or when in steady before reaching the next acceleration point? It means It will never be able to finish the race. Therefore if in any segment
u*u+2*a*s is less than 0, then -1 should be printed.


Setter's Solution
import math
from math import sqrt

def get_v(u, a, s):
    v_sq = (2 * a * s) + (u ** 2)
    if v_sq < 0:
        return -1
    return math.sqrt(v_sq)

T = int(input())
for z in range(T):
    tym = 0
    last_vel = 0
    acc, breaking, ground = map(float, input().split())
    length, no_of_points = map(int, input().split())
    points = []
    # Add accelearation from starting point
    points.append((2, 0))
    for i in range(no_of_points):
        typ, dist = map(int, input().split())
        points.append((typ, dist))
    # Add end point to calculate time
    points.append((1, length))
    unable_to_finish = False
    for i in range(1, len(points)):
        typ = points[i - 1][0]
        dist = points[i][1] - points[i - 1][1]
        # print(typ, dist)
        a = 0
        if typ == 1:
            a = -breaking
        elif typ == 2:
            a = acc
            a = -ground

        v = get_v(last_vel, a, dist)
        if v<0:
            unable_to_finish = True
        x = (v - last_vel) / a
        tym += x
        last_vel = v
    if unable_to_finish:

Link to Solution