IBMOA #4
Optimal Priority (Lexicographic Sort with Parity Swap)
Estimated MediumJudge-ready
Topics (AI-labelled)GreedyTwo Pointers
Each time, you may only swap two adjacent items with different parity. Find the lexicographically smallest sequence that can be reached. Each item is a single digit.
Input
The first line contains n, and the second line contains the digits. On this site 1≤n≤200000, 0≤digit≤9.
Output
Output the full minimum sequence.
Example 1
Input:
6
2 4 6 4 3 2
Output:
2 3 4 6 4 2
Example 2
Input:
3
9 7 2
Output:
2 9 7
Example 3
Input:
1
0
Output:
0
Example notes
The examples give 2 3 4 6 4 2; 2 9 7; and 0 respectively.
Editorial
Loading solution…