#C6410. Minimum Swaps to Match Dish Distribution
Minimum Swaps to Match Dish Distribution
Minimum Swaps to Match Dish Distribution
You are given two arrays of integers of equal length: food
and distribution
.
The array food
represents the favorite dish of each guest such that the favorite dish for guest i is food[i]
. The array distribution
represents the current arrangement of dishes at the tables.
You are allowed to perform swaps on the distribution
array. In one swap, you can exchange two dishes. Your goal is to rearrange distribution
so that for every guest i, the dish at position i matches food[i]
.
Formally, given an integer \( n \) (the number of guests), two arrays of integers \( food[0 \ldots n-1] \) and \( distribution[0 \ldots n-1] \), find the minimum number of swaps needed so that for every index \( i \), distribution[i] = food[i]
.
Note: It is guaranteed that distribution
is a permutation of food
.
inputFormat
The first line of input contains an integer ( n ) representing the number of guests. The second line contains ( n ) space-separated integers representing the food
array (the guests' favorite dishes). The third line contains ( n ) space-separated integers representing the distribution
array (the current dish distribution).
outputFormat
Output a single integer: the minimum number of swaps required to rearrange the distribution
array such that for every ( i ), distribution[i] = food[i]
.## sample
4
1 4 3 2
2 1 4 3
3