PROBLEM LINK:
Practice
Contest: Division 1
Contest: Division 2
Contest: Division 3
Contest: Division 4
Author: raysh07
Tester: sushil2006
Editorialist: iceknight1093
DIFFICULTY:
Cakewalk
PREREQUISITES:
None
PROBLEM:
Chef has A units of orange syrup and B units of water. Mixing these in a 1:2 ratio will result in orange juice.
How many units of orange juice can be made, if only integer units of each liquid can be mixed?
EXPLANATION:
If Chef uses x units of orange syrup, he must use 2x units of water to maintain the 1:2 ratio.
Because A, B \leq 100, there aren’t too many different choices for what x can be.
So, one simple solution is to just try every x from 1 to A, and for this x, check if 2x \leq B.
If 2x \leq B is satisfied, Chef can make (x + 2x) = 3x units of orange juice; so print the maximum such value.
The largest x that satisfies this condition can also be found mathematically: we want x \leq A and 2x \leq B to hold, and the second equation translates to x \le \frac B 2.
So, the largest valid x is \min(A, \frac B 2), rounded down to the nearest integer.
Once this x is known, the answer is 3x.
TIME COMPLEXITY:
\mathcal{O}(1) per testcase.
CODE:
Editorialist's code (PyPy3)
for _ in range(int(input())):
a, b = map(int, input().split())
print(min(a, b//2)*3)