B.MakeItIncreasing
- 游戏开发
- 2025-09-07 20:57:01

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 0Note
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; }B.MakeItIncreasing由讯客互联游戏开发栏目发布,感谢您对讯客互联的认可,以及对我们原创作品以及文章的青睐,非常欢迎各位朋友分享到个人网站或者朋友圈,但转载请说明文章出处“B.MakeItIncreasing”
上一篇
python的装饰器