Codeforces Round #702 (Div. 3)-A题

在这里插入图片描述
在这里插入图片描述

如果相邻两数字大的比上小的大于2,就要插进去至少一个数字,因为比例最大为2,贪心的思想插入进去的数字是最小值的两倍,再判断新插入的数字和大的比较,如果不符合,重复上一步。

#include<bits/stdc++.h>
using namespace std;
int n;
int a[55];
int main()
{
    
    
	int t;
	cin>>t;
	while(t--)
	{
    
    
		int res=0;
		cin>>n;
		for(int i=0;i<n;i++)
		cin>>a[i];
		for(int i=0;i<n-1;i++)
		{
    
    
			int x=max(a[i],a[i+1]);
			int y=min(a[i],a[i+1]);
			if(x>2*y)
			{
    
    
				while(x>y*2)
				{
    
    
					res++;
					y*=2;
				}
			}
		}
		cout<<res<<endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_51768569/article/details/113834620