#P7344. 【DSOI 2021】积木
【DSOI 2021】积木
Description
“Now, what you have are some unit cubes with side length . And I, the owner of the game, will tell you the front view and the left view of the final 3D shape you build. You think I want you to build exactly that shape? Don’t panic, the game is far from that simple. In the front view, I will hide some columns, so you cannot observe their heights.
“To make the game more interesting, I rule that the heights of those columns you cannot observe are arbitrary—this can be decided by you.
“I suppose, my dear player, you have known since primary school that a shape cannot be uniquely determined with only the front view and the left view, not to mention that part of the front view is hidden by me. Therefore, let be the number of blocks that can be used to build a shape consistent with what I give. Please tell me: at most, how many integers can satisfy my requirements?” (That is, you should first decide the values of the unknown columns yourself, and then compute based on that choice. You need to choose the heights so that the number of possible values of is maximized.)
Input Format
This problem contains multiple test cases.
The first line contains an integer , the number of test cases.
For each test case:
The first line contains two integers , representing the length and width of the final block shape.
The second line contains integers. The -th integer is the height of blocks seen in the -th column from left to right in the front view. In particular, if , then the height of this column is arbitrary.
The third line contains integers. The -th integer is the height of blocks seen in the -th column from back to front in the left view. It is guaranteed that there is no column with unknown height.
Output Format
For each test case, output one integer per line, representing the maximum number of integers that can satisfy the requirements. In particular, if there is no integer that satisfies the conditions, output .
2
3 2
1 2 1
2 2
2 2
-1 2
2 2
3
5
Hint
[Sample Explanation]
For the first test case:
The feasible values of are .
For each value of , one possible construction is as follows (the top view is shown; the numbers indicate the height of that cell):

For the second test case:
When the heights of the unknown columns are set to , the number of valid values is maximized. There are such values: .
[Constraints and Limits]
This problem uses bundled testdata. You must pass all test points in a Subtask to obtain the score for that Subtask.
| Subtask | Score | Special Property | |
|---|---|---|---|
| 1 | 7pts | None | |
| 2 | 8pts | The number of in array is at most | |
| 3 | Guarantee | ||
| 4 | 12pts | ||
| 5 | 7pts | Guarantee all are equal | |
| 6 | 8pts | ||
| 7 | 15pts | None | |
| 8 | 35pts |
For of the testdata, , , , and .
The input size of this problem is large. Please use a fast input method.
Translated by ChatGPT 5
京公网安备 11011102002149号