CF1662E.Round Table

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Each test contains multiple test cases. The first line contains an integer tt ( 1t100001\le t\le 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 ( 3n2000003 \le n \le 200\,000 ) — the number of people sitting at the table.

The second line contains nn distinct integers p1,p2,,pnp_1, p_2, \dots, p_n ( 1pin1 \le p_i \le n , pipjp_i \ne p_j for iji \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 200000200\,000 .

输入格式

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

输出格式

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

输入输出样例

  • 输入#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 44 and person 11 (who are adjacent) in the initial configuration and get the order [4,2,3,1][4, 2, 3, 1] which is equivalent to the desired one. Hence in this case a single swap is sufficient.

首页