CF1899E.Queue Sort
普及-
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Vlad found an array a of n 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,4], it will change as follows:
- [4,3,1,2,6,4];
- [3,1,2,6,4,4];
- [3,1,2,6,4,4];
- [3,1,2,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.
弗拉德找到了一个包含 n 个整数的数组 a,并决定将其按非递减顺序排序。
为此,弗拉德可以任意多次执行以下操作:
- 将数组的第一个元素取出,并将其插入到数组末尾;
- 然后将该元素与其前一个元素不断交换,直到它成为第一个元素,或直到它严格大于其前一个元素为止。
注意:上述两个动作共同构成一次操作;每次操作中,这两个动作都必须执行。
例如,对数组 [4,3,1,2,6,4] 执行一次该操作,过程如下:
- [4,3,1,2,6,4];
- [3,1,2,6,4,4];
- [3,1,2,6,4,4];
- [3,1,2,4,6,4]。
弗拉德没有时间执行所有操作,因此他请你判断:使数组有序所需的最少操作次数,或者判断这是不可能的。
输入格式
The first line of the input contains a single integer t (1≤t≤104) — the number of test cases. Descriptions of the test cases follow.
The first line of each test case contains a single integer n (1≤n≤2⋅105) — the length of the array.
The second line of each test case contains n integers a1,a2,a3,…,an (1≤ai≤109) — the elements of the array.
It is guaranteed that the sum of n over all testcases does not exceed 2⋅105.
输入的第一行包含一个整数 t(1≤t≤104),表示测试用例的数量。随后是各测试用例的描述。
每个测试用例的第一行包含一个整数 n(1≤n≤2⋅105),表示数组的长度。
每个测试用例的第二行包含 n 个整数 a1,a2,a3,…,an(1≤ai≤109),表示数组的元素。
保证所有测试用例中 n 的总和不超过 2⋅105。
输出格式
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 as the answer.
对于每个测试用例,输出一个整数——即排序该数组所需的最少操作次数。如果无法完成排序,则输出 −1 作为答案。
输入输出样例
输入#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
输入解题思路,AI测评打分。不知道怎么写?