CF1450F.The Struggling Contestant
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
To help those contestants who struggle a lot in contests, the headquarters of Codeforces are planning to introduce Division 5. In this new division, the tags of all problems will be announced prior to the round to help the contestants.
The contest consists of n problems, where the tag of the i -th problem is denoted by an integer ai .
You want to AK (solve all problems). To do that, you must solve the problems in some order. To make the contest funnier, you created extra limitations on yourself. You do not want to solve two problems consecutively with the same tag since it is boring. Also, you are afraid of big jumps in difficulties while solving them, so you want to minimize the number of times that you solve two problems consecutively that are not adjacent in the contest order.
Formally, your solve order can be described by a permutation p of length n . The cost of a permutation is defined as the number of indices i ( 1≤i<n ) where ∣pi+1−pi∣>1 . You have the requirement that api=api+1 for all 1≤i<n .
You want to know the minimum possible cost of permutation that satisfies the requirement. If no permutations meet this requirement, you should report about it.
输入格式
The first line contains a single integer t ( 1≤t≤104 ) — the number of test cases.
The first line of the description of each test case contains a single integer n ( 1≤n≤105 ) — the number of problems in the contest.
The next line contains n integers a1,a2,…an ( 1≤ai≤n ) — the tags of the problems.
It is guaranteed that the sum of n over all test cases does not exceed 105 .
输出格式
For each test case, if there are no permutations that satisfy the required condition, print −1 . Otherwise, print the minimum possible cost of a permutation that satisfies the required condition.
输入输出样例
输入#1
4 6 2 1 2 3 1 1 5 1 1 1 2 2 8 7 7 2 7 7 1 8 7 10 1 2 3 4 1 1 2 3 4 1
输出#1
1 3 -1 2
说明/提示
In the first test case, let p=[5,4,3,2,1,6] . The cost is 1 because we jump from p5=1 to p6=6 , and ∣6−1∣>1 . This permutation is valid because we don't solve problems with the same tag twice in a row. We cannot find a permutation with a cost smaller than 1 .
In the second test case, let p=[1,5,2,4,3] . The cost is 3 because ∣p2−p1∣>1 , ∣p3−p2∣>1 , and ∣p4−p3∣>1 . The permutation is valid because we don't solve problems with the same tag twice in a row. We cannot find a permutation with a cost smaller than 3 .
In the third test case, for any order of solving the problems, we will solve two problems with the same tag consecutively, so the answer is −1 .