Increase or Smash
면접 대비시간 제한1초메모리 제한2048 MB
모두 0인 배열에서 시작해 모든 원소에 같은 값을 더하거나 일부 원소를 0으로 만드는 연산만 사용해 목표 배열을 만들 때 필요한 최소 연산 수를 구한다.
문제
Geumjae has an array consisting of zeros. His goal is to transform it into a given target array using a minimum number of operations.
He can perform the following two types of operations any number of times, in any order:
- Increase: Choose any positive integer . Increase all elements of the array by . In other words, he chooses a positive integer , and for each (), he replaces with .
- Smash: Set some elements (possibly none or all) of the array to . In other words, for each (), he either replaces with or leaves it as before.
Given the final target state of the array , find the minimum total number of operations (both Increase and Smash) Geumjae needs to perform.
It can be shown that for any given final array, a sequence of operations always exists.
입력
Each test contains multiple test cases. The first line contains the number of test cases (). The description of the test cases follows.
The first line contains a single integer () --- the number of elements in the array .
The second line contains integers () --- the elements of the target array .
출력
For each test case, output a single integer --- the minimum number of operations required.
힌트
Explanation of the first test case:
The target array is . A possible sequence of 3 operations (which is the minimum) is:
- Initially, the array is . After an Increase operation with , the array becomes .
- Next, after a Smash operation on the first two elements, the array becomes .
- Finally, after an Increase operation with , the array becomes .
We used Increase operations and Smash operation for a total of operations.
Explanation of the second test case:
The target array is . A single Increase operation with gives the target array.