PROBLEM LINK:
Practice
Contest: Division 1
Contest: Division 2
Contest: Division 3
Contest: Division 4
Author: iceknight1093
Tester: sushil2006
Editorialist: iceknight1093
DIFFICULTY:
Cakewalk
PREREQUISITES:
None
PROBLEM:
Alice has an N\times M bar of chocolate.
She can cut this either vertically or horizontally, at most once - then keep one piece and give away the other to Bob.
How much of the chocolate can she keep with her while ensuring Bob gets at least K units?
EXPLANATION:
Suppose Alice makes a vertical cut, say at distance i.
The chocolate is then split into two parts with dimensions i\times N and (M-i)\times N.
If i\times N \geq K Alice can give this part to Bob and keep (M-i)\times N for herself, otherwise this cut isn’t valid to satisfy Bob.
Try every vertical cut (i.e. every value of i from 0 to M) and take the best value among them for Alice.
Similarly, try every horizontal cut - which will split the rectangle into M\times i and M\times (N-i) - and take the best value for Alice among them.
The final answer is the maximum of both these values.
TIME COMPLEXITY:
\mathcal{O}(N+M) per testcase.
CODE:
Editorialist's code (PyPy3)
for _ in range(int(input())):
n, m, x = map(int, input().split())
ans = 0
for i in range(n+1):
if i*m >= x: ans = max(ans, (n-i)*m)
for i in range(m+1):
if i*n >= x: ans = max(ans, (m-i)*n)
print(ans)