#P6721. Constructing a Permutation from Prefix Medians
Constructing a Permutation from Prefix Medians
Constructing a Permutation from Prefix Medians
Given a positive integer and a sequence , where is defined as the prefix medians of a permutation of the numbers , your task is to construct one such permutation . In other words, let be a permutation of and define its prefix-median sequence of length by letting [ B_i = \text{median}(A_1,A_2,\dots,A_{2i-1}), \quad 1\le i\le N. ] Here the median of an odd-length sequence is the element in the middle when the sequence is sorted in non‑decreasing order. It is known that in any permutation of the median of the whole sequence is always . Thus a necessary condition for the input is , and one may show that if a solution exists then the input has special properties. You are guaranteed that the given is valid (i.e. there exists at least one permutation having as its prefix medians).
One observation is that the elements at odd positions in must be exactly (in that order) because the -th prefix, which has length , must have as its median. The remaining positions (the even indices) must be filled with the numbers from the set [ S = {1,2,\dots,2N-1} \setminus {B_1,B_2,\dots,B_N}, ] in some order so that for every the following condition holds. Let be the numbers placed at positions . Then for every , in the prefix [ [A_1, A_2, \dots, A_{2i-1}] = [B_1, X_1, B_2, X_2, \dots, B_{i-1}, X_{i-1}, B_i], ] it must be that exactly numbers are strictly less than . That is, [ #{j:1\le j\le i-1,; B_j < B_i} + #{j:1\le j\le i-1,; X_j < B_i} = i-1. ] Your task is to choose a permutation of for the even positions (the ’s) so that all these conditions are met, and then output the full permutation .
It is guaranteed that the input is valid; if there are multiple answers, any one is acceptable.
inputFormat
The first line contains a single integer $N$ ($1\le N\le 10$).
The second line contains $N$ space‐separated integers, $B_1, B_2, \dots, B_N$.
Note: It is guaranteed that $B_N=N$, and that there exists at least one permutation $A$ of $\{1,2,\dots,2N-1\}$ whose prefix medians are exactly $B$.
outputFormat
Output $2N-1$ space-separated integers --- a permutation $A$ of $\{1,2,\dots,2N-1\}$ whose prefix medians are exactly $B$.
sample
2
3 2
3 1 2
</p>