ADD1234 - Editorial

PROBLEM LINK:

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

Author: raysh07
Tester: sushil2006
Editorialist: iceknight1093

DIFFICULTY:

TBD

PREREQUISITES:

None

PROBLEM:

You have X pieces of paper with 1 written on them, Y pieces with 2, and Z pieces with 3.
How many pairs of pieces can you make such that the sum of the written numbers is 4?

EXPLANATION:

To obtain a sum of 4, we must either pair 2 with 2 or 1 with 3.

There are Y pieces of paper with 2 on them, and we’ll use up two at a time to form a sum of 4.
So, we can make \text{floor}(\frac{Y}{2}) such pairs.
Here, \text{floor}(\frac{Y}{2}) refers to the largest integer not exceeding \frac{Y}{2}, i.e. \frac{Y}{2} rounded down.

Next, we have X ones and Z threes.
Each sum of 4 requires one of each type.
So, if X \le Z we can make X pairs before we run out of ones; and if X \ge Z we can make Z pairs before we run out of threes.
The number of pairs here is thus \min(X, Z).

The final answer is hence

\text{floor}(\frac{Y}{2}) + \min(X, Z)

TIME COMPLEXITY:

\mathcal{O}(1) per testcase.

CODE:

Editorialist's code (PyPy3)
for _ in range(int(input())):
    x, y, z = map(int, input().split())
    print(y//2 + min(x, z))