LONGDRIVE - Editorial

PROBLEM LINK:

Practice
Contest: Division 1
Contest: Division 2
Contest: Division 3
Contest: Division 4

Author: notsoloud
Tester: tabr
Editorialist: iceknight1093

DIFFICULTY:

TBD

PREREQUISITES:

None

PROBLEM:

After 10 hours of driving, Chef’s average speed is X kilometers per hour.
He wants his average speed to be Y kilometers per hour.
If Chef can drive at a maximum speed of 100 kilometers per hour, how many hours will he need to drive to achieve this?

EXPLANATION:

Since X\lt Y, it’s optimal for Chef to drive at 100 kilometers per hour to make his average speed reach Y as quickly as possible.
Now, this turns into an algebra problem.

Recall that

\text{Average speed} = \frac{\text{Total distance covered}}{\text{Total time taken}}

Since Chef’s current average speed is X, and he has driven for 10 hours, his total distance covered so far is 10\cdot X km.

Now, suppose Chef drives for H hours at a speed of 100 km/h.
Then,

\text{Distance} = 10\cdot X + 100\cdot H \\ \text{Time} = 10 + H

We want the average speed to be equal to Y, so putting these into the equation, we obtain

Y = \frac{10\cdot X + 100\cdot H}{10 + H}

Cross-multiplying and simplifying, this gives

H = \frac{10\cdot (X-Y)}{Y - 100}

This is the number of hours that Chef needs to drive at 100 km/hr to make his average speed exactly Y.
Since we want the answer to be an integer, and this quantity may not be an integer, we take the ceiling of it - i.e, the answer is

\left\lceil\frac{10\cdot (X-Y)}{Y - 100}\right\rceil

If you dislike math, the constraints are also small enough to allow for a brute-force solution.
While the average speed is \lt Y, increase the number of hours by 1 and the distance traveled by 100, and then recompute the average speed.

TIME COMPLEXITY:

\mathcal{O}(1) per testcase.

CODE:

Editorialist's code (Python)
import math
for _ in range(int(input())):
    x, y = map(int, input().split())
    ans = 10*(x-y)
    ans /= (y - 100)
    print(math.ceil(ans))