Imagine there are \$n\$ people \$\{a_1, a_2, \ldots, a_n\}\$ who enter a room in order and sit down in \$n\$ seats, arranged in a row. Each of these people belong to some gang, indicated by an integer \$\{1, 2, \ldots, k\}\$. These gangs hate the members of the other gangs, so they want to sit as far away from those people as possible; specifically, they sit in the set which maximizes the minimum distance from anyone in a different gang who is already seated. They break ties by sitting in the seat furthest to the left.1
For example, suppose \$n=6\$, and there are \$k=3\$ gangs \$\{1, 2, 3\}\$.
Now imagine that the first person that enters is in gang 1, the next two are in gang 2, the next two are in gang 3, and the final person is in gang 1 again -- i.e. the order is [1, 2, 2, 3, 3, 1]
. Then the people will sit down in the following order:
_ _ _ _ _ _
1: 1 _ _ _ _ _
2: 1 _ _ _ _ 2
2: 1 _ _ _ 2 2
3: 1 _ 3 _ 2 2
3: 1 3 3 _ 2 2
1: 1 3 3 1 2 2
Your challenge is, given a sequence of numbers \$\{a_1, a_2, \ldots, a_n\}\$, where each \$a_i \in \{1, 2, \ldots, k\}\$ is some positive integer representing a person in some gang, output the final arrangement of the \$n\$ people in \$n\$ seats as described above. (If useful, you can take \$n\$ and \$k\$ as extra inputs.) You can assume that no gang number will be skipped - e.g. \$\{1, 2, 4\}\$ would not be a valid input.
Test Cases
[1] -> [1]
[1, 2] -> [1, 2]
[1, 1, 1] -> [1, 1, 1]
[1, 2, 1, 2, 1] -> [1, 1, 1, 2, 2]
[1, 2, 2, 3, 3, 1] -> [1, 3, 3, 1, 2, 2]
[1, 2, 4, 3, 2, 1, 4] -> [1, 3, 1, 4, 4, 2, 2]
[4, 4, 4, 3, 3, 2, 1] -> [4, 4, 4, 2, 1, 3, 3]
[1, 2, 3, 4, 5, 6, 7] -> [1, 4, 5, 3, 6, 7, 2]
Standard loopholes are forbidden. As this is code-golf, shortest program wins.
1 This is a variation of the "urinal protocol".