Erdős-Szekeres (Large)
Time limit5sMemory limit512 MB
Given the per-position increasing and decreasing subsequence lengths, print the lexicographically smallest permutation of 1 to N that produces them.
Problem
is a sequence that uses each of the numbers through exactly once. An increasing subsequence of is a set of elements picked in order that grows from left to right, and a decreasing subsequence is one that shrinks from left to right. For example, is an increasing subsequence of .
Almost 80 years ago Paul Erdős and George Szekeres proved the following. Every such of length has an increasing subsequence of length at least or a decreasing subsequence of length at least . For example, has the decreasing subsequence of length 4.
To demonstrate the theorem in a combinatorics class, two values were computed for every .
- : the length of the longest increasing subsequence that has as its largest element, that is, the longest increasing subsequence ending at .
- : the length of the longest decreasing subsequence that has as its largest element, that is, the longest decreasing subsequence starting at .
The proof turns on the pair being different for every , which forces or to be at least for some . Here are both values for the sequence above.
Only and were kept, and the original sequence was forgotten. Given and , reconstruct .
is the numbers through in some order. If several sequences fit, print the lexicographically smallest one. That means is as small as possible, and if several candidates remain then is as small as possible, and so on.
Input
The first line contains the number of test cases, . test cases follow, each on three lines.
The first line of a test case contains one integer . The second line contains positive integers separated by spaces, which are . The third line contains in the same format.
Limits
- Every test case admits at least one valid .
Output
For each test case, print one line containing Case #x: followed by in order, separated by spaces. Here is the test case number starting from 1.