#P3430. Proper Soldier Rearrangement
Proper Soldier Rearrangement
Proper Soldier Rearrangement
We are given $2n$ soldiers arranged in two rows with n soldiers each. The soldiers have various heights, and each height appears at most twice among the $2n$ soldiers. In one operation, you can swap the two soldiers in the same column (i.e. exchange the soldier in the first row with the soldier in the second row in that column).
The goal is to rearrange the soldiers so that in each row, no height appears more than once – we say that the soldiers are set up properly if each row contains distinct heights. Your task is to determine the minimum number of swaps required to achieve this.
After a swap the configuration for a column i becomes:
- If no swap is performed, the column gives a pair \( (a_i, b_i) \) from row1 and row2 respectively.
- If a swap is performed, the column gives a pair \( (b_i, a_i) \).
Consider any soldier height \( h \) that appears in two different columns. Suppose in one column \( h \) appears in the first row (denoted by "A") and in the other column it appears in also the first row. Then if you do no swap in both, the first row will contain \( h \) twice, which violates the condition. In general, if in two columns the same height appears in the same position (both in the first row or both in the second row), the swap decisions in these two columns must be different. Otherwise, if the occurrences are in different rows (one in the first row and one in the second row), the swap decisions must be the same. Formally, for each soldier height \( h \) that appears in two columns \( i \) and \( j \):
- If the occurrences are of type A-A or B-B: then \( x_i \neq x_j \) must hold.
- If the occurrences are A-B or B-A: then \( x_i = x_j \) must hold.
Your task is to decide for each column whether to swap or not (i.e. assign a binary value \( x_i \)) so that both rows have all distinct soldier heights and the total number of swaps (i.e. the number of \( x_i=1 \)) is minimized.
inputFormat
The first line contains a single integer n (number of columns).
The second line contains n integers representing the heights of the soldiers in the first row.
The third line contains n integers representing the heights of the soldiers in the second row.
outputFormat
Output a single integer – the minimum number of swaps required so that each row has all distinct soldier heights.
sample
3
1 2 3
2 3 1
0