欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 教育 > 幼教 > B. Make It Increasing

B. Make It Increasing

2025/2/24 14:59:46 来源:https://blog.csdn.net/jj12345jj198999/article/details/145671348  浏览:    关键词: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;
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词