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
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,
We want the average speed to be equal to Y, so putting these into the equation, we obtain
Cross-multiplying and simplifying, this gives
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
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))