Rocket#356

Rocket (Editorial)

Written by al95ireyiz

A rocket is launched with an initial velocity and constant acceleration . Our goal is to find the first meter along the rocket's path that it will traverse in less than a given time .

As the rocket accelerates, each subsequent meter will take less time to travel.

Example

Suppose the inputs are:

To solve this, we calculate the time taken to travel each meter until we find the first one completed in under seconds.

Kinematic Equation with Initial Velocity

We use the kinematic equation for motion with constant acceleration:

S = V \times T + \frac{1}{2} A \times T^2

where:

To find the time to reach a given distance , we rearrange the equation as:

T = \frac{-V + \sqrt{V^2 + 2 \cdot A \cdot S}}{A}

This formula provides the time needed to travel a distance from the starting point.

Calculating the Time to Traverse Each Meter

To find out how long it takes to traverse each individual meter:

  1. Calculate the time t_{n-1} to reach the position at (N-1) meters.

  2. Calculate the time t_n to reach meters.

  3. The time taken to traverse the -th meter alone is:

    \Delta t_N = t_N - t_{N-1}

Writing the Program

We implement this step-by-step in C++ as follows:

  1. Set up variables for acceleration, initial velocity, and target time. Initialize a res variable to 1, representing the current meter.

  2. Calculate t_N and t_{N-1} for each meter incrementally.

  3. Check if \Delta t_N is less than . If so, we've found our answer. Otherwise, increment the meter and continue.

Full Code Example

int main() {
    double A, V, T, time=0.0, now, delta;
    int res = 1;
    cin >> A >> V >> T;
    
    while (true) {
        // Calculate time to reach the end of the current meter
        //        using formula mentioned before
        now = (-V + sqrt(V * V + 2 * A * res)) / A;
        // Time taken to traverse this meter
        delta = now - time;
        // Check if time to traverse this meter is less than target time
        if (delta < T) {
            cout << res << endl;
            return 0;
        }
        time = now;
        res++;
    }
}