[CF2290C] 书架摆书 (Books on a Shelf)
Problem Statement
There are books, where the -th book has height . You may arrange all 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 () — the number of test cases.
Each test case consists of () on the first line, and integers () on the second line.
It is guaranteed that the sum of over all test cases does not exceed .
Output
For each test case, print two lines:
- First line: the maximum number of visible books .
- Second line: integers representing any valid permutation of that achieves 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 is accepted.
- Constraints: , , , .
- Source: Codeforces 2290C · Books on a Shelf