Maximizer
Time limit2sMemory limit1024 MB
Given two permutations A and B of 1..N, find the minimum number of adjacent swaps in A needed to reach a permutation maximizing the sum of |a_i - b_i|.
- Level
Hard8 of 10
- Topics
- Greedy, Combinatorics, Sorting, Math
- Solved
- No attempts yet
Problem
Maximizer has two permutations and . Both and have length and consist of distinct integers from to .
Maximizer wants to maximize the sum of differences of each element, . But he can only swap two adjacent elements in . Precisely, he can only swap and for some from to . He can swap as many times as he wants.
What is the minimum number of swaps required for maximizing the difference sum?
Input
The first line contains an integer . ()
The second line contains integers ().
The third line contains integers ().
Each of and is a permutation. In other words, it is consisted of distinct integers from to .
Output
Print an integer, the minimum number of swaps required for maximizing the difference sum.