CF1662E.Round Table

普及/提高-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

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 p1,p2,…,pnp_1, p_2, \dots, p_n. More specifically, you want to change the seats of the people so that at the end person pi+1p_{i+1} is sitting to the right of person pip_i (with person p1p_1 sitting to the right of person pnp_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 \le x \le 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.

有 nn 个人,编号为 11 到 nn,围坐在一张圆桌旁。其中,第 i+1i+1 号人坐在第 ii 号人的右侧(第 11 号人坐在第 nn 号人的右侧)。

你设计了一种更优的座位安排,该安排由一个排列 p1,p2,…,pnp_1, p_2, \dots, p_n 给出。具体而言,你希望通过对人员座位进行调整,使得最终第 pi+1p_{i+1} 号人坐在第 pip_i 号人的右侧(第 p1p_1 号人坐在第 pnp_n 号人的右侧)。注意:对任意一种座位安排,均有 nn 个不同的排列可描述它(这些排列彼此可通过旋转得到)。

为实现该目标,你每次只能交换相邻座位上的两个人;但有一个限制:对所有满足 1≤x≤n−11 \le x \le n-1 的 xx,你不能交换第 xx 号人与第 x+1x+1 号人(注意:你可以交换第 nn 号人与第 11 号人)。问:达成目标所需的最少交换次数是多少?可以证明,任意一种座位安排均能实现。

输入格式

Each test contains multiple test cases. The first line contains an integer tt (1≤t≤10 0001\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 (3≤n≤200 0003 \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 (1≤pi≤n1 \le p_i \le n, pi≠pjp_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.

每个测试包含多个测试用例。第一行包含一个整数 tt(1≤t≤10 0001\le t\le 10\,000),表示测试用例的数量。接下来是 tt 个测试用例的描述。

每个测试用例的第一行包含一个整数 nn(3≤n≤200 0003 \le n \le 200\,000),表示围坐在桌旁的人数。

第二行包含 nn 个互不相同的整数 p1,p2,…,pnp_1, p_2, \dots, p_n(1≤pi≤n1 \le p_i \le n,且当 i≠ji \ne j 时 pi≠pjp_i \ne p_j),表示人们绕桌就座的期望最终顺序。

所有测试用例中 nn 的值之和不超过 200 000200\,000。

输出格式

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

在第一个测试用例中,我们可以在初始排列中交换相邻的人 44 和人 11,从而得到排列 [4,2,3,1][4, 2, 3, 1],该排列与目标排列等价。因此,在本例中,一次交换即足够。

输入解题思路,AI测评打分。不知道怎么写?

首页