Official

A - Make M Editorial by evima


When \(N = 1\), there are no conditions, so the objective is always achievable. Also, since the input sequence \(A\) can be freely rearranged, the original order does not matter. From now on, we assume that \(N \ge 3\) and that the input \(A\) is sorted in ascending order in advance.

It is not so easy to properly prove the validity of the solution, but you can get the correct answer just by trying an intuitive method. The computational complexity is \(\mathrm{O}(N \log N)\), with sorting being the bottleneck.

Solution 1. Rearrange greedily

First, we want to place the larger values at even positions and smaller values at odd positions. Therefore, for the sequence \(B = (B_1, B_2, \dots, B_N)\) rearranged to be M-shaped, we may assume the following:

\[\{B_1, B_3, \dots, B_N\} = \left\{A_1, A_2, \dots, A_\frac{N+1}{2}\right\}\]

\[\{B_2, B_4, \dots, B_{N-1}\} = \left\{A_\frac{N+3}{2}, A_\frac{N+5}{2}, \dots, A_N\right\}\]

If the sequence is M-type without this, you can confirm that it remains M-type even if you swap the even and odd positions appropriately so that the non-M-type part becomes M-type.

When rearranged to satisfy the above conditions, the values placed at even positions will never be smaller than those placed at odd positions. Therefore, the only thing to worry about is equal values being adjacent. To put equal values as far apart as possible, it is better to arrange them from smallest to largest, and it can be assumed that \(B_1 \le B_3 \le \cdots \le B_N\) and \(B_2 \le B_4 \le \cdots \le B_{N-1}\) hold. (See the next solution for proof.) In other words, the problem can be solved by checking if the sequence \(B\) obtained by rearranging \(A\) as follows is M-type:

\[B_{2i-1} = A_i \quad \left(i = 1, 2, \dots, \frac{N+1}{2}\right)\]

\[B_{2i} = A_{i+\frac{N+1}{2}} \quad \left(i = 1, 2, \dots, \frac{N-1}{2}\right)\]

A different representation is:

\[B = (B_1, B_2, \dots, B_N) = \left(A_1, A_\frac{N+3}{2}, A_2, A_\frac{N+5}{2}, \dots, A_N, A_\frac{N+1}{2}\right)\]

Sample Implementation (C, 56 ms)

(Above is a modification of a translation by GPT-4.)

(Below is a translation by GPT-4.)

Solution 2. Check the frequency of the reference value

Check if \(x = A_\frac{N+3}{2}\) (the smallest value you want to place at even positions) occupies more than half of the total. In conclusion, if \(x\) occupies more than half, it is impossible, and if not, it is possible (by arranging as in Solution 1).

First, confirm that it is impossible to rearrange the sequence to be M-type when \(x\) occupies more than half of the total. In this case, when trying to rearrange the sequence to be M-type, the only way to arrange it is to make sure that there are no adjacent positions with \(x\). Since \(x\) occupies more than half of the total, the only way to satisfy this is to make all odd positions \(x\). However, even in that case, there are at least \(\displaystyle\frac{N+3}{2}\) integers less than or equal to \(x\), so at least one even position will have a number less than or equal to \(x\), and it is impossible to be M-type.

Next, we will show that when the sequence is not M-type in the arrangement of Solution 1, \(x\) occupies more than half of the total. (By considering the contrapositive, if \(x\) does not occupy more than half of the total, the sequence can be made M-type by the arrangement of Solution 1.) If the sequence is not M-type in the arrangement of Solution 1, there exists an even number \(k\) such that \(B_k = B_{k+1}\) holds. (If \(B_k > B_{k+1}\), then \(B_{k-1} < B_{k+1}\), so \(B_{k-1} < B_k > B_{k+1}\) holds for \(k\).) Considering the original sequence \(A\), this means that \(A_{\frac{k}{2} + \frac{N+1}{2}} = A_{\frac{k}{2} + 1}\) holds,

\[A_{\frac{k}{2} + 1} = A_{\frac{k}{2} + 3} = \cdots = A_{\frac{k}{2} + \frac{N+1}{2}}\]

holds. Since \(B_{k+1} = A_{\frac{k}{2} + 1}\) is an odd position and \(B_k = A_{\frac{k}{2} + \frac{N+1}{2}}\) is an even position, this means

\[A_{\frac{k}{2} + 1} = A_\frac{N+1}{2} = A_\frac{N+3}{2} = A_{\frac{k}{2} + \frac{N+1}{2}}\]

and it has been shown that \(x = A_\frac{N+3}{2}\) has at least \(\displaystyle\frac{N+1}{2}\) (i.e., occupies more than half of the total).

Sample Implementation (C, 59 ms)

Note that Solution 2 can also be executed in \(\mathrm{O}(N)\) time. (cf. Median of Medians, Quickselect, Boyer–Moore Majority Vote Algorithm, etc.)

posted:
last update: