Circuit
Contest: Codeforces Round 983 (Div. 2)
Difficulty rating: 800
Tags: greedy, implementation, math, number theory

Alice has just crafted a circuit with $$$n$$$ lights and $$$2n$$$ switches. Each component (a light or a switch) has two states: on or off. The lights and switches are arranged in a way that:

- Each light is connected to exactly two switches.
- Each switch is connected to exactly one light. It's unknown which light each switch is connected to.
- When all switches are off, all lights are also off.
- If a switch is toggled (from on to off, or vice versa), the state of the light connected to it will also toggle.

Alice brings the circuit, which shows only the states of the $$$2n$$$ switches, to her sister Iris and gives her a riddle: what is the minimum and maximum number of lights that can be turned on?

Knowing her little sister's antics too well, Iris takes no more than a second to give Alice a correct answer. Can you do the same?

Input format:
Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 50$$$) — the number of lights in the circuit.

The second line of each test case contains $$$2n$$$ integers $$$a_1, a_2, \ldots, a_{2n}$$$ ($$$0 \le a_i \le 1$$$) — the states of the switches in the circuit. $$$a_i = 0$$$ means the $$$i$$$-th switch is off, and $$$a_i = 1$$$ means the $$$i$$$-th switch is on.

Output format:
For each test case, output two integers — the minimum and maximum number of lights, respectively, that can be turned on.

Constraints:
Time limit: 1.0 s
Memory limit: 256.0 MB

Note:
In the first test case, there is only one light in the circuit, and no switch is on, so the light is certainly off.

In the second test case, there is only one light in the circuit, but one switch connected to it is on, so the light is on.

In the third test case, there is only one light in the circuit, and both switches are on, so the light is off as it was toggled twice.

In the fourth test case, to have no lights on, the switches can be arranged in this way:

- Switch $$$1$$$ and switch $$$4$$$ are connected to light $$$1$$$. Since both switches are off, light $$$1$$$ is also off.
- Switch $$$2$$$ and switch $$$6$$$ are connected to light $$$2$$$. Since both switches are off, light $$$2$$$ is also off.
- Switch $$$3$$$ and switch $$$5$$$ are connected to light $$$3$$$. Both switches are on, so light $$$3$$$ is toggled twice from its initial off state, and thus also stays off.

And to have $$$2$$$ lights on, the switches can be arranged in this way:

- Switch $$$1$$$ and switch $$$2$$$ are connected to light $$$1$$$. Since both switches are off, light $$$1$$$ is also off.
- Switch $$$3$$$ and switch $$$4$$$ are connected to light $$$2$$$. Since switch $$$3$$$ is on and switch $$$4$$$ is off, light $$$2$$$ is toggled once from its initial off state, so it is on.
- Switch $$$5$$$ and switch $$$6$$$ are connected to light $$$3$$$. Since switch $$$5$$$ is on and switch $$$6$$$ is off, light $$$3$$$ is toggled once from its initial off state, so it is on.

Example input:
5
1
0 0
1
0 1
1
1 1
3
0 0 1 0 1 0
3
0 1 1 1 0 0

Example output:
0 0
1 1
0 0
0 2
1 3

Editorial:
Observe that an even number of switch toggles on the same light will not change that light's status. In other words, a light is on if and only if exactly one of the two switches connecting to it is on.
Let's denote $$$\text{cnt}_0$$$ and $$$\text{cnt}_1$$$ as the number of off switches and on switches in the circuit. We see that
• The maximum number of on lights is $$$\min(\text{cnt}_0, \text{cnt}_1)$$$: we can't achieve more than this amount since any on light decreases both $$$\text{cnt}_0$$$ and $$$\text{cnt}_1$$$ by $$$1$$$, and we can achieve this amount by matching $$$\min(\text{cnt}_0, \text{cnt}_1)$$$ pairs of one off switch and one on switch, and the rest can be matched arbitrarily.
• The minimum number of on lights is $$$\text{cnt}_0 \bmod 2$$$. Since $$$\text{cnt}_0 + \text{cnt}_1 = 2n$$$, they have the same parity. Then, we can easily see that when both $$$\text{cnt}_0$$$ and $$$\text{cnt}_1$$$ are even, we can match $$$n$$$ pairs of the same type of switches, so there are no on lights in this case. When both $$$\text{cnt}_0$$$ and $$$\text{cnt}_1$$$ are odd, we must match one on switch with one off switch to make $$$\text{cnt}_0$$$ and $$$\text{cnt}_1$$$ even, so there is one on light in this case.
The calculation of $$$\text{cnt}_0$$$ and $$$\text{cnt}_1$$$ can be easily done by a simple iteration over the switches.
Time complexity: $$$\mathcal{O}(n)$$$.