{"problem":{"name":"Swap and Erase","description":{"content":"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 ","description_type":"Markdown"},"platform":"AtCoder","limit":{"time_limit":2000,"memory_limit":262144},"difficulty":"None","is_remote":true,"is_sync":true,"sync_url":null,"sign":"arc195_d"},"statements":[{"statement_type":"Markdown","content":"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:\n\n*   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$.\n*   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$.\n\nFind the minimum total number of operations required to make $A$ an empty sequence.\nYou are given $T$ test cases; solve each of them.\n\n## Constraints\n\n*   $1\\leq T\\leq 10^5$\n*   $2 \\leq N \\leq 2 \\times 10^5$\n*   $1 \\leq A_i \\leq N$\n*   The sum of $N$ over all test cases is at most $2\\times 10^5$.\n*   All input values are integers.\n\n## Input\n\nThe input is given from Standard Input in the following format:\n\n$T$\n$\\mathrm{case}_1$\n$\\mathrm{case}_2$\n$\\vdots$\n$\\mathrm{case}_T$\n\nEach case is given in the following format:\n\n$N$\n$A_1$ $A_2$ $\\ldots$ $A_N$\n\n[samples]","is_translate":false,"language":"English"}],"meta":{"iden":"arc195_d","tags":[],"sample_group":[["3\n5\n1 1 2 1 2\n4\n4 2 1 3\n11\n1 2 1 2 1 2 1 2 1 2 1","3\n4\n8\n\nFor the 1st test case, $A$ can be made empty by the following three operations:\n\n*   Swap the 3rd and 4th elements of $A$. Now, $A$ is $(1,1,1,2,2)$.\n*   Delete the 1st through 3rd elements of $A$. Now, $A$ is $(2,2)$.\n*   Delete the 1st through 2nd elements of $A$. Now, $A$ is an empty sequence.\n\nFor 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."]],"created_at":"2026-03-03 11:01:14"}}