AtCoder Regular Contest 195 (Div. 2)
D
Swap and Erase
AtCoder
⌀ 2470
原题
← CE →
翻译中…

Problem Statement

翻译中…

There is a sequence $A = (A_1,\ldots,A_N)$. You can perform the following two types of operations any number of times in any order:

翻译中…
  • Let $K$ be the length of $A$ just before the operation. Choose an integer $i$ such that $1 \leq i \leq K-1$, and swap the $i$-th and $(i+1)$-th elements of $A$.
  • Let $K$ be the length of $A$ just before the operation. Choose an integer $i$ such that $1 \leq i \leq K$ and all the values from the $1$-st through the $i$-th elements of $A$ are equal, and delete all the elements from the $1$-st through the $i$-th of $A$.
翻译中…

Find the minimum total number of operations required to make $A$ an empty sequence.

翻译中…

You are given $T$ test cases; solve each of them.

翻译中…

Constraints

翻译中…
  • $1\leq T\leq 10^5$
  • $2 \leq N \leq 2 \times 10^5$
  • $1 \leq A_i \leq N$
  • The sum of $N$ over all test cases is at most $2\times 10^5$.
  • All input values are integers.
翻译中…

Input

翻译中…

The input is given from Standard Input in the following format:

翻译中…

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

翻译中…

Each case is given in the following format:

翻译中…

N
A_1 A_2 \ldots A_N

翻译中…

Output

翻译中…

Print the answer for each test case in order, separated by newlines.

翻译中…

样例

Input #1
3
5
1 1 2 1 2
4
4 2 1 3
11
1 2 1 2 1 2 1 2 1 2 1
Output #1
3
4
8
For the 1st test case, $A$ can be made empty by the following three operations: - Swap the 3rd and 4th elements of $A$. Now, $A$ is $(1,1,1,2,2)$. - Delete the 1st through 3rd elements of $A$. Now, $A$ is $(2,2)$. - Delete the 1st through 2nd elements of $A$. Now, $A$ is an empty sequence. For the 2nd test case, $A$ can be made empty by deleting the 1st element four times. Also, it is impossible to make $A$ empty in three or fewer operations.
题目信息
题号
arc195_d
时间限制
2 s
内存限制
1024 MB

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