Arranging Shoes
문제
Adnan owns the biggest shoe store in Baku. A box containing pairs of shoes has just arrived at the store. Each pair consists of two shoes of the same size: a left and a right one. Adnan has put all of the shoes in a row consisting of positions numbered through from left to right.
Adnan wants to rearrange the shoes into a valid arrangement. An arrangement is valid if and only if for every (), the following conditions hold:
- The shoes at positions and are of the same size.
- The shoe at position is a left shoe.
- The shoe at position is a right shoe.
For this purpose, Adnan can make a series of swaps.
In each swap, he selects two shoes that are adjacent at that moment and exchanges them (i.e., picks them up and puts each one on the former position of the other shoe).
Two shoes are adjacent if their positions differ by one.
Determine the minimum number of swaps that Adnan needs to perform in order to obtain a valid arrangement of the shoes.