CF1899E.Queue Sort

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Vlad found an array aa of nn integers and decided to sort it in non-decreasing order.

To do this, Vlad can apply the following operation any number of times:

  • Extract the first element of the array and insert it at the end;
  • Swap that element with the previous one until it becomes the first or until it becomes strictly greater than the previous one.

Note that both actions are part of the operation, and for one operation, you must apply both actions.

For example, if you apply the operation to the array [ 4,3,1,2,6,44, 3, 1, 2, 6, 4 ], it will change as follows:

  • [ 4,3,1,2,6,4\color{red}{4}, 3, 1, 2, 6, 4 ];
  • [ 3,1,2,6,4,43, 1, 2, 6, 4, \color{red}{4} ];
  • [ 3,1,2,6,4,43, 1, 2, 6, \color{red}{4}, 4 ];
  • [ 3,1,2,4,6,43, 1, 2, \color{red}{4}, 6, 4 ].

Vlad doesn't have time to perform all the operations, so he asks you to determine the minimum number of operations required to sort the array or report that it is impossible.

输入格式

The first line of the input contains a single integer tt ( 1t1041 \le t \le 10^4 ) — the number of test cases. Descriptions of the test cases follow.

The first line of each test case contains a single integer nn ( 1n21051 \le n \le 2 \cdot 10^5 ) — the length of the array.

The second line of each test case contains nn integers a1,a2,a3,,ana_1, a_2, a_3, \dots, a_n ( 1ai1091 \le a_i \le 10^9 ) — the elements of the array.

It is guaranteed that the sum of nn over all testcases does not exceed 21052 \cdot 10^5 .

输出格式

For each test case, output a single integer — the minimum number of operations needed to sort the array. If it is impossible to do so, output 1-1 as the answer.

输入输出样例

  • 输入#1

    5
    5
    6 4 1 2 5
    7
    4 5 3 7 8 6 2
    6
    4 3 1 2 6 4
    4
    5 2 4 2
    3
    2 2 3

    输出#1

    2
    6
    -1
    -1
    0
首页