CF1561A.Simply Strange Sort
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You have a permutation: an array a=[a1,a2,…,an] of distinct integers from 1 to n . The length of the permutation n is odd.
Consider the following algorithm of sorting the permutation in increasing order.
A helper procedure of the algorithm, f(i) , takes a single argument i ( 1≤i≤n−1 ) and does the following. If ai>ai+1 , the values of ai and ai+1 are exchanged. Otherwise, the permutation doesn't change.
The algorithm consists of iterations, numbered with consecutive integers starting with 1 . On the i -th iteration, the algorithm does the following:
- if i is odd, call f(1),f(3),…,f(n−2) ;
- if i is even, call f(2),f(4),…,f(n−1) .
It can be proven that after a finite number of iterations the permutation will be sorted in increasing order.
After how many iterations will this happen for the first time?
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t ( 1≤t≤100 ). Description of the test cases follows.
The first line of each test case contains a single integer n ( 3≤n≤999 ; n is odd) — the length of the permutation.
The second line contains n distinct integers a1,a2,…,an ( 1≤ai≤n ) — the permutation itself.
It is guaranteed that the sum of n over all test cases does not exceed 999 .
输出格式
For each test case print the number of iterations after which the permutation will become sorted in increasing order for the first time.
If the given permutation is already sorted, print 0 .
输入输出样例
输入#1
3 3 3 2 1 7 4 5 7 1 3 2 6 5 1 2 3 4 5
输出#1
3 5 0
说明/提示
In the first test case, the permutation will be changing as follows:
- after the 1 -st iteration: [2,3,1] ;
- after the 2 -nd iteration: [2,1,3] ;
- after the 3 -rd iteration: [1,2,3] .
In the second test case, the permutation will be changing as follows:
- after the 1 -st iteration: [4,5,1,7,2,3,6] ;
- after the 2 -nd iteration: [4,1,5,2,7,3,6] ;
- after the 3 -rd iteration: [1,4,2,5,3,7,6] ;
- after the 4 -th iteration: [1,2,4,3,5,6,7] ;
- after the 5 -th iteration: [1,2,3,4,5,6,7] .
In the third test case, the permutation is already sorted and the answer is 0 .