두 순열 A와 B가 주어질 때, 인접한 원소를 교환해 A를 재배열하여 |a_i - b_i|의 합을 최대로 만들고, 그때 필요한 최소 교환 횟수를 구한다.
어려움8그리디정렬조합론수학아직 제출이 없습니다시간 제한2초메모리 제한1024 MBMaximizer has two permutations A=\[a_1,a_2,⋯,a_N] and B=\[b_1,b_2,⋯,b_N]. Both A,B have length N and consists of distinct integers from 1 to N.
Maximizer wants to maximize the sum of differences of each element, ∑_i=1N∣a_i−b_i∣. But he can only swap two adjacent elements in A. Precisely, he can only swap a_i and a_i+1 for some i from 1 to N−1. He can swap as many times as he wants.
What is the minimum number of swaps required for maximizing the difference sum?
The first line contains an integer N. (1≤N≤250,000)
The second line contains N integers a_1,a_2,⋯,a_N (1≤a_i≤N).
The third line contains N integers b_1,b_2,⋯,b_N (1≤b_i≤N).
Each of \[a_1,a_2,⋯,a_N] and \[b_1,b_2,⋯,b_N] is a permutation. In other words, it is consisted of distinct integers from 1 to N.
Print an integer, the minimum number of swaps required for maximizing the difference sum.