CF1685D2.Permutation Weight (Hard Version)

NOI/NOI+/CTSC

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

This is a hard version of the problem. The difference between the easy and hard versions is that in this version, you have to output the lexicographically smallest permutation with the smallest weight.

You are given a permutation p1,p2,…,pnp_1, p_2, \ldots, p_n of integers from 11 to nn.

Let's define the weight of the permutation q1,q2,…,qnq_1, q_2, \ldots, q_n of integers from 11 to nn as $$|q_1 - p_{q_{2}}| + |q_2 - p_{q_{3}}| + \ldots + |q_{n-1} - p_{q_{n}}| + |q_n - p_{q_{1}}|$$

You want your permutation to be as lightweight as possible. Among the permutations qq with the smallest possible weight, find the lexicographically smallest.

Permutation a1,a2,…,ana_1, a_2, \ldots, a_n is lexicographically smaller than permutation b1,b2,…,bnb_1, b_2, \ldots, b_n, if there exists some 1≤i≤n1 \le i \le n such that aj=bja_j = b_j for all 1≤j<i1 \le j \lt i and ai<bia_i \lt b_i.

这是一个该问题的困难版本。简单版本与困难版本的区别在于:在本版本中,你需要输出权重最小的字典序最小的排列。

给你一个 11 到 nn 的整数排列 p1,p2,…,pnp_1, p_2, \ldots, p_n。

我们定义 11 到 nn 的整数排列 q1,q2,…,qnq_1, q_2, \ldots, q_n 的权重为

∣q_1−p_q_2∣+∣q_2−p_q_3∣+…+∣q_n−1−p_q_n∣+∣q_n−p_q_1∣|q\_1 - p\_{q\_{2}}| + |q\_2 - p\_{q\_{3}}| + \ldots + |q\_{n-1} - p\_{q\_{n}}| + |q\_n - p\_{q\_{1}}|

你希望你的排列尽可能轻(即权重尽可能小)。在所有具有最小可能权重的排列 qq 中,请找出字典序最小的一个。

排列 a1,a2,…,ana_1, a_2, \ldots, a_n 字典序小于排列 b1,b2,…,bnb_1, b_2, \ldots, b_n,当且仅当存在某个 1≤i≤n1 \le i \le n,使得对所有 1≤j<i1 \le j < i 均有 aj=bja_j = b_j,且 ai<bia_i < b_i。

输入格式

The first line of the input contains a single integer tt (1≤t≤1001 \le t \le 100) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer nn (2≤n≤2002 \le n \le 200) — the size of the permutation.

The second line of each test case contains nn integers p1,p2,…,pnp_1, p_2, \ldots, p_n (1≤pi≤n1 \le p_i \le n, all pip_i are distinct) — the elements of the permutation.

The sum of nn over all test cases doesn't exceed 400400.

输入的第一行包含一个整数 tt(1≤t≤1001 \le t \le 100),表示测试用例的数量。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(2≤n≤2002 \le n \le 200),表示排列的长度。

每个测试用例的第二行包含 nn 个整数 p1,p2,…,pnp_1, p_2, \ldots, p_n(1≤pi≤n1 \le p_i \le n,且所有 pip_i 互不相同),表示该排列的各个元素。

所有测试用例的 nn 值之和不超过 400400。

输出格式

For each test case, output nn integers q1,q2,…,qnq_1, q_2, \ldots, q_n (1≤qi≤n1 \le q_i \le n, all qiq_i are distinct) — the lexicographically smallest permutation with the smallest weight.

对于每个测试用例,输出 nn 个整数 q1,q2,…,qnq_1, q_2, \ldots, q_n(其中 1≤qi≤n1 \le q_i \le n,且所有 qiq_i 互不相同)——即具有最小权值的字典序最小的排列。

输入输出样例

  • 输入#1

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

    输出#1

    1 2 
    1 3 4 2 
    1 3 4 2 5

说明/提示

In the first test case, there are two permutations of length 22: (1,2)(1, 2) and (2,1)(2, 1). Permutation (1,2)(1, 2) has weight ∣1−p2∣+∣2−p1∣=0|1 - p_2| + |2 - p_1| = 0, and the permutation (2,1)(2, 1) has the same weight: ∣2−p1∣+∣1−p2∣=0|2 - p_1| + |1 - p_2| = 0. In this version, you have to output the lexicographically smaller of them — (1,2)(1, 2).

In the second test case, the weight of the permutation (1,3,4,2)(1, 3, 4, 2) is ∣1−p3∣+∣3−p4∣+∣4−p2∣+∣2−p1∣=∣1−1∣+∣3−4∣+∣4−3∣+∣2−2∣=2|1 - p_3| + |3 - p_4| + |4 - p_2| + |2 - p_1| = |1 - 1| + |3 - 4| + |4 - 3| + |2 - 2| = 2. There are no permutations with smaller weights.

In the third test case, the weight of the permutation (1,3,4,2,5)(1, 3, 4, 2, 5) is ∣1−p3∣+∣3−p4∣+∣4−p2∣+∣2−p5∣+∣5−p1∣=∣1−3∣+∣3−2∣+∣4−4∣+∣2−1∣+∣5−5∣=4|1 - p_3| + |3 - p_4| + |4 - p_2| + |2 - p_5| + |5 - p_1| = |1 - 3| + |3 - 2| + |4 - 4| + |2 - 1| + |5 - 5| = 4. There are no permutations with smaller weights.

在第一个测试用例中,长度为 22 的排列有两个:(1,2)(1, 2) 和 (2,1)(2, 1)。排列 (1,2)(1, 2) 的权重为 ∣1−p2∣+∣2−p1∣=0|1 - p_2| + |2 - p_1| = 0,而排列 (2,1)(2, 1) 具有相同的权重:∣2−p1∣+∣1−p2∣=0|2 - p_1| + |1 - p_2| = 0。在此版本中,你需要输出字典序更小的那个——即 (1,2)(1, 2)。

在第二个测试用例中,排列 (1,3,4,2)(1, 3, 4, 2) 的权重为 ∣1−p3∣+∣3−p4∣+∣4−p2∣+∣2−p1∣=∣1−1∣+∣3−4∣+∣4−3∣+∣2−2∣=2|1 - p_3| + |3 - p_4| + |4 - p_2| + |2 - p_1| = |1 - 1| + |3 - 4| + |4 - 3| + |2 - 2| = 2。不存在权重更小的排列。

在第三个测试用例中,排列 (1,3,4,2,5)(1, 3, 4, 2, 5) 的权重为 ∣1−p3∣+∣3−p4∣+∣4−p2∣+∣2−p5∣+∣5−p1∣=∣1−3∣+∣3−2∣+∣4−4∣+∣2−1∣+∣5−5∣=4|1 - p_3| + |3 - p_4| + |4 - p_2| + |2 - p_5| + |5 - p_1| = |1 - 3| + |3 - 2| + |4 - 4| + |2 - 1| + |5 - 5| = 4。不存在权重更小的排列。

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

首页