309941: CF1762G. Unequal Adjacent Elements

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

Description

Unequal Adjacent Elements

题目描述

You are given an array $ a $ consisting of $ n $ positive integers. Find any permutation $ p $ of $ [1,2,\dots,n] $ such that: - $ p_{i-2} < p_i $ for all $ i $ , where $ 3 \leq i \leq n $ , and - $ a_{p_{i-1}} \neq a_{p_i} $ for all $ i $ , where $ 2 \leq i \leq n $ . Or report that no such permutation exists.

输入输出格式

输入格式


Each test contains multiple test cases. The first line contains a single integer $ t $ ( $ 1 \leq t \leq 10^5 $ ) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $ n $ ( $ 3 \leq n \leq 3 \cdot 10^5 $ ) — the length of the array $ a $ . The second line of each test case contains $ n $ space-separated integers $ a_1,a_2,\ldots,a_n $ ( $ 1 \leq a_i \leq n $ ) — representing the array $ a $ . It is guaranteed that the sum of $ n $ over all test cases does not exceed $ 3 \cdot 10^5 $ .

输出格式


For each test case, output "NO" if no such permutation exists, otherwise output "YES" in the first line and print the permutation $ p $ in the next line. In case there are multiple permutations, print any one of them. You can output "YES" and "NO" in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive response).

输入输出样例

输入样例 #1

4
3
1 2 1
4
1 2 3 4
3
1 1 1
7
1 2 1 1 3 1 4

输出样例 #1

YES
1 2 3
YES
3 1 4 2
NO
YES
1 2 3 5 4 7 6

说明

In the first test case, $ p=[1,2,3] $ is the only permutation of $ [1,2,3] $ that satisfy the given constraints. In the second test case, $ [1,3,2,4] $ , $ [2,1,4,3] $ and some other permutations are also acceptable. In the third test case, it can be proved that there does not exist any permutation of $ [1,2,3] $ satisfying the given constraints.

Input

题意翻译

给定 $n$ 个正整数构成的序列 $a$,构造排列 $p$ 满足: * $\forall i\in [3,n],p_{i-2}<p_i$。 * $\forall i\in[2,n],a_{p_{i-1}}\neq a_{p_i}$。 若有解则构造,若无解则报告无解。

Output

**题目描述**

给定一个由 $ n $ 个正整数组成的数组 $ a $。

找到一个 $ [1,2,\dots,n] $ 的排列 $ p $ 使得:

- 对于所有 $ 3 \leq i \leq n $,满足 $ p_{i-2} < p_i $,
- 对于所有 $ 2 \leq i \leq n $,满足 $ a_{p_{i-1}} \neq a_{p_i} $。

如果不存在这样的排列,则报告。

**输入输出格式**

**输入格式**

每个测试包含多个测试案例。第一行包含一个整数 $ t $($ 1 \leq t \leq 10^5 $)——测试案例的数量。接下来是每个测试案例的描述。

每个测试案例的第一行包含一个整数 $ n $($ 3 \leq n \leq 3 \cdot 10^5 $)——数组 $ a $ 的长度。

每个测试案例的第二行包含 $ n $ 个由空格分隔的整数 $ a_1, a_2, \ldots, a_n $($ 1 \leq a_i \leq n $)——代表数组 $ a $。

保证所有测试案例的 $ n $ 之和不超过 $ 3 \cdot 10^5 $。

**输出格式**

对于每个测试案例,如果不存在这样的排列,则输出 "NO",否则在第一行输出 "YES",并在下一行打印排列 $ p $。

如果有多个排列,则输出其中任意一个。

"YES" 和 "NO" 的输出可以为任意大小写形式(例如,"yEs"、"yes"、"Yes" 和 "YES" 都将被识别为肯定的响应)。

**输入输出样例**

**输入样例 #1**
```
4
3
1 2 1
4
1 2 3 4
3
1 1 1
7
1 2 1 1 3 1 4
```

**输出样例 #1**
```
YES
1 2 3
YES
3 1 4 2
NO
YES
1 2 3 5 4 7 6
```

**说明**

在第一个测试案例中,$ p=[1,2,3] $ 是唯一满足给定约束的 $ [1,2,3] $ 的排列。

在第二个测试案例中,$ [1,3,2,4] $、$ [2,1,4,3] $ 等其他排列也是可接受的。

在第三个测试案例中,可以证明不存在任何排列 $ [1,2,3] $ 满足给定约束。**题目描述** 给定一个由 $ n $ 个正整数组成的数组 $ a $。 找到一个 $ [1,2,\dots,n] $ 的排列 $ p $ 使得: - 对于所有 $ 3 \leq i \leq n $,满足 $ p_{i-2} < p_i $, - 对于所有 $ 2 \leq i \leq n $,满足 $ a_{p_{i-1}} \neq a_{p_i} $。 如果不存在这样的排列,则报告。 **输入输出格式** **输入格式** 每个测试包含多个测试案例。第一行包含一个整数 $ t $($ 1 \leq t \leq 10^5 $)——测试案例的数量。接下来是每个测试案例的描述。 每个测试案例的第一行包含一个整数 $ n $($ 3 \leq n \leq 3 \cdot 10^5 $)——数组 $ a $ 的长度。 每个测试案例的第二行包含 $ n $ 个由空格分隔的整数 $ a_1, a_2, \ldots, a_n $($ 1 \leq a_i \leq n $)——代表数组 $ a $。 保证所有测试案例的 $ n $ 之和不超过 $ 3 \cdot 10^5 $。 **输出格式** 对于每个测试案例,如果不存在这样的排列,则输出 "NO",否则在第一行输出 "YES",并在下一行打印排列 $ p $。 如果有多个排列,则输出其中任意一个。 "YES" 和 "NO" 的输出可以为任意大小写形式(例如,"yEs"、"yes"、"Yes" 和 "YES" 都将被识别为肯定的响应)。 **输入输出样例** **输入样例 #1** ``` 4 3 1 2 1 4 1 2 3 4 3 1 1 1 7 1 2 1 1 3 1 4 ``` **输出样例 #1** ``` YES 1 2 3 YES 3 1 4 2 NO YES 1 2 3 5 4 7 6 ``` **说明** 在第一个测试案例中,$ p=[1,2,3] $ 是唯一满足给定约束的 $ [1,2,3] $ 的排列。 在第二个测试案例中,$ [1,3,2,4] $、$ [2,1,4,3] $ 等其他排列也是可接受的。 在第三个测试案例中,可以证明不存在任何排列 $ [1,2,3] $ 满足给定约束。

加入题单

算法标签: