#P6721. Constructing a Permutation from Prefix Medians

    ID: 19929 Type: Default 1000ms 256MiB

Constructing a Permutation from Prefix Medians

Constructing a Permutation from Prefix Medians

Given a positive integer NN and a sequence B=[B1,B2,,BN]B=[B_1,B_2,\dots,B_N], where BB is defined as the prefix medians of a permutation AA of the numbers 1,2,,2N11,2,\dots,2N-1, your task is to construct one such permutation AA. In other words, let AA be a permutation of {1,2,,2N1}\{1,2,\dots,2N-1\} and define its prefix-median sequence BB of length NN 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 {1,2,,2N1}\{1,2,\dots,2N-1\} the median of the whole sequence is always NN. Thus a necessary condition for the input is BN=NB_N=N, and one may show that if a solution exists then the input BB has special properties. You are guaranteed that the given BB is valid (i.e. there exists at least one permutation AA having BB as its prefix medians).

One observation is that the elements at odd positions in AA must be exactly B1,B2,,BNB_1, B_2, \dots, B_N (in that order) because the ii-th prefix, which has length 2i12i-1, must have BiB_i as its median. The remaining N1N-1 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 2iN2\le i\le N the following condition holds. Let X1,X2,,XN1X_1,X_2,\dots,X_{N-1} be the numbers placed at positions 2,4,,2N22,4,\dots,2N-2. Then for every 2iN2\le i\le N, 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 i1i-1 numbers are strictly less than BiB_i. 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 SS for the even positions (the XjX_j’s) so that all these conditions are met, and then output the full permutation AA.

It is guaranteed that the input BB 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>