Codeforces Round 740 (Div. 2, based on VK Cup 2021 - Final (Engine))
A
Simply Strange Sort
Codeforces
800
原题
←B →
翻译中…

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 \gt 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?

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 100$). Description of the test cases follows.

翻译中…

The first line of each test case contains a single integer $n$ ($3 \le n \le 999$; $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 $999$.

翻译中…

Output

翻译中…

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$.

翻译中…

Note

翻译中…

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$.

翻译中…

样例

Input #1
3
3
3 2 1
7
4 5 7 1 3 2 6
5
1 2 3 4 5
Output #1
3
5
0
InputCopy OutputCopy
题目信息
题号
1561A
时间限制
2 s
内存限制
512 MB
标签
暴力
模拟
排序

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。