Minimum Swaps
InterviewTime limit1sMemory limit64 MB
Given permutations A and B, find the minimum number of swaps within A that turn it into B.
Problem
You are given arrays and . Each one holds the numbers through exactly once, but the two orders may differ.
One operation swaps two numbers inside .
Find the minimum number of operations that makes equal to .
Input
- The first line contains , the size of arrays and . ()
- The second line contains the elements of , separated by single spaces.
- The third line contains the elements of , separated by single spaces.
Output
Print on the first line the minimum number of operations that makes equal to .
Hint
For and , swapping with , then with , then with turns into in three operations.
For and , swapping with , then with , then with , then with takes four operations.