bonus

Time limit: 0.1s Memory limit: 256MB Input: Output:

As it would be way too easy to have to solve just 77 problems in a contest, the scientific committee decided to give you a permutation too. But because we live in the present, we want, from a moral standpoint, to give it further, in a modified version, so that it respects certain properties.

Task

You are given a permutation. Make as few swaps of adjacent values as possible, in such a way that for the final permutation, PiPi1=1|P_i - P_{i-1}| = 1 for at least N2N - 2 indexes between 22 and NN.

Input data

The first line of the input will contain TT, the number of test cases. Each test case contains on the first line the order of the permutation, NN and on the second line the permutation, formed of NN distinct values between 11 and NN.

Output data

The output will contain TT lines, on each line you will print the minimal answer for the corresponding test case.

Constraints and clarifications

  • 1T51 \leq T \leq 5
  • 1N50 0001 \leq N \leq 50 \ 000
  • For 1010 points, N7N \leq 7
  • For 2020 more points, N50N \leq 50
  • For 1010 more points, one of the permutations for which we can obtain the minimum number of swaps is the identity permutation (1,2,3,,N)(1, 2, 3, \dots, N).
  • For 1010 more points, the answer is at most 22

Example

stdin

5
4
1 2 3 4
4
4 1 2 3
4
2 3 4 1
5
3 2 1 5 4
6
1 2 4 3 5 6

stdout

0
0
0
4
1

Explanation

We have T=5T = 5 tests. In the first 33 tests, the permutations already respect the given property, therefore there is no need of any swaps.

The fourth permutation needs 44 swaps of adjacent values to be brought to either 1 2 3 4 51 \ 2 \ 3 \ 4 \ 5 or 2 3 4 5 12 \ 3 \ 4 \ 5 \ 1.

For the fifth permutation it is enough to swap 33 and 44 to obtain the identical permutation.

Log in or sign up to be able to send submissions!