CF1490A.Dense Array

入门

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Polycarp calls an array dense if the greater of any two adjacent elements is not more than twice bigger than the smaller. More formally, for any ii (1≤i≤n−11 \le i \le n-1), this condition must be satisfied: $$\frac{\max(a[i], a[i+1])}{\min(a[i], a[i+1])} \le 2$$

For example, the arrays [1,2,3,4,3][1, 2, 3, 4, 3], [1,1,1][1, 1, 1] and [5,10][5, 10] are dense. And the arrays [5,11][5, 11], [1,4,2][1, 4, 2], [6,6,1][6, 6, 1] are not dense.

You are given an array aa of nn integers. What is the minimum number of numbers you need to add to an array to make it dense? You can insert numbers anywhere in the array. If the array is already dense, no numbers need to be added.

For example, if a=[4,2,10,1]a=[4,2,10,1], then the answer is 55, and the array itself after inserting elements into it may look like this: a=[4,2,3‾,5‾,10,6‾,4‾,2‾,1]a=[4,2,\underline{\textbf{3}},\underline{\textbf{5}},10,\underline{\textbf{6}},\underline{\textbf{4}},\underline{\textbf{2}},1] (there are other ways to build such aa).

Polycarp 将一个数组称为稠密的(dense),如果其中任意两个相邻元素中,较大者不超过较小者的两倍。更形式化地,对任意 ii(1≤i≤n−11 \le i \le n-1),必须满足以下条件:

max⁡(a[i],a[i+1])min⁡(a[i],a[i+1])≤2\frac{\max(a[i], a[i+1])}{\min(a[i], a[i+1])} \le 2

例如,数组 [1,2,3,4,3][1, 2, 3, 4, 3]、[1,1,1][1, 1, 1] 和 [5,10][5, 10] 是稠密的;而数组 [5,11][5, 11]、[1,4,2][1, 4, 2]、[6,6,1][6, 6, 1] 不是稠密的。

给定一个包含 nn 个整数的数组 aa,你需要在数组中插入最少多少个数,才能使其变为稠密数组?你可以在数组的任意位置插入数字。如果原数组已经是稠密的,则无需插入任何数字。

例如,若 a=[4,2,10,1]a=[4,2,10,1],则答案为 55,插入元素后得到的数组可能形如:a=[4,2,3‾,5‾,10,6‾,4‾,2‾,1]a=[4,2,\underline{\textbf{3}},\underline{\textbf{5}},10,\underline{\textbf{6}},\underline{\textbf{4}},\underline{\textbf{2}},1](存在其他可行的构造方式)。

输入格式

The first line contains one integer tt (1≤t≤10001 \le t \le 1000). Then tt test cases follow.

The first line of each test case contains one integer nn (2≤n≤502 \le n \le 50) — the length of the array aa.

The next line contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (1≤ai≤501 \le a_i \le 50).

第一行包含一个整数 tt(1≤t≤10001 \le t \le 1000)。随后是 tt 组测试数据。

每组测试数据的第一行包含一个整数 nn(2≤n≤502 \le n \le 50)—— 表示数组 aa 的长度。

下一行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(1≤ai≤501 \le a_i \le 50)。

输出格式

For each test case, output one integer — the minimum number of numbers that must be added to the array to make it dense.

对于每个测试用例,输出一个整数——为使数组变为稠密数组所需添加的最少数字个数。

输入输出样例

  • 输入#1

    6
    4
    4 2 10 1
    2
    1 3
    2
    6 1
    3
    1 4 2
    5
    1 2 3 4 3
    12
    4 31 25 50 30 20 34 46 42 16 15 16

    输出#1

    5
    1
    2
    1
    0
    3

说明/提示

The first test case is explained in the statements.

In the second test case, you can insert one element, a=[1,2‾,3]a=[1,\underline{\textbf{2}},3].

In the third test case, you can insert two elements, a=[6,4‾,2‾,1]a=[6,\underline{\textbf{4}},\underline{\textbf{2}},1].

In the fourth test case, you can insert one element, a=[1,2‾,4,2]a=[1,\underline{\textbf{2}},4,2].

In the fifth test case, the array aa is already dense.

第一个测试用例在题目描述中已作解释。

在第二个测试用例中,你可以插入一个元素:a=[1,2‾,3]a=[1,\underline{\textbf{2}},3]。

在第三个测试用例中,你可以插入两个元素:a=[6,4‾,2‾,1]a=[6,\underline{\textbf{4}},\underline{\textbf{2}},1]。

在第四个测试用例中,你可以插入一个元素:a=[1,2‾,4,2]a=[1,\underline{\textbf{2}},4,2]。

在第五个测试用例中,数组 aa 已经是稠密的。

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

首页