264 传统题

[CF2290C] 书架摆书 (Books on a Shelf)

时间限制
2000ms
内存限制
256MiB
登顶 / 尝试
1/ 1
难度
2

[CF2290C] 书架摆书 (Books on a Shelf)

Problem Statement

There are nn books, where the ii-th book has height hih_i. You may arrange all nn books in a single row on a shelf in any order.

A book is visible from the left if it is strictly taller than every book to its left (the leftmost book is always visible from the left). Similarly, a book is visible from the right if it is strictly taller than every book to its right (the rightmost book is always visible from the right).

Find the maximum possible number of books that are visible from at least one side (left or right), and output any arrangement achieving this maximum.

Input

The first line contains tt (1≤t≤1041 \le t \le 10^4) — the number of test cases.

Each test case consists of nn (1≤n≤2⋅1051 \le n \le 2 \cdot 10^5) on the first line, and nn integers h1,h2,…,hnh_1, h_2, \dots, h_n (1≤hi≤1091 \le h_i \le 10^9) on the second line.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052 \cdot 10^5.

Output

For each test case, print two lines:

  • First line: the maximum number of visible books kk.
  • Second line: nn integers representing any valid permutation of hh that achieves kk visible books.

Example

5
6
1 2 2 3 3 3
1
5
3
7 7 7
4
4 1 3 2
11
3 1 4 1 5 9 2 6 5 3 5
5
1 2 3 3 3 2
1
5
2
7 7 7
4
1 2 3 4
10
1 2 3 4 5 6 9 5 5 3 1

Note

  • A Special Judge is enabled: any valid rearrangement of the input heights yielding the optimal visible count kk is accepted.
  • Constraints: 1≤t≤1041 \le t \le 10^4, 1≤n≤2⋅1051 \le n \le 2 \cdot 10^5, ∑n≤2⋅105\sum n \le 2 \cdot 10^5, 1≤hi≤1091 \le h_i \le 10^9.
  • Source: Codeforces 2290C · Books on a Shelf