Dense Array

#Dense Array# 标题

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 (), this condition must be satisfied:

For example, the arrays , and are dense. And the arrays , , are not
dense.

You are given an array of 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 , then the answer is , and the array itself after
inserting elements into it may look like this: (there are other ways
to build such ).

Input The first line contains one integer (). Then test cases
follow.

The first line of each test case contains one integer () — the length
of the array .

The next line contains integers ().

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

Example
inputCopy

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

outputCopy

5
1
2
1
0
3

Note The first test case is explained in the statements.

In the second test case, you can insert one element, .

In the third test case, you can insert two elements, .

In the fourth test case, you can insert one element, .

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

#include<stdio.h>
using namespace std;
#include <algorithm>
int main()
{
    int a,b,c,d,s[100010];
    scanf("%d",&a);
    while(a--)
    {
        scanf("%d",&b);
        for(int i=0; i<b; i++)
        {
            scanf("%d",&s[i]);
        }
        c=0;
        for(int i=0; i<b-1; i++)
        {
            int min1,max1;
            min1=min(s[i],s[i+1]),max1=max(s[i],s[i+1]);
            if(max1>2*min1)
            {

                for(;;)
                {
                    if(max1>2*min1)
                    {

                        c++;
                        min1*=2;
                    }
                    else
                        break;
                }
            }
        }
        printf("%d\n",c);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Prime me

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值