#107. 数组重排:4后跟5
数组重排:4后跟5
Rearrange Array: 4 Followed by 5
Background
In a data organization task, you are required to rearrange a special sequence of numbers. The numbers in this sequence need to conform to a specific pattern to ensure data correctness and usability.
Problem Description
(This is a slightly harder version of the fix34 problem.) Return an array that contains exactly the same numbers as the given array, but rearranged so that every is immediately followed by a . Do not move the 's, but every other number may move. The array contains the same number of 's and 's, and every has a number after it that is not a . In this version, 's may appear anywhere in the original array.
Input Format
Input is given from standard input in the following format.
An integer array, representing the original sequence.
Output Format
Output is printed to standard output in the following format.
An integer array, representing the rearranged sequence.
Sample
[5 4 9 4 9 5]
[9, 4, 5, 4, 5, 9]
[1 4 1 5]
[1, 4, 5, 1]
[1 4 1 5 5 4 1]
[1, 4, 5, 1, 1, 4, 5]
Sample Explanation
For Sample 1: The original array is [5, 4, 9, 4, 9, 5]. Here, s are at indices and . We need to place s after the s. In the original array, the s at indices and are "free" (i.e., they are not preceded by a ). The s at indices and need to be moved to make space for s. Ultimately, the at index is moved to index , and the at index is moved to index . The original s at indices and are moved to indices and respectively, resulting in [9, 4, 5, 4, 5, 9].
Constraints
Time limit: s, Memory limit: KiB for each test case.