logo AlgoBeat OnlineJudge
登录 注册

#215338. [SWERC 2021] Round Table

内存限制:2048 MiB 时间限制:2000 ms 标准输入输出
题目类型:VJudge(洛谷) 评测方式:VJudge
上传者: 匿名

题目描述

There are people, numbered from to , sitting at a round table. Person is sitting to the right of person (with person sitting to the right of person ).

You have come up with a better seating arrangement, which is given as a permutation . More specifically, you want to change the seats of the people so that at the end person is sitting to the right of person (with person sitting to the right of person ). Notice that for each seating arrangement there are permutations that describe it (which can be obtained by rotations).

In order to achieve that, you can swap two people sitting at adjacent places; but there is a catch: for all you cannot swap person and person (notice that you can swap person and person ). What is the minimum number of swaps necessary? It can be proven that any arrangement can be achieved.

输入格式

Each test contains multiple test cases. The first line contains an integer ( ) — the number of test cases. The descriptions of the test cases follow.

The first line of each test case contains a single integer ( ) — the number of people sitting at the table.

The second line contains distinct integers ( , for ) — the desired final order of the people around the table.

The sum of the values of over all test cases does not exceed .

输出格式

For each test case, print the minimum number of swaps necessary to achieve the desired order.

样例

样例输入 1

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

样例输出 1

1
10
22

数据范围与提示

In the first test case, we can swap person and person (who are adjacent) in the initial configuration and get the order which is equivalent to the desired one. Hence in this case a single swap is sufficient.