P14616 [CodeChef] PERMUTATION
普及-
题目描述

You are given an array A of size N. In one operation, you can:
Choose an index i (1≤ i ≤ N) and increase A_{i} by 1.

Find the minimum number of operations required to convert the array A into a permutation of size N. If it is impossible to do so, print -1.

Note that a permutation of size N contains each element from 1 to N exactly once.

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

  • The first line of input will contain a single integer T, denoting the number of test cases.
  • Each test case consists of multiple lines of input.
  • The first line of each test case contains the integer N — the size of the array.
  • The next line contains N space-separated integers, the elements of the array A.

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

For each test case, output on a new line, the minimum number of operations required to convert the array A into a permutation of size N.
If it is impossible to do so, print -1.

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

$1 ≤ T ≤ 500$
$1 ≤ N ≤ 1000$
$0 ≤ A_{i} ≤ 1000$

----- Sample Input 1 ------
4
4
3 1 1 2
3
0 3 3
3
3 2 1
3
2 0 1

----- Sample Output 1 ------
3
-1
0
3
----- explanation 1 ------
Test case $1$: We can convert the array $A$ into a permutation using $3$ operations:
- Operation $1$: Choose $i = 3$ and increase $A_{i}$ by $1$. Thus, the array becomes $A = [3, 1, 2, 2]$.
- Operation $2$: Choose $i = 3$ and increase $A_{i}$ by $1$. Thus, the array becomes $A = [3, 1, 3, 2]$.
- Operation $3$: Choose $i = 3$ and increase $A_{i}$ by $1$. Thus, the array becomes $A = [3, 1, 4, 2]$.

It can be shown that this is the minimum number of operations required to convert $A$ into a permutation.

Test case $2$: The given array cannot be converted into a permutation using any number of operations.

Test case $3$: The given array is already a permutation. Thus, we require $0$ operations.

Test case $4$: We can convert the array $A$ into a permutation using $3$ operations:
- Operation $1$: Choose $i = 1$ and increase $A_{i}$ by $1$. Thus, the array becomes $A = [3, 0, 1]$.
- Operation $2$: Choose $i = 2$ and increase $A_{i}$ by $1$. Thus, the array becomes $A = [3, 1, 1]$.
- Operation $3$: Choose $i = 3$ and increase $A_{i}$ by $1$. Thus, the array becomes $A = [3, 1, 2]$.

It can be shown that this is the minimum number of operations required to convert $A$ into a permutation.

输入格式

见题面

输出格式

见题面

样例输入
4 4 3 1 1 2 3 0 3 3 3 3 2 1 3 2 0 1
样例输出
3 -1 0 3