Under construction

Still being built. Things may change

Erdős-Szekeres (Large)

Time limit5sMemory limit512 MB

Summary
Given the per-position increasing and decreasing subsequence lengths, print the lexicographically smallest permutation of 1 to N that produces them.
Level

Medium7 of 10

Topics
Greedy, Sorting
Solved
No attempts yet

Problem

XX is a sequence that uses each of the numbers 11 through NN exactly once. An increasing subsequence of XX 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, (5,7,8)(5, 7, 8) is an increasing subsequence of (4,5,3,7,6,2,8,1)(4, 5, 3, 7, 6, 2, 8, 1).

Almost 80 years ago Paul Erdős and George Szekeres proved the following. Every such XX of length NN has an increasing subsequence of length at least N\sqrt{N} or a decreasing subsequence of length at least N\sqrt{N}. For example, (4,5,3,7,6,2,8,1)(4, 5, 3, 7, 6, 2, 8, 1) has the decreasing subsequence (5,3,2,1)(5, 3, 2, 1) of length 4.

To demonstrate the theorem in a combinatorics class, two values were computed for every ii.

  • A[i]A[i]: the length of the longest increasing subsequence that has X[i]X[i] as its largest element, that is, the longest increasing subsequence ending at X[i]X[i].
  • B[i]B[i]: the length of the longest decreasing subsequence that has X[i]X[i] as its largest element, that is, the longest decreasing subsequence starting at X[i]X[i].

The proof turns on the pair (A[i],B[i])(A[i], B[i]) being different for every ii, which forces A[i]A[i] or B[i]B[i] to be at least N\sqrt{N} for some ii. Here are both values for the sequence above.

iiX[i]X[i]A[i]A[i]B[i]B[i]
0414
1524
2313
3734
4633
5212
6842
7111

Only AA and BB were kept, and the original sequence XX was forgotten. Given A[i]A[i] and B[i]B[i], reconstruct XX.

XX is the numbers 11 through NN in some order. If several sequences fit, print the lexicographically smallest one. That means X[0]X[0] is as small as possible, and if several candidates remain then X[1]X[1] is as small as possible, and so on.

Input

The first line contains the number of test cases, TT. TT test cases follow, each on three lines.

The first line of a test case contains one integer NN. The second line contains NN positive integers separated by spaces, which are A[0],A[1],…,A[N−1]A[0], A[1], \dots, A[N-1]. The third line contains B[0],B[1],…,B[N−1]B[0], B[1], \dots, B[N-1] in the same format.

Limits

  • 1≤T≤301 \le T \le 30
  • 1≤N≤20001 \le N \le 2000
  • Every test case admits at least one valid XX.

Output

For each test case, print one line containing Case #x: followed by X[0],X[1],…,X[N−1]X[0], X[1], \dots, X[N-1] in order, separated by spaces. Here xx is the test case number starting from 1.

Examples3

  1. Example 1

    Input
    2
    1
    1
    1
    8
    1 2 1 3 3 1 4 1
    4 4 3 4 3 2 2 1
    
    Expected output
    Case #1: 1
    Case #2: 4 5 3 7 6 2 8 1
    
  2. Example 2

    Input
    3
    1
    1
    1
    2
    1 2
    1 1
    2
    1 1
    2 1
    
    Expected output
    Case #1: 1
    Case #2: 1 2
    Case #3: 2 1
    
  3. Example 3

    Input
    2
    12
    1 2 3 4 5 6 7 8 9 10 11 12
    1 1 1 1 1 1 1 1 1 1 1 1
    12
    1 1 1 1 1 1 1 1 1 1 1 1
    12 11 10 9 8 7 6 5 4 3 2 1
    
    Expected output
    Case #1: 1 2 3 4 5 6 7 8 9 10 11 12
    Case #2: 12 11 10 9 8 7 6 5 4 3 2 1