309475: CF1685D1. Permutation Weight (Easy Version)

Memory Limit:256 MB Time Limit:1 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Permutation Weight (Easy Version)

题意翻译

**这是本题的简单版本。** **简单版本和困难版本的区别在于对你所构造的排列的限制。** **在本题中你可以输出任意一个权值最小化的排列。** ### 题目描述 给定一个 $1\sim n$ 的排列 $p$。 对于一个 $1\sim n$ 的排列 $q$,定义其权值为: $$|q_1-p_{q_2}|+|q_2-p_{q_3}|+|q_3-p_{q_4}|+\cdots+|q_{n-1}-p_{q_n}|+|q_n-p_{q_1}|$$ 找出 **任意一个** 权值最小化的 $1\sim n$ 的排列 $q$。 每个测试点包含 $t$ 组数据。 ### 输入格式 第一行输入一个整数 $t(1\leq t\leq100)$ 表示数据组数,接下来对于每组数据: 第一行输入一个整数 $n(2\leq n\leq200;\sum n\leq400)$ 表示排列长度。 接下来一行输入 $n$ 个整数表示排列 $p(1\leq p_i\leq n)$。 ### 输出格式 对于每组数据: 输出 $n$ 个整数表示你求出的权值最小化的排列 $q$。

题目描述

This is an easy version of the problem. The difference between the easy and hard versions is that in this version, you can output any permutation with the smallest weight. You are given a permutation $ p_1, p_2, \ldots, p_n $ of integers from $ 1 $ to $ n $ . Let's define the weight of the permutation $ q_1, q_2, \ldots, q_n $ of integers from $ 1 $ to $ n $ as $ $|q_1 - p_{q_{2}}| + |q_2 - p_{q_{3}}| + \ldots + |q_{n-1} - p_{q_{n}}| + |q_n - p_{q_{1}}| $ $ </p><p>You want your permutation to be as lightweight as possible. Find any permutation $ q$ with the smallest possible weight.

输入输出格式

输入格式


The first line of the input contains a single integer $ t $ ( $ 1 \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 $ n $ ( $ 2 \le n \le 200 $ ) — the size of the permutation. The second line of each test case contains $ n $ integers $ p_1, p_2, \ldots, p_n $ ( $ 1 \le p_i \le n $ , all $ p_i $ are distinct) — the elements of the permutation. The sum of $ n $ over all test cases doesn't exceed $ 400 $ .

输出格式


For each test case, output $ n $ integers $ q_1, q_2, \ldots, q_n $ ( $ 1 \le q_i \le n $ , all $ q_i $ are distinct) — one of the permutations with the smallest weight.

输入输出样例

输入样例 #1

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

输出样例 #1

1 2 
1 3 4 2 
1 4 2 3 5

说明

In the first test case, there are two permutations of length $ 2 $ : $ (1, 2) $ and $ (2, 1) $ . Permutation $ (1, 2) $ has weight $ |1 - p_2| + |2 - p_1| = 0 $ , and permutation $ (2, 1) $ has the same weight: $ |2 - p_1| + |1 - p_2| = 0 $ . You can output any of these permutations in this version. In the second test case, the weight of the permutation $ (1, 3, 4, 2) $ is $ |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, 4, 2, 3, 5) $ is $ |1 - p_4| + |4 - p_2| + |2 - p_3| + |3 - p_5| + |5 - p_1| = |1 - 2| + |4 - 4| + |2 - 3| + |3 - 1| + |5 - 5| = 4 $ . There are no permutations with smaller weights.

Input

题意翻译

**这是本题的简单版本。** **简单版本和困难版本的区别在于对你所构造的排列的限制。** **在本题中你可以输出任意一个权值最小化的排列。** ### 题目描述 给定一个 $1\sim n$ 的排列 $p$。 对于一个 $1\sim n$ 的排列 $q$,定义其权值为: $$|q_1-p_{q_2}|+|q_2-p_{q_3}|+|q_3-p_{q_4}|+\cdots+|q_{n-1}-p_{q_n}|+|q_n-p_{q_1}|$$ 找出 **任意一个** 权值最小化的 $1\sim n$ 的排列 $q$。 每个测试点包含 $t$ 组数据。 ### 输入格式 第一行输入一个整数 $t(1\leq t\leq100)$ 表示数据组数,接下来对于每组数据: 第一行输入一个整数 $n(2\leq n\leq200;\sum n\leq400)$ 表示排列长度。 接下来一行输入 $n$ 个整数表示排列 $p(1\leq p_i\leq n)$。 ### 输出格式 对于每组数据: 输出 $n$ 个整数表示你求出的权值最小化的排列 $q$。

Output

**题目大意:**

这是一个简化版本的排列权重问题。在这个问题中,你需要找到一个权值最小的排列 $ q $。

给定一个 $1 \sim n$ 的排列 $ p $。对于 $1 \sim n$ 的任意排列 $ q $,其权值定义为:

$$
|q_1 - p_{q_2}| + |q_2 - p_{q_3}| + \cdots + |q_{n-1} - p_{q_n}| + |q_n - p_{q_1}|
$$

你的任务是找到任意一个权值最小的排列 $ q $。

**输入格式:**

第一行包含一个整数 $ t $($ 1 \le t \le 100 $),表示测试用例的数量。每个测试用例包含两行:

- 第一行包含一个整数 $ n $($ 2 \le n \le 200 $),表示排列的长度。
- 第二行包含 $ n $ 个不同的整数,表示排列 $ p $。

所有测试用例的 $ n $ 之和不超过 $ 400 $。

**输出格式:**

对于每个测试用例,输出一行包含 $ n $ 个整数,表示你找到的权值最小的排列 $ q $。

**输入输出样例:**

**输入样例 #1:**

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

**输出样例 #1:**

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

**说明:**

- 在第一个测试用例中,长度为 2 的排列有两种:(1, 2) 和 (2, 1)。这两个排列的权值都是 0,所以输出其中任意一个都是正确的。
- 在第二个测试用例中,排列 (1, 3, 4, 2) 的权值是 2,没有其他排列的权值更小。
- 在第三个测试用例中,排列 (1, 4, 2, 3, 5) 的权值是 4,没有其他排列的权值更小。**题目大意:** 这是一个简化版本的排列权重问题。在这个问题中,你需要找到一个权值最小的排列 $ q $。 给定一个 $1 \sim n$ 的排列 $ p $。对于 $1 \sim n$ 的任意排列 $ q $,其权值定义为: $$ |q_1 - p_{q_2}| + |q_2 - p_{q_3}| + \cdots + |q_{n-1} - p_{q_n}| + |q_n - p_{q_1}| $$ 你的任务是找到任意一个权值最小的排列 $ q $。 **输入格式:** 第一行包含一个整数 $ t $($ 1 \le t \le 100 $),表示测试用例的数量。每个测试用例包含两行: - 第一行包含一个整数 $ n $($ 2 \le n \le 200 $),表示排列的长度。 - 第二行包含 $ n $ 个不同的整数,表示排列 $ p $。 所有测试用例的 $ n $ 之和不超过 $ 400 $。 **输出格式:** 对于每个测试用例,输出一行包含 $ n $ 个整数,表示你找到的权值最小的排列 $ q $。 **输入输出样例:** **输入样例 #1:** ``` 3 2 2 1 4 2 3 1 4 5 5 4 3 2 1 ``` **输出样例 #1:** ``` 1 2 1 3 4 2 1 4 2 3 5 ``` **说明:** - 在第一个测试用例中,长度为 2 的排列有两种:(1, 2) 和 (2, 1)。这两个排列的权值都是 0,所以输出其中任意一个都是正确的。 - 在第二个测试用例中,排列 (1, 3, 4, 2) 的权值是 2,没有其他排列的权值更小。 - 在第三个测试用例中,排列 (1, 4, 2, 3, 5) 的权值是 4,没有其他排列的权值更小。

加入题单

算法标签: