아직 만들고 있는 페이지입니다.

이 페이지는 아직 만드는 중입니다. 보이는 내용은 바뀔 수 있습니다.

Round Table

시간 제한2초메모리 제한2048 MB

요약
연속한 번호끼리의 교환은 금지되고 n과 1만 허용될 때, 원형 좌석을 주어진 순서로 바꾸는 최소 교환 횟수를 구한다.
난이도

보통10점 중 7점

유형
그리디, 구현, 수학
정답자
아직 제출이 없습니다

문제

There are nn people, numbered from 11 to nn, sitting at a round table. Person i+1i + 1 is sitting to the right of person ii (with person 11 sitting to the right of person nn).

You have come up with a better seating arrangement, which is given as a permutation p_1p\_1, p_2p\_2, …\dots, p_np\_n. More specifically, you want to change the seats of the people so that at the end person p_i+1p\_{i+1} is sitting to the right of person p_ip\_i (with person p_1p\_1 sitting to the right of person p_np\_n). Notice that for each seating arrangement there are nn 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 1≤x≤n−11 ≤ x ≤ n - 1 you cannot swap person xx and person x+1x + 1 (notice that you can swap person nn and person 11). 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 tt (1≤t≤10,0001 ≤ t ≤ 10\\,000) — the number of test cases. The descriptions of the tt test cases follow.

The first line of each test case contains a single integer nn (3≤n≤200,0003 ≤ n ≤ 200\\,000) — the number of people sitting at the table.

The second line contains nn distinct integers p_1p\_1, p_2p\_2, …\dots, p_np\_n (1≤p_i≤n1 ≤ p\_i ≤ n, p_i≠p_jp\_i \ne p\_j for i≠ji \ne j) — the desired final order of the people around the table.

The sum of the values of nn over all test cases does not exceed 200,000200\\,000.

출력

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

예제1

  1. 예제 1

    입력
    3
    4
    2 3 1 4
    5
    5 4 3 2 1
    7
    4 1 6 5 3 7 2
    
    예상 출력
    1
    10
    22