#P13781. [eJOI 2022] Adjacent Pairs

[eJOI 2022] Adjacent Pairs

题目描述

Let's call an array b1,b2,,bmb_1, b_2, \ldots, b_m good, if bibi+1b_i \neq b_{i+1} for any ii with 1im11 \leq i \leq m - 1.

You are given a good array of nn positive integers a1,a2,a3,,ana_1, a_2, a_3, \ldots, a_n.

You can perform the following operations on this array:

  • Choose any index ii (1in1 \leq i \leq n) and a number xx (1x1091 \leq x \leq 10^9). Then, set aia_i to xx. 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 tt (1t1051 \leq t \leq 10^5), the number of test cases. The description of test cases follows.

The first line of each test case contains a single integer nn (2n21052 \leq n \leq 2 \cdot 10^5) - the length of the array.

The second line of each test case contains nn integers a1,a2,,ana_1, a_2, \ldots, a_n (1ain1 \leq a_i \leq n) - elements of the array. It's guaranteed that aiai+1a_i \neq a_{i+1} for 1in11 \leq i \leq n - 1 (that is, the array is good).

It is guaranteed that the sum of nn over all test cases does not exceed 21052 \cdot 10^5.

输出格式

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.

2
5
4 5 2 4 5
2
1 2
3
0

提示

Note

In the first test case, one of the optimal sequences of operations is:

$(4, 5, 2, 4, 5) \rightarrow (2, 5, 2, 4, 5) \rightarrow (2, 5, 2, 4, 2) \rightarrow (2, 5, 2, 5, 2)$.

In the second test case, the array already contains only two distinct values, so the answer is 00.

Scoring

  1. (20 points): The sum of nn over all test cases does not exceed 100100
  2. (10 points): The sum of nn over all test cases does not exceed 500500
  3. (25 points): The sum of nn over all test cases does not exceed 40004000
  4. (45 points): No additional constraints