P17471 [CodeChef] REMOVEADD
提高-
题目描述

You are given an array A of N integers. You must perform some (possibly zero) operations to make the elements of A distinct.

In one operation, you can either:
Remove one element from the beginning of the array A and append any positive integer to the end.
Or remove one element from the end of the array A and prepend any positive integer to the beginning.

Find the minimum number of operations required to make all the elements of the array distinct.

------ Input Format ------

  • The first line contains a single integer T — the number of test cases. Then the test cases follow.
  • The first line of each test case contains an integer N — the size of the array A.
  • The second line of each test case contains $N space-separated integers A_{1}, A_{2}, \dots, A_{N}$ denoting the array A.

------ Output Format ------

For each test case, output the minimum number of operations required to make all the elements distinct.

------ Constraints ------

$1 ≤ T ≤ 10^{3}$
$1 ≤ N ≤ 2 \cdot 10^{5}$
- The sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$
$1 ≤ A_{i} ≤ 10^{9}$

----- Sample Input 1 ------
3
8
1 3 3 5 1 2 4 1
4
1 2 3 4
5
10 20 10 20 30

----- Sample Output 1 ------
4
0
2

----- explanation 1 ------
Test case $1$:
Initially $A = [1, 3, 3, 5, 1, 2, 4, 1]$. Consider the following sequence of operations.
- $[1, 3, 3, 5, 1, 2, 4, \underline{1}] \xrightarrow[\mathrm{prepend} \, 6]{\mathrm{remove} \, 1} [\boldsymbol 6, 1, 3, 3, 5, 1, 2, 4]$

  • $[\underline{6}, 1, 3, 3, 5, 1, 2, 4] \xrightarrow[\mathrm{append} \, 7]{\mathrm{remove} \, 6} [1, 3, 3, 5, 1, 2, 4, \boldsymbol 7]$

  • $[\underline{1}, 3, 3, 5, 1, 2, 4, 7] \xrightarrow[\mathrm{append} \, 8]{\mathrm{remove} \, 1} [3, 3, 5, 1, 2, 4, 7, \boldsymbol 8]$

  • $[\underline{3}, 3, 5, 1, 2, 4, 7, 8] \xrightarrow[\mathrm{append} \, 9]{\mathrm{remove} \, 3} [3, 5, 1, 2, 4, 7, 8, \boldsymbol 9]$

All the elements of $A$ are now distinct. It can be shown that it is not possible in less than $4$ operations.

Test case $2$: No operations are required since all the elements of $A$ are already distinct.

输入格式

见题面

输出格式

见题面

样例输入
3 8 1 3 3 5 1 2 4 1 4 1 2 3 4 5 10 20 10 20 30
样例输出
4 0 2