Adjacent Pairs
시간 제한3초메모리 제한1024 MB
인접한 원소가 같은 경우가 없는 배열이 주어질 때, 인접한 원소가 같지 않으면서 정확히 두 가지 값만 남도록 최소 개수의 위치를 바꾸는 문제이다.
문제
Let's call an array good, if for any with .
You are given a good array of positive integers .
You can perform the following operations on this array:
Choose any index () and a number (). Then, set to . After this operation, the array has to remain good.
You want to perform several operations so that the resulting array will contain exactly two distinct values. Determine the smallest number of operations needed to achieve this goal.
입력
The first line of input contains the integer (), the number of test cases. The description of test cases follows.
The first line of each test case contains a single integer () - the length of the array.
The second line of each test case contains integers () - elements of the array. It's guaranteed that for (that is, the array is good).
It is guaranteed that the sum of n over all test cases does not exceed .
출력
For each test case, output a single integer - the smallest number of operations needed to achieve an array in which there are exactly two distinct values.
힌트
In the first test case, one of the optimal sequences of operations is:
.
In the second test case, the array already contains only two distinct values, so the answer is .