Under construction

Still being built. Things may change

Erdős and Szekeres Sequence Reconstruction

Time limit5sMemory limit512 MB

Summary
Rebuild the lexicographically smallest permutation of 1 to N whose increasing and decreasing subsequence lengths match the given arrays.
Level

Medium7 of 10

Topics
Backtracking, Greedy, Dynamic programming
Solved
No attempts yet

Problem

XX is a sequence that lists the numbers 11 through NN, each exactly once. Take some entries of XX in their original order. If the values keep growing, the entries form an increasing subsequence; if they keep shrinking, they form a decreasing subsequence. 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).

Paul Erdős and George Szekeres proved that XX always contains an increasing subsequence of length at least N\sqrt{N} or a decreasing subsequence of length at least N\sqrt{N}. The sequence above contains the decreasing subsequence (5,3,2,1)(5, 3, 2, 1) of length 4.

Two values are defined for each ii.

  • AiA_i is the maximum length of an increasing subsequence whose largest number is XiX_i.
  • BiB_i is the maximum length of a decreasing subsequence whose largest number is XiX_i.

The largest number of an increasing subsequence is its last entry, so AiA_i equals the maximum length of an increasing subsequence that ends at XiX_i. The largest number of a decreasing subsequence is its first entry, so BiB_i equals the maximum length of a decreasing subsequence that starts at XiX_i.

For X=(4,5,3,7,6,2,8,1)X = (4, 5, 3, 7, 6, 2, 8, 1) the values are:

iiXiX_iAiA_iBiB_i
0414
1524
2313
3734
4633
5212
6842
7111

The pair (Ai,Bi)(A_i, B_i) differs for every ii, and that forces AiA_i or BiB_i to be at least N\sqrt{N} for some ii.

Given AA and BB, reconstruct XX. The answer must list the numbers 11 through NN exactly once each. If several sequences are possible, print the lexicographically smallest one: make X0X_0 as small as possible, then make X1X_1 as small as possible among what is left, and so on.

Input

The first line holds the number of test cases TT. Each test case takes three lines.

The first line holds one integer NN. The second line holds A0,A1,…,AN−1A_0, A_1, \ldots, A_{N-1} separated by spaces. The third line holds B0,B1,…,BN−1B_0, B_1, \ldots, B_{N-1} separated by spaces.

Limits

  • 1≤T≤301 \le T \le 30
  • 1≤N≤201 \le N \le 20
  • AiA_i and BiB_i are positive integers.
  • At least one valid XX exists.

Output

For each test case print one line with Case #x: followed by X0,X1,…,XN−1X_0, X_1, \ldots, X_{N-1} separated by spaces. Here xx is the test case number, counting from 1.

Examples1

  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