#C1841. Minimum Transformations to Strictly Increasing Sequence

    ID: 45091 Type: Default 1000ms 256MiB

Minimum Transformations to Strictly Increasing Sequence

Minimum Transformations to Strictly Increasing Sequence

You are given an integer sequence \(a_1, a_2, \dots, a_n\). Your task is to determine the minimum number of transformations needed to make the sequence strictly increasing, i.e., \(a_1 < a_2 < \dots < a_n\).

In one transformation, you are allowed to swap two adjacent elements if the swap helps in achieving the strictly increasing order. Formally, when you encounter an index \(i\) (with \(2 \le i \le n\)) such that \(a_{i-1} \ge a_i\), you may swap \(a_{i-1}\) and \(a_i\) if either of the following conditions holds:

  • \(i \gt 1\) and \(a_{i-2} < a_i\), or
  • \(i \lt n\) and \(a_{i-1} < a_{i+1}\).

If a required swap is not possible, then the answer for that test case is \(-1\). Otherwise, output the minimum number of transformations (swaps) needed.

inputFormat

The input is given via standard input (stdin) and has the following format:

T
n1
a1 a2 ... an1
n2
b1 b2 ... bn2
...
nT
c1 c2 ... ncT

Here, \(T\) denotes the number of test cases. For each test case, the first line contains an integer \(n\) representing the number of elements in the sequence, followed by a line with \(n\) space-separated integers.

outputFormat

For each test case, output a single integer on a new line representing the minimum number of transformations required to make the sequence strictly increasing. If it is not possible, output \(-1\).

## sample
3
4
1 3 2 4
4
4 3 2 1
5
2 1 3 4 5
1

-1 1

</p>