当前位置: 首页 > news >正文

B. Make It Increasing

time limit per test

2 seconds

memory limit per test

256 megabytes

Given nn integers a1,a2,…,ana1,a2,…,an. You can perform the following operation on them:

  • select any element aiai (1≤i≤n1≤i≤n) and divide it by 22 (round down). In other words, you can replace any selected element aiai with the value ⌊ai2⌋⌊ai2⌋ (where ⌊x⌋⌊x⌋ is – round down the real number xx).

Output the minimum number of operations that must be done for a sequence of integers to become strictly increasing (that is, for the condition a1<a2<⋯<ana1<a2<⋯<an to be satisfied). Or determine that it is impossible to obtain such a sequence. Note that elements cannot be swapped. The only possible operation is described above.

For example, let n=3n=3 and a sequence of numbers [3,6,5][3,6,5] be given. Then it is enough to perform two operations on it:

  • Write the number ⌊62⌋=3⌊62⌋=3 instead of the number a2=6a2=6 and get the sequence [3,3,5][3,3,5];
  • Then replace a1=3a1=3 with ⌊32⌋=1⌊32⌋=1 and get the sequence [1,3,5][1,3,5].

The resulting sequence is strictly increasing because 1<3<51<3<5.

Input

The first line of the input contains an integer tt (1≤t≤1041≤t≤104) — the number of test cases in the input.

The descriptions of the test cases follow.

The first line of each test case contains a single integer nn (1≤n≤301≤n≤30).

The second line of each test case contains exactly nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤2⋅1090≤ai≤2⋅109).

Output

For each test case, print a single number on a separate line — the minimum number of operations to perform on the sequence to make it strictly increasing. If a strictly increasing sequence cannot be obtained, print "-1".

Example

Input

Copy

 

7

3

3 6 5

4

5 3 2 1

5

1 2 3 4 5

1

1000000000

4

2 8 7 5

5

8 26 5 21 10

2

5 14

Output

Copy

2
-1
0
0
4
11
0

Note

The first test case is analyzed in the statement.

In the second test case, it is impossible to obtain a strictly increasing sequence.

In the third test case, the sequence is already strictly increasing.

解题说明:此题是一道模拟题,按照题目意思,将数列中的数字除以2后保证数列递增。可以从数列队尾往前遍历计算,确保前面的数字小于后面的数字,直到数字为0(如果不是第一个数字,则此时将输出-1)或者遍历到第一个数字为止。

#include<iostream>
using namespace std;
int main() 
{
	int t = 0;
	cin >> t;
be:
	while (t--) 
	{
		int n = 0, a[32] = { 0 }, s = 0;
		cin >> n;
		for (int i = 0; i < n; i++)
		{
			cin >> a[i];
		}
		if (n == 1)
		{
			cout << "0\n";
		}
		else
		{
			for (int i = n - 2; i >= 0; i--)
			{
				if (a[i] == 0 && i != 0 || a[i + 1] == 0)
				{
					cout << "-1\n";
					goto be;
				}
				while (a[i] >= a[i + 1])
				{
					a[i] /= 2;
					s++;
				}
			}
			cout << s << "\n";
		}
	}
	return 0;
}

相关文章:

  • 【phpstudy】关于实现两个不同版本的mysql并存。
  • SpringBoot速成(12)文章分类P15-P19
  • JAVA集合
  • python 视频处理库moviepy 设置字幕
  • 【LeetCode Hot100 矩阵】矩阵置零、螺旋矩阵、旋转图像、搜索二维矩阵II
  • 开源协议深度解析:理解MIT、GPL、Apache等常见许可证
  • JavaScript 简介
  • 枚举类型Enum
  • DeepSeek-R1 + Cherry Studio 本地部署打造个人 AI 知识库
  • C语言01
  • Kimi k1.5:继Deepseek R1 后多模态 AI 的新标杆
  • 低成本、高效率且成熟的电商实时数据采集方案:梦想成真?
  • 放大镜效果
  • 图论算法篇:邻接矩阵以及邻接表和链式前向星建图
  • winfrom实现人脸识别比对功能
  • 大模型开发实战篇5:多模态--文生图模型API
  • 如何设置Python爬虫的User-Agent?
  • torch-xla动态shape——通过torch.nonzero分析mhlo实现
  • 第六天:requests库的用法
  • JS数组扁平化
  • 三大交易所多举措支持科创债再扩容,约160亿证券公司科创债有望近期落地
  • 重磅金融政策密集发布!一文梳理这场国新办发布会
  • 个人住房公积金贷款利率下调,100万元30年期贷款总利息将减少近5万元
  • 印巴冲突升级,巴防长称已击落5架印度战机
  • 象屿集团:对去化压力大、市场有效需求不足区域坚决暂停投资,打造多元上市路径
  • 印度导弹凌晨打击巴基斯坦多座设施,巴总理:正对战争行为作有力回应