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 n positive integers a1,a2,…,an.
Each operation takes one second. In one operation, Willem does the following:
- Choose an index p (1≤p≤n);
- Then, replace ap with ⌊2ap⌋, and for every i=p, replace ai by ⌈2ai⌉. All replacements are performed simultaneously.
Find the minimum number of seconds needed to make all n integers equal to 0.
在她最后一次出击之前,克洛伊向威尔姆提出了三个问题。
第一个问题是:如果终结不可避免,那么需要多长时间才会什么都不剩?
威尔姆无法直接回答她。相反,他写下了 n 个正整数 a1,a2,…,an。
每次操作耗时一秒。在一次操作中,威尔姆执行以下步骤:
- 选择一个下标 p(满足 1≤p≤n);
- 然后将 ap 替换为 ⌊2ap⌋,并对每个 i=p,将 ai 替换为 ⌈2ai⌉。所有替换同时进行。
求使全部 n 个整数均变为 0 所需的最少秒数。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). The description of the test cases follows.
The first line of each test case contains one integer n (1≤n≤2⋅105) — the number of integers.
The second line of each test case contains n integers a1,a2,…,an (1≤ai≤109) — the initial integers.
It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤104)。随后是各测试用例的描述。
每个测试用例的第一行包含一个整数 n(1≤n≤2⋅105)—— 表示整数的个数。
每个测试用例的第二行包含 n 个整数 a1,a2,…,an(1≤ai≤109)—— 表示初始的整数。
保证所有测试用例中 n 的总和不超过 2⋅105。
输出格式
For each test case, output a single integer — the minimum number of seconds needed to make all integers equal to 0.
对于每个测试用例,输出一个整数——使所有整数变为 0 所需的最少秒数。
输入输出样例
输入#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→0, so the answer is 2.
In the second test case, an integer equal to 1 becomes 0 only when its index is chosen. Thus, at least 3 seconds are necessary, and choosing every index once is sufficient.
In the third test case, an optimal sequence is:
- Choose p=1: [1,2,4]→[0,1,2];
- Choose p=2: [0,1,2]→[0,0,1];
- Choose p=3: [0,0,1]→[0,0,0].
In the fourth test case, an optimal sequence is [5,2]→[2,1]→[1,0]→[0,0], where the chosen indices are 1, 2, and 1.
在第一个测试用例中,唯一的整数变化过程为 3→1→0,因此答案为 2。
在第二个测试用例中,值为 1 的整数仅在其下标被选中时才会变为 0。因此,至少需要 3 秒;而依次选择每个下标一次即已足够。
在第三个测试用例中,一个最优操作序列为:
- 选择 p=1:[1,2,4]→[0,1,2];
- 选择 p=2:[0,1,2]→[0,0,1];
- 选择 p=3:[0,0,1]→[0,0,0]。
在第四个测试用例中,一个最优操作序列为 [5,2]→[2,1]→[1,0]→[0,0],所选下标依次为 1、2 和 1。
输入解题思路,AI测评打分。不知道怎么写?