Double inversions

2.6

31 votes
Arrays, Data Structures, 1-D, Math
Problem

Consider a certain permutation of integers from 1 to n as A={a1,a2,...,an} and reverse of array A as R, that is, R={an,an1,...,a1}. You are given the inversions at each position of array A and R as {IA1,IA2,..,IAn} and {IR1,IR2,..,IRn} respectively.

Find the original array A. If, there are multiple solutions, print any of them. If there is no solution, then print -1.

Note: The inversion of array arr for position i is defined as the count of positions j satisfying the following condition arrj>arri and 1j<i.

Input format

  • The first line contains T denoting the number of test cases.
  • For each test case:
    • The first line contains n denoting the number of elements.
    • The second line contains the elements {IA1,IA2,..,IAn}.
    • The third line contains the elements {IR1,IR2,..,IRn}.

Output format

For each test case, print the array A in the space-separated format or -1 if no solution exists. Each test case should be answered in a new line. 

Constraints 

1T101n1050IAi,IRi<n  i[1,n]

Time Limit: 3
Memory Limit: 256
Source Limit:
Explanation

For first test case

Consider permutation A={3,4,2,1}.

It can be seen that inversion for this array will be IA={0,0,2,3} as:

  •  For i = 1, no positions satisfy   arrj>arri and 1j<i.
  •  For i = 2, no position satisfy   arrj>arri and 1j<i.
  •  For i = 3, j = 1, 2 satisfy   arrj>arri and 1j<i.
  •  For i = 4, j = 1, 2, 3 no positions satisfy   arrj>arri and 1j<i.

Now, R={1,2,4,3}.

It can be seen that inversion for this array will be IR={0,0,0,1} as:

  •  For i = 1, no positions satisfy   arrj>arri and 1j<i.
  •  For i = 2, no positions satisfy   arrj>arri and 1j<i.
  •  For i = 3,  no positions satisfy   arrj>arri and 1j<i.
  •  For i = 4, j = 3 satisfy   arrj>arri and 1j<i.

Thus, A={3,4,2,1} is a valid permutation.

For second test case

There is no valid permutation that satisfies the condition.

Editor Image

?