Time limit
1s
Memory limit
128 MB
Triangles, squares, and circles are arranged in a line. In one operation, choose any two positions and swap the shapes at those positions. The goal is to rearrange the line so that each kind of shape occupies one contiguous block. The order of the three blocks does not matter.
Given the current order of the shapes, write a program that finds the minimum number of swaps needed to make equal shapes contiguous.
The first line contains the total number of shapes N. N is between 3 and 100,000, inclusive.
The second line contains N integers separated by spaces, representing the shapes in order. Integer 1 represents a triangle, integer 2 represents a square, and integer 3 represents a circle.
Each of the three kinds of shape appears at least once.
Print the minimum number of swaps required to make equal shapes contiguous.