Rank and File (Large)
InterviewTime limit5sMemory limit512 MB
Given all but one of the rows and columns of a sorted grid, find the missing list of heights in increasing order.
- Level
Easy3 of 10
- Topics
- Implementation, Hash map, Sorting
- Solved
- No attempts yet
Problem
When Sergeant Argus's army assembles for drilling, the soldiers stand in the shape of an square grid, with exactly one soldier in each cell. Each soldier has a certain height.
Argus believes that he must keep an eye on all of his soldiers at all times. Since he likes to look at the grid from the upper left, he requires that:
- Within every row of the grid, the soldiers' heights are in strictly increasing order from left to right.
- Within every column of the grid, the soldiers' heights are in strictly increasing order from top to bottom.
No two soldiers in the same row or column may have the same height, but several soldiers in the grid as a whole may have the same height.
Since soldiers sometimes train separately with their row or their column, Argus has asked you to make a report consisting of lists of the soldiers' heights: one for each row (in left-to-right order) and one for each column (in top-to-bottom order). While surveying the soldiers you only had small pieces of paper, so you wrote each list on a separate piece. On the way back to your office, a loud bugle blast startled you, you dropped all the pieces, and the wind blew one away before you could recover it! The remaining pieces are now in no particular order, and you cannot remember which lists are rows and which are columns, since you did not write that down.
Argus will make you do hundreds of push-ups if you hand in an incomplete report. Find the missing list.
Input
The first line of the input gives the number of test cases, . test cases follow. Each consists of one line with an integer , followed by lines of integers each: the lists you have, as described in the statement. It is guaranteed that these lists are all but one of the rows and columns of a grid that satisfies the conditions in the statement.
Limits
- Every height is between and , inclusive.
- The integers on each line are in strictly increasing order.
- It is guaranteed that a unique valid answer exists.
Output
For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the integers of the missing list in strictly increasing order, separated by spaces.
Hint
In the sample case, the grid must be either this:
1 2 3
2 3 4
3 5 6
or this:
1 2 3
2 3 5
3 4 6
In either case, the missing list is 3 4 6.