CF2255D.How Long Until Nothing Remains?

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Before her final sortie, Chtholly asks Willem three questions.

The first is this: if the end is inevitable, how long will it take until nothing remains?

Willem cannot answer her directly. Instead, he writes down nn positive integers a1,a2,…,ana_1,a_2,\ldots,a_n.

Each operation takes one second. In one operation, Willem does the following:

  • Choose an index pp (1≤p≤n1\le p\le n);
  • Then, replace apa_p with ⌊ap2⌋\left\lfloor\dfrac{a_p}{2}\right\rfloor, and for every i≠pi\ne p, replace aia_i by ⌈ai2⌉\left\lceil\dfrac{a_i}{2}\right\rceil. All replacements are performed simultaneously.

Find the minimum number of seconds needed to make all nn integers equal to 00.

在她最后一次出击之前,克洛伊向威尔姆提出了三个问题。

第一个问题是:如果终结不可避免,那么需要多长时间才会什么都不剩?

威尔姆无法直接回答她。相反,他写下了 nn 个正整数 a1,a2,…,ana_1,a_2,\ldots,a_n。

每次操作耗时一秒。在一次操作中,威尔姆执行以下步骤:

  • 选择一个下标 pp(满足 1≤p≤n1\le p\le n);
  • 然后将 apa_p 替换为 ⌊ap2⌋\left\lfloor\dfrac{a_p}{2}\right\rfloor,并对每个 i≠pi\ne p,将 aia_i 替换为 ⌈ai2⌉\left\lceil\dfrac{a_i}{2}\right\rceil。所有替换同时进行。

求使全部 nn 个整数均变为 00 所需的最少秒数。

输入格式

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 one integer nn (1≤n≤2⋅1051\le n\le2\cdot10^5) — the number of integers.

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\le10^9) — the initial integers.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052\cdot10^5.

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

每个测试用例的第一行包含一个整数 nn(1≤n≤2⋅1051\le n\le2\cdot10^5)—— 表示整数的个数。

每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1,a_2,\ldots,a_n(1≤ai≤1091\le a_i\le10^9)—— 表示初始的整数。

保证所有测试用例中 nn 的总和不超过 2⋅1052\cdot10^5。

输出格式

For each test case, output a single integer — the minimum number of seconds needed to make all integers equal to 00.

对于每个测试用例,输出一个整数——使所有整数变为 00 所需的最少秒数。

输入输出样例

  • 输入#1

    5
    1
    3
    3
    1 1 1
    3
    1 2 4
    2
    5 2
    6
    1 2 3 4 5 6

    输出#1

    2
    3
    3
    3
    6

说明/提示

In the first test case, the only integer changes as 3→1→03\to1\to0, so the answer is 22.

In the second test case, an integer equal to 11 becomes 00 only when its index is chosen. Thus, at least 33 seconds are necessary, and choosing every index once is sufficient.

In the third test case, an optimal sequence is:

  1. Choose p=1p=1: [1,2,4]→[0,1,2][1,2,4]\to[0,1,2];
  2. Choose p=2p=2: [0,1,2]→[0,0,1][0,1,2]\to[0,0,1];
  3. Choose p=3p=3: [0,0,1]→[0,0,0][0,0,1]\to[0,0,0].

In the fourth test case, an optimal sequence is [5,2]→[2,1]→[1,0]→[0,0][5,2]\to[2,1]\to[1,0]\to[0,0], where the chosen indices are 11, 22, and 11.

在第一个测试用例中,唯一的整数变化过程为 3→1→03\to1\to0,因此答案为 22。

在第二个测试用例中,值为 11 的整数仅在其下标被选中时才会变为 00。因此,至少需要 33 秒;而依次选择每个下标一次即已足够。

在第三个测试用例中,一个最优操作序列为:

  1. 选择 p=1p=1:[1,2,4]→[0,1,2][1,2,4]\to[0,1,2];
  2. 选择 p=2p=2:[0,1,2]→[0,0,1][0,1,2]\to[0,0,1];
  3. 选择 p=3p=3:[0,0,1]→[0,0,0][0,0,1]\to[0,0,0]。

在第四个测试用例中,一个最优操作序列为 [5,2]→[2,1]→[1,0]→[0,0][5,2]\to[2,1]\to[1,0]\to[0,0],所选下标依次为 11、22 和 11。

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

首页