P24982 [AtCoder] [ARC043C] Inversion Distance
提高+
题目描述

Problem Description

A sequence rearranged from integers $1$ to $N$ is called a permutation of size $N$.

Given two permutations $X$ and $Y$ of the same size, the number of pairs of numbers whose order is reversed in $X$ and $Y$ is called the inversion distance between $X$ and $Y$.

For example, in the permutations $[3, 1, 4, 2, 5]$ and $[2, 5, 3, 4, 1]$, the inversion distance is $7$ because the order of the following $7$ pairs is reversed:

  • $ (1, 2), (1, 4), (1, 5), (2, 3), (2, 4), (3, 5), (4, 5) $

You are given two permutations $A$ and $B$ of size $N$.

Determine if there exists a permutation of size $N$ that has the same inversion distance to both $A$ and $B$. If such a permutation exists, provide one such permutation.

If there are multiple possible answers, any one of them is acceptable.

Input Format

The input is given from the standard input in the following format:

$ N $ $ A_1 $ $ A_2 $ .. $ A_N $ $ B_1 $ $ B_2 $ .. $ B_N $

  • The first line contains an integer $ N (1 ≦ N ≦ 10^5) $ representing the size of the permutations.
  • The second line contains $ N $ integers separated by spaces, representing the elements of permutation $ A $. The $ i $-th integer is $ A_i (1 ≦ A_i ≦ N) $.
  • The third line contains $ N $ integers separated by spaces, representing the elements of permutation $ B $. The $ i $-th integer is $ B_i (1 ≦ B_i ≦ N) $.
  • $ A_i ≠ A_j $ and $ B_i ≠ B_j $ hold for $ i ≠ j $.

Output Format

If no such permutation exists, output $ -1 $ on a single line. If it exists, output the elements of the permutation separated by spaces on a single line.

Sample Input and Output

Sample Input #1

5
1 2 3 4 5
5 4 3 2 1

Sample Output #1

5 2 1 3 4

Sample Input #2

5
1 2 3 4 5
1 2 4 3 5

Sample Output #2

-1

Sample Input #3

9
3 1 4 2 5 9 7 6 8
2 1 8 3 5 7 9 4 6

Sample Output #3

3 1 2 8 4 5 7 9 6

Notes/Hints

Partial Points

This problem has partial points.

  • If you solve the dataset where $ 1 ≦ N ≦ 3,000 $, you will be awarded $ 30 $ points.
  • If you solve the dataset where $ 1 ≦ N ≦ 10^5 $, you will be awarded an additional $ 70 $ points, totaling $ 100 $ points.

Sample Explanation 1

If the output permutation is denoted as $ C $, both the inversion distance between $ A $ and $ C $ and the inversion distance between $ B $ and $ C $ are $ 5 $.

Sample Explanation 2

No permutation exists that has the same inversion distance to both $ A $ and $ B $.

输入格式

见题面 Input 部分

输出格式

见题面 Output 部分

样例输入
5 1 2 3 4 5 5 4 3 2 1
样例输出
5 2 1 3 4