Two Merged Sequences
Contest: Codeforces Round 550 (Div. 3)
Difficulty rating: 2400
Tags: dp, greedy

Two integer sequences existed initially, one of them was strictly increasing, and another one — strictly decreasing.

Strictly increasing sequence is a sequence of integers $$$[x_1 < x_2 < \dots < x_k]$$$. And strictly decreasing sequence is a sequence of integers $$$[y_1 > y_2 > \dots > y_l]$$$. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.

Elements of increasing sequence were inserted between elements of the decreasing one (and, possibly, before its first element and after its last element) without changing the order. For example, sequences $$$[1, 3, 4]$$$ and $$$[10, 4, 2]$$$ can produce the following resulting sequences: $$$[10, \textbf{1}, \textbf{3}, 4, 2, \textbf{4}]$$$, $$$[\textbf{1}, \textbf{3}, \textbf{4}, 10, 4, 2]$$$. The following sequence cannot be the result of these insertions: $$$[\textbf{1}, 10, \textbf{4}, 4, \textbf{3}, 2]$$$ because the order of elements in the increasing sequence was changed.

Let the obtained sequence be $$$a$$$. This sequence $$$a$$$ is given in the input. Your task is to find any two suitable initial sequences. One of them should be strictly increasing, and another one — strictly decreasing. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.

If there is a contradiction in the input and it is impossible to split the given sequence $$$a$$$ into one increasing sequence and one decreasing sequence, print "NO".

Input format:
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$.

The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \le a_i \le 2 \cdot 10^5$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$.

Output format:
If there is a contradiction in the input and it is impossible to split the given sequence $$$a$$$ into one increasing sequence and one decreasing sequence, print "NO" in the first line.

Otherwise print "YES" in the first line. In the second line, print a sequence of $$$n$$$ integers $$$res_1, res_2, \dots, res_n$$$, where $$$res_i$$$ should be either $$$0$$$ or $$$1$$$ for each $$$i$$$ from $$$1$$$ to $$$n$$$. The $$$i$$$-th element of this sequence should be $$$0$$$ if the $$$i$$$-th element of $$$a$$$ belongs to the increasing sequence, and $$$1$$$ otherwise. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Example input:
9
5 1 3 6 8 2 9 0 10

Example output:
YES
1 0 0 0 0 1 0 1 0

Example input:
5
1 2 4 0 2

Example output:
NO

Editorial:
I know about greedy solutions and other approaches, but I'll describe my solution. This is dynamic programming. I'll consider all positions $$$0$$$-indexed.
Let $$$dp_{i, 0}$$$ be the maximum possible minimal element in the decreasing sequence, if the last element ($$$i-1$$$-th) was in the increasing sequence, and $$$dp_{i, 1}$$$ be the minimum possible maximum element in the increasing sequence, if the last element ($$$i-1$$$-th) was in the decreasing sequence.
Initially, all $$$dp_{i, 0}$$$ are $$$-\infty$$$ and all $$$dp_{i, 1}$$$ are $$$\infty$$$ (except two values: $$$dp_{0, 0} = \infty$$$ and $$$dp_{0, 1} = -\infty$$$).
What about transitions? Let's consider four cases:
• The previous element was in the increasing sequence and we want to add the current element to the increasing sequence. We can do $$$dp_{i, 0} := max(dp_{i, 0}, dp_{i - 1, 0})$$$ if $$$a_i > a_{i - 1}$$$;
• the previous element was in the increasing sequence and we want to add the current element to the decreasing sequence. We can do $$$dp_{i, 1} := min(dp_{i, 1}, a_{i - 1})$$$ if $$$a_i < dp_{i - 1, 0}$$$;
• the previous element was in the decreasing sequence and we want to add the current element to the decreasing sequence. We can do $$$dp_{i, 1} := min(dp_{i, 1}, dp_{i - 1, 1})$$$ if $$$a_i < a_{i - 1}$$$;
• the previous element was in the decreasing sequence and we want to add the current element to the increasing sequence. We can do $$$dp_{i, 0} := max(dp_{i, 0}, a_{i - 1})$$$ if $$$a_i > dp_{i - 1, 1}$$$.
The logic behind these transitions is kinda hard but understandable.
If $$$dp_{n - 1, 0} = -\infty$$$ and $$$dp_{n - 1, 1} = \infty$$$ then the answer is "NO". Otherwise we can restore any possible answer using parents in the dynamic programming.