CF BUDDY
← Problems·

1995C · Squaring

1800 · brute force, constructive algorithms, greedy

Problem: ikrpprpp found an array aa consisting of integers. He likes justice, so he wants to make aa fair — that is, make it non-decreasing. To do that, he can perform an act of justice on an index 1in1 \le i \le n of the array, which will replace aia_i with ai2a_i ^ 2 (the element at position ii with its square). For example, if a=[2,4,3,3,5,3]a = [2,4,3,3,5,3] and ikrpprpp chooses to perform an act of justice on i=4i = 4, aa becomes [2,4,3,9,5,3][2,4,3,9,5,3].

What is the minimum number of acts of justice needed to make the array non-decreasing?

Input Format: First line contains an integer tt (1t10001 \le t \le 1000) — the number of test cases. It is followed by the description of test cases.

For each test case, the first line contains an integer nn — size of the array aa. The second line contains nn (1n21051 \le n \le 2 \cdot 10 ^5) integers a1,a2,,ana_1, a_2,\ldots, a_n (1ai1061 \le a_i \le 10 ^ 6).

The sum of nn over all test cases does not exceed 21052 \cdot {10}^5.

Output Format: For each testcase, print an integer — minimum number of acts of justice required to make the array aa non-decreasing. If it is impossible to do that, print 1-1.

Note: In the first test case, there's no need to perform acts of justice. The array is fair on its own!

In the third test case, it can be proven that the array cannot become non-decreasing.

In the fifth test case, ikrpprppp can perform an act of justice on index 3, then an act of justice on index 2, and finally yet another act of justice on index 3. After that, aa will become [4,9,16][4, 9, 16].

Sample Cases

Case 1

Input

7
3
1 2 3
2
3 2
3
3 1 5
4
1 1 2 3
3
4 3 2
9
16 2 4 2 256 2 4 2 8
11
10010 10009 10008 10007 10006 10005 10004 10003 10002 10001 10000

Output

0
1
-1
0
3
15
55

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback