#K84647. Minimum Adjacent Swaps

    ID: 36466 Type: Default 1000ms 256MiB

Minimum Adjacent Swaps

Minimum Adjacent Swaps

You are given two permutations of n distinct numbers. The first permutation represents the initial order of packages, and the second represents the desired final order. Your task is to determine the minimum number of adjacent swaps required to convert the initial order into the final order. An adjacent swap only exchanges two consecutive elements.

Hint: First, map the initial order into positions defined by the final order. Then, count the number of inversions in this mapped sequence. The inversion count is equal to the minimum number of adjacent swaps needed.

Input Format: The first line contains an integer n (1 ≤ n ≤ 105). The second line contains n unique integers denoting the initial order. The third line contains n unique integers denoting the final order.

Output Format: Output a single integer – the minimum number of adjacent swaps required.

inputFormat

The input is read from standard input (stdin). It consists of three lines:

  1. An integer n, representing the number of packages.
  2. n space-separated integers that represent the initial order.
  3. n space-separated integers that represent the final order.

outputFormat

Print a single integer to standard output (stdout): the minimum number of adjacent swaps required to change the initial order into the final order.## sample

4
4 3 2 1
1 2 3 4
6