#P15033. [UOI 2021 II Stage] 字典序
[UOI 2021 II Stage] 字典序
Problem Description
Recently, someone gave the Cossack an array containing integers.
The bearded man immediately wants to sort the elements of the array so that after sorting, any two adjacent numbers in the array are different. Among all such sortings, he wants to find the lexicographically smallest one.
Recall that lexicographical order is defined as follows. Suppose there are two arrays. Find the first position where the elements of the two arrays differ. If at that position the element of the first array is smaller than that of the second array, then the first array is lexicographically smaller than the second; otherwise, the first array is lexicographically larger. For example, the following inequalities hold: , , .
Input Format
The first line contains an integer , the number of elements in the array.
The second line contains integers , the elements of the array.
Output Format
If it is impossible to sort the Cossack’s array, output a single number .
Otherwise, output integers, the lexicographically smallest valid sorting of the Cossack’s array.
5
3 1 2 3 3
3 1 3 2 3
6
2 3 1 1 2 4
1 2 1 2 3 4
4
1 2 1 1
-1
Hint
Sample Explanation
In the first sample, there is a unique sorting.
In the second sample, there are other sortings as well, for example: or . But all these sortings are lexicographically greater than .
In the third sample, there is no valid sorting (the array will always have two ’s in adjacent positions).
Scoring Rules
This problem uses per-testpoint scoring. Some additional constraints are:
- (5 points):
- (10 points):
- (25 points):
- (5 points):
- (10 points):
- (45 points):
Translated by DeepSeek V3.
Translated by ChatGPT 5