Increase or Smash

면접 대비

시간 제한1초메모리 제한2048 MB

요약
모두 0인 배열에서 시작해 모든 원소에 같은 값을 더하거나 일부 원소를 0으로 만드는 연산만 사용해 목표 배열을 만들 때 필요한 최소 연산 수를 구한다.
난이도

보통10점 중 5점

유형
그리디, 배열, 구현, 정렬
정답자
아직 제출이 없습니다

문제

Geumjae has an array aa consisting of nn 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:

  1. Increase: Choose any positive integer xx. Increase all elements of the array aa by xx. In other words, he chooses a positive integer xx, and for each ii (1≤i≤n1 \le i \le n), he replaces a_ia\_i with a_i+xa\_i + x.
  2. Smash: Set some elements (possibly none or all) of the array aa to 00. In other words, for each ii (1≤i≤n1 \le i \le n), he either replaces a_ia\_i with 00 or leaves it as before.

Given the final target state of the array aa, 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 tt (1≤t≤10001 \le t \le 1000). The description of the test cases follows.

The first line contains a single integer nn (1≤n≤1001 \le n \le 100) --- the number of elements in the array aa.

The second line contains nn integers a_1,a_2,…,a_na\_1, a\_2, \ldots, a\_n (1≤a_i≤1001 \le a\_i \le 100) --- the elements of the target array aa.

출력

For each test case, output a single integer --- the minimum number of operations required.

힌트

Explanation of the first test case:

The target array is \[1,1,3]\[1, 1, 3]. A possible sequence of 3 operations (which is the minimum) is:

  • Initially, the array is \[0,0,0]\[0, 0, 0]. After an Increase operation with x=2x = 2, the array becomes \[2,2,2]\[2, 2, 2].
  • Next, after a Smash operation on the first two elements, the array becomes \[0,0,2]\[0, 0, 2].
  • Finally, after an Increase operation with x=1x = 1, the array becomes \[1,1,3]\[1, 1, 3].

We used 22 Increase operations and 11 Smash operation for a total of 33 operations.

Explanation of the second test case:

The target array is \[100]\[100]. A single Increase operation with x=100x = 100 gives the target array.

예제1

  1. 예제 1

    입력
    3
    3
    1 1 3
    1
    100
    9
    9 9 3 2 4 4 8 5 3
    
    예상 출력
    3
    1
    11