Honeycomb Research
Time limit1sMemory limit512 MB
Given a honeycomb grid with blocked cells, place one sensor and as many non-interfering devices as possible, then report the device count.
- Level
Medium7 of 10
- Topics
- Graph, Brute force, Dynamic programming
- Solved
- No attempts yet
Problem
Albert studies honeycombs and honeybees as a hobby. As is well known, each cell of a honeycomb is hexagonal. For convenience, the honeycomb Albert studies is a hexagonal grid with rows and columns. The figure below shows the case , .

Albert wants to place tiny devices in some of the empty cells to observe honeybee behavior. A device occupies one cell, and each cell holds at most one device. Devices that are too close can interfere with each other. Specifically, a device does not interfere with the (at most) two neighboring cells in the same column, but it does interfere with the (at most) four neighboring cells in the other columns.
For example, suppose devices are placed at cells (1, 2), (2, 7), and (5, 6), as in the figure above.
- Cell (1, 2) has five neighbors. Devices cannot be placed at (1, 1), (2, 1), (1, 3), or (2, 3) because they may interfere. Cell (2, 2) is adjacent to (1, 2) but does not interfere with it.
- Cell (2, 7) has six neighbors. Cells (1, 6), (1, 8), (2, 6), and (2, 8) may interfere. Devices can still be added at (1, 7) and (3, 7).
- Cell (5, 6) has three neighbors. Cells (5, 5) and (5, 7) may interfere. A device can still be added at (4, 6).
With no other constraints, the most devices that avoid interference are placed in odd-numbered columns (1, 3, ...). Albert has proven that this is the maximum. However, Albert has two more concerns besides interference.
First, not every cell is empty. K cells contain cocoons instead of larvae, so no device can be placed there. The position of the i-th cell with a cocoon is .
Second, Albert wants to test a new sensor of the same size. The sensor also occupies one cell. It cannot be placed on a cocoon cell or on a cell that holds a device. The sensor interferes with every other device in its column. Cells in other columns do not interfere with it, even when adjacent. Only one sensor exists, and it must be installed.
For example, consider a grid with , , , , and .
- In the second figure, the sensor is at (2, 2), and devices are at (2, 1), (1, 3), and (2, 3). This is possible.
- In the third figure, the sensor is at (2, 1), and devices are at (3, 2), (1, 3), and (2, 3). This is possible.
- In the rightmost figure, the sensor is at (2, 3), and devices are at (2, 2), (3, 2), and (1, 3). This layout fails because the sensor interferes with the device at (1, 3) in the same column. The device at (2, 2) is in a different column, so it does not interfere with the sensor.
In this example, the best result is 3 devices besides the sensor.
Another example uses , , , , and .
- The middle figure places the sensor at (2, 2) and devices at (1, 1), (3, 1), (1, 4), and (2, 4). This is possible.
- The right figure places the sensor at (2, 3) and devices at (1, 1), (2, 2), (1, 4), and (2, 4). This is possible.
In this example, the best result is 4 devices besides the sensor. Other placements also reach 4.
Given , , , , and , find the maximum number of devices that can be installed while satisfying all the conditions above.
Input
The first line contains the number of test cases .
The first line of each test case contains , , and , separated by spaces. If , the input for that test case ends on this line. If , the second line contains the integers of separated by spaces, and the third line contains the integers of separated by spaces.
Output
For each test case, print the answer on its own line.
Constraints
- If , then for every with :
- The pairs are distinct (no duplicate coordinates are given).