Strange Sort
题意翻译
### 题面描述
有一个长度为 $ n $ 的**排列** $ a = [a_1, ..., a_n] $。保证 $ n $ 是奇数。
定义一次操作 $ f(i) $ 为:若 $ a_i > a_{i + 1} $,则将 $ a_i $ 和 $ a_{i + 1} $ 交换。
定义第 $ i $ **轮**操作为:
- 若 $ i $ 为奇数,则进行 $ f(1), f(3), ..., f(n - 2) $ 这些操作。
- 若 $ i $ 为偶数,则进行 $ f(2), f(4), ..., f(n - 1) $ 这些操作。
求最早在第几轮操作后,排列已经有序。轮次从 $ 1 $ 开始编号。
### 输入格式
第一行,数据组数 $ t $。
每组数据第一行,排列长度 $ n $。
第二行 $ n $ 个数 $ a_1, ..., a_n $,表示这个排列。
### 输出格式
每组数据输出一个数,表示使得整个排列有序的最早轮数。
如果排列本来就是有序的,输出 $ 0 $。
### 数据范围
$ 1 \leqslant t \leqslant 10^4, 3 \leqslant n \leqslant 2 \cdot 10^5 - 1, n \equiv 1 \pmod 2 $
保证给定的 $ [a_1, ..., a_n] $ 是一个 $ 1 $ 到 $ n $ 的排列,且对于所有数据,$ \sum n \leqslant 2 \cdot 10^5 - 1 $。
题目描述
You have a permutation: an array $ a = [a_1, a_2, \ldots, a_n] $ 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 \le i \le n-1 $ ) and does the following. If $ a_i > a_{i+1} $ , the values of $ a_i $ and $ a_{i+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), \ldots, f(n - 2) $ ;
- if $ i $ is even, call $ f(2), f(4), \ldots, 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 \le t \le 10^4 $ ). Description of the test cases follows.
The first line of each test case contains a single integer $ n $ ( $ 3 \le n \le 2 \cdot 10^5 - 1 $ ; $ n $ is odd) — the length of the permutation.
The second line contains $ n $ distinct integers $ a_1, a_2, \ldots, a_n $ ( $ 1 \le a_i \le n $ ) — the permutation itself.
It is guaranteed that the sum of $ n $ over all test cases does not exceed $ 2 \cdot 10^5 - 1 $ .
输出格式
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 $ .