Given any array ~x~ consisting of ~n~ positive integers, we define the score of array ~x~ as
$$\max_{1 \le i < j \le n} \operatorname{LCP}(x_{i:n}, x_{j:n})$$
where ~x_{l:r}~ denotes the subarray ~[x_l, x_{l + 1}, \ldots, x_r]~ of ~x~, and ~\operatorname{LCP}(b, c)~ returns the length of the longest common prefix of arrays ~b~ and ~c~.
You are given an array ~a~ of ~n~ positive integers. Rearrange the array ~a~ so that its score is as large as possible.
~\operatorname{LCP}(b, c)~ is a function that returns ~i - 1~, where ~1 \le i \le \min(|b|, |c|)~ is the first position such that ~b_i \neq c_i~, or returns ~\min(|b|, |c|)~ if such a position does not exist.
Input
Each input consists of multiple test cases. The first line contains the number of test cases ~t~ (~1 \le t \le 10^5~). The description of each test case is as follows.
The first line of each test case contains an integer ~n~ (~2 \le n \le 5 \cdot 10^5~) — the length of the array ~a~.
The second line of each test case contains ~n~ positive integers ~a_1, \ldots, a_n~ (~1 \le a_i \le n~) – the elements of array ~a~.
It is guaranteed that the sum of ~n~ over all test cases does not exceed ~5 \cdot 10^5~.
Output
For each test case, print two lines.
The first line should contain a single integer — the maximum possible score after rearranging the elements of ~a~.
The second line should contain ~n~ integers ~a^\prime_1, a^\prime_2, \ldots, a^\prime_n~, where ~a^\prime~ is an array that can be obtained by rearranging the elements of ~a~ to achieve the maximum score.
If there are multiple ways to rearrange the array to achieve the maximum possible score, output any one of them.
Scoring
Subtask | Points | Constraints |
---|---|---|
1 | ~500~ | ~1 \le a_i \le 2~ |
2 | ~1000~ | The sum of ~n~ over all test cases does not exceed ~5000~ |
3 | ~1250~ | No additional constraints |
Total | ~2750~ |
Sample Input 1
6
7
1 3 2 2 1 4 3
7
1 3 2 2 1 4 1
4
1 2 3 4
9
6 6 9 9 6 9 9 9 9
13
11 7 12 7 12 12 12 4 12 7 7 4 7
10
2 1 1 9 1 9 1 2 1 1
Sample Output 1
3
1 3 2 4 1 3 2
3
4 1 2 1 2 1 3
0
1 2 3 4
6
9 6 9 9 6 9 9 6 9
8
11 4 7 12 7 12 7 12 7 12 7 12 4
6
1 1 2 9 1 1 2 9 1 1
Sample Input 2
3
5
1 1 1 1 2
6
2 1 2 1 1 1
32
1 2 1 2 1 2 2 1 1 2 1 1 1 1 2 2 1 1 2 2 1 1 2 1 1 1 2 2 1 2 1 2
Sample Output 2
3
2 1 1 1 1
3
2 2 1 1 1 1
27
1 1 1 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1
Notes
In the first test case, the score of the array ~[1, 3, 2, 4, 1, 3, 2]~ is ~3~, because if we take ~i = 1~ and ~j = 5~ then $$\operatorname{LCP}(a_{i:n}, a_{j:n}) = \operatorname{LCP}([1, 3, 2, 4, 1, 3, 2], [1, 3, 2]) = 3$$ and there is no pair ~i~ and ~j~ that gives a higher result. This is also the maximum possible score for any rearrangement of ~a~. Another rearrangement that gives a score of ~3~ is ~[4, 3, 2, 1, 3, 2, 1]~.
In the second test case, the score of the array ~[4, 1, 2, 1, 2, 1, 3]~ is ~3~, because for ~i = 2~ and ~j = 4~ we have $$\operatorname{LCP}(a_{i:n}, a_{j:n}) = \operatorname{LCP}([1, 2, 1, 2, 1, 3], [1, 2, 1, 3]) = 3$$
In the third test case, any rearrangement gives a score of ~0~.
Comments