CF2231C.Chipmunk Theo and Equality

普及/提高-

通过率:0%

时间限制:2.00s

内存限制:512MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

While exploring the depths of the Internet, Theodore the chipmunk found a very interesting sequence of positive integers and decided to play with it.

In one operation, he chooses an element of the sequence and performs the following action:

  • If the chosen element is even, he divides it by 2.
  • If the chosen element is odd, he increases it by 1.

Theo really loves equality, so he wants to make all numbers in the sequence equal (otherwise, some numbers might feel offended). Since he needs to plan his lunch time, help him determine the minimum number of operations required to make all numbers equal.

西奥多这只花栗鼠在探索互联网深处时,发现了一个非常有趣的正整数序列,并决定和它玩一玩。

在一次操作中,他选择序列中的一个元素,并执行以下操作:

  • 如果所选元素为偶数,则将其除以 2;
  • 如果所选元素为奇数,则将其加 1。

西奥多特别喜欢“相等”,因此他希望使序列中所有数字都相等(否则,有些数字可能会感到受伤)。由于他需要规划自己的午餐时间,请你帮他确定使所有数字相等所需的最少操作次数。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041 \le t \le 10^4). The description of the test cases follows.

The first line of each test case contains a single integer nn (1≤n≤1051 \le n \le 10^5) — the length of the sequence.

The second line of each test case contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (1≤ai≤1091 \le a_i \le 10^9) — the elements of the sequence.

It is guaranteed that the sum of nn over all test cases does not exceed 10510^5.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1041 \le t \le 10^4)。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(1≤n≤1051 \le n \le 10^5)—— 序列的长度。

每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(1≤ai≤1091 \le a_i \le 10^9)—— 序列的元素。

保证所有测试用例的 nn 之和不超过 10510^5。

输出格式

For each test case, output a single integer — the minimum number of operations Theo needs to make all numbers in the sequence equal.

对于每个测试用例,输出一个整数——Theo 使序列中所有数字相等所需的最少操作次数。

输入输出样例

  • 输入#1

    5
    3
    3 2 4
    7
    3 6 7 16 8 8 7
    3
    1 4 2
    5
    10 10 10 10 10
    6
    1 1 3 1 1 1

    输出#1

    3
    11
    2
    0
    3

说明/提示

In the first test case, we have a sequence: [3,2,4][3, 2, 4]

One possible sequence of operations: $$ [\textbf{3}, 2, 4] \rightarrow [\textbf{4}, 2, 4] \rightarrow [2, 2, \textbf{4}] \rightarrow [2, 2, 2] $$ (Operations are performed on numbers shown in bold.)

In the second test case, the sequence is: [3,6,7,16,8,8,7][3, 6, 7, 16, 8, 8, 7]

Possible operations:

  • Perform the operation once on the 1st element: 3→43 \rightarrow 4
  • Perform the operation twice on the 2nd element: 6→3→46 \rightarrow 3 \rightarrow 4
  • Perform the operation twice on the 4th element: 7→8→47 \rightarrow 8 \rightarrow 4
  • Perform the operation twice on the 5th element: 16→8→416 \rightarrow 8 \rightarrow 4
  • Perform the operation once on the 6th element: 8→48 \rightarrow 4
  • Perform the operation once on the 7th element: 8→48 \rightarrow 4
  • Perform the operation twice on the 8th element: 7→8→47 \rightarrow 8 \rightarrow 4

After these 1111 operations, all elements become 44.

在第一个测试用例中,我们有一个序列:[3,2,4][3, 2, 4]。

一种可能的操作序列如下:

\[\mathbf{3}, 2, 4\] \rightarrow \[\mathbf{4}, 2, 4\] \rightarrow \[2, 2, \mathbf{4}\] \rightarrow \[2, 2, 2\]

(加粗显示的数字表示每次操作所作用的元素。)

在第二个测试用例中,序列为:[3,6,7,16,8,8,7][3, 6, 7, 16, 8, 8, 7]。

可能的操作如下:

  • 对第 1 个元素执行一次操作:3→43 \rightarrow 4
  • 对第 2 个元素执行两次操作:6→3→46 \rightarrow 3 \rightarrow 4
  • 对第 4 个元素执行两次操作:7→8→47 \rightarrow 8 \rightarrow 4
  • 对第 5 个元素执行两次操作:16→8→416 \rightarrow 8 \rightarrow 4
  • 对第 6 个元素执行一次操作:8→48 \rightarrow 4
  • 对第 7 个元素执行一次操作:8→48 \rightarrow 4
  • 对第 8 个元素执行两次操作:7→8→47 \rightarrow 8 \rightarrow 4

经过这 1111 次操作后,所有元素均变为 44。

输入解题思路,AI测评打分。不知道怎么写?

首页