Pinely Round 5 (Div. 1 + Div. 2)
D
Locked Out
Codeforces
2100
原题
← CE →
翻译中…

Who's a good array? You're a good array! (c)

翻译中…

An array $b$ is good if there do not exist indices $1 \leq i \lt j \leq |b|$ such that $b_j - b_i = 1$.

翻译中…

You are given an integer. array $a_1, a_2, \ldots, a_n$. Determine the minimum number of elements that need to be removed from the given array so that it becomes a good array.

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 6 \cdot 10^4$). The description of the test cases follows.

翻译中…

The first line of each test case contains an integer $n$ ($1 \leq n \leq 3 \cdot 10^5$) — the number of elements in the array.

翻译中…

The second line of each test case contains $n$ integers $a_1$, $a_2$, $\ldots$, $a_n$ ($1 \leq a_i \leq n$) — the elements of the array.

翻译中…

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

翻译中…

Output

翻译中…

For each test case, print a single integer — the minimum number of elements that need to be removed from the array to make it a good array.

翻译中…

Note

翻译中…

In the first test case, the array is good from the very beginning, so nothing needs to be removed from it.

翻译中…

In the second test case, the optimal solution is to remove the second and fourth elements of the array, which will result in $1$, $3$, $5$ array.

翻译中…

In the third test case, the array is good from the very beginning.

翻译中…

In the fourth test case, the array is good from the very beginning.

翻译中…

In the fifth test case, the optimal solution is to remove the fourth element of the array, which will result in $1$, $7$, $1$, $5$, $7$, $1$ array.

翻译中…

In the sixth test case, one of the optimal solutions is to remove the first and fourth elements of the array, which will result in $2$, $5$, $5$, $5$ array.

翻译中…

样例

Input #1
6
1
1
5
1 2 3 4 5
5
5 4 3 2 1
5
5 5 5 4 4
7
1 7 1 2 5 7 1
6
1 2 5 6 5 5
Output #1
0
2
0
0
1
2
InputCopy OutputCopy
题目信息
题号
2161D
时间限制
2 s
内存限制
512 MB
标签
二分
数据结构
动态规划
贪心

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。