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-----

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-----

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.


-----Examples-----
Input
9
5 1 3 6 8 2 9 0 10

Output
YES
1 0 0 0 0 1 0 1 0 

Input
5
1 2 4 0 2

Output
NO

Source: codeforces | Difficulty: VERY_HARD
Tags: Dynamic programming

Greedy algorithms


Solutions:
MAX = 1 + 2 * 10 ** 5
MIN = -1
ASC = 0
DSC = 1
n = int(input())
a = list(map(int, input().split()))
asc = MIN
dsc = MAX
res = []
for i in range(n):
	if a[i] > asc and a[i] < dsc:
		if i + 1 == n:
			res.append(DSC)
		elif a[i] > a[i + 1]:
			res.append(DSC)
			dsc = a[i]
		else:
			res.append(ASC)
			asc = a[i]
	elif a[i] > asc:
		res.append(ASC)
		asc = a[i]
	elif a[i] < dsc:
		res.append(DSC)
		dsc = a[i]
	else:
		print('NO')
		return
print('YES')
print(*res)


n = int(input())
a = [int(ai) for ai in input().split()] + [0]
(inc, dec) = (-1, 10 ** 6)
marker = [0] * n
for i in range(n):
	if inc < a[i] and a[i] < dec:
		if a[i] < a[i + 1]:
			inc = a[i]
		else:
			dec = a[i]
			marker[i] = 1
	elif inc < a[i]:
		inc = a[i]
	elif dec > a[i]:
		dec = a[i]
		marker[i] = 1
	else:
		print('NO')
		break
else:
	print('YES')
	print(*marker)


n = int(input())
a = list(map(int, input().split()))
inf = 10 ** 6
inc = [inf for i in range(0, n + 1)]
dec = [-inf for i in range(0, n + 1)]
trinc = [-1 for i in range(0, n + 1)]
trdec = [-1 for i in range(0, n + 1)]
inc[0] = -inf
dec[0] = inf
for i in range(0, n - 1):
	if a[i + 1] < a[i]:
		if inc[i + 1] > inc[i]:
			inc[i + 1] = inc[i]
			trinc[i + 1] = 1
	if a[i + 1] > a[i]:
		if dec[i + 1] < dec[i]:
			dec[i + 1] = dec[i]
			trdec[i + 1] = 1
	if a[i + 1] > inc[i]:
		if dec[i + 1] < a[i]:
			dec[i + 1] = a[i]
			trdec[i + 1] = 0
	if a[i + 1] < dec[i]:
		if inc[i + 1] > a[i]:
			inc[i + 1] = a[i]
			trinc[i + 1] = 0
if inc[n - 1] == inf and dec[n - 1] == -inf:
	print('NO')
	return
now_inc = False
ans = [0 for i in range(0, n)]
if inc[n - 1] != inf:
	now_inc = True
for i in range(n - 1, -1, -1):
	if now_inc:
		ans[i] = 1
		if trinc[i] == 0:
			now_inc = False
	else:
		ans[i] = 0
		if trdec[i] == 0:
			now_inc = True
print('YES')
print(' '.join((str(x) for x in ans)))


def main():
	n = int(input())
	a = list(map(int, input().split()))
	result = [0] * n
	last_increasing = -1
	last_decreasing = 200001
	for (i, x) in enumerate(a):
		if i == n - 1:
			if x < last_decreasing:
				result[i] = 1
			elif x <= last_increasing:
				print('NO')
				return
		else:
			if last_increasing >= x >= last_decreasing:
				print('NO')
				return
			if x > last_increasing and x >= last_decreasing:
				last_increasing = x
			if x <= last_increasing and x < last_decreasing:
				last_decreasing = x
				result[i] = 1
			if last_increasing < x < last_decreasing:
				if a[i + 1] > x:
					last_increasing = x
				else:
					last_decreasing = x
					result[i] = 1
	print('YES')
	print(' '.join(map(str, result)))
main()


(n, a) = (int(input()), list(map(int, input().split())) + [0])
(ans, inc, dec) = ([0] * n, -1, 10000000.0)
for i in range(n):
	if inc < a[i] < dec:
		if a[i] < a[i + 1]:
			inc = a[i]
		else:
			dec = a[i]
			ans[i] = 1
	elif inc < a[i]:
		inc = a[i]
	elif dec > a[i]:
		dec = a[i]
		ans[i] = 1
	else:
		print('NO')
		break
else:
	print('YES')
	print(*ans)


n = int(input())
a = list(map(int, input().split())) + [0]
answer = [0] * n
current_increase_max = -1
current_decrease_min = 1000000.0
for i in range(n):
	if current_increase_max < a[i] and a[i] < current_decrease_min:
		if a[i] < a[i + 1]:
			current_increase_max = a[i]
			answer[i] = 0
		else:
			current_decrease_min = a[i]
			answer[i] = 1
	elif current_increase_max < a[i]:
		current_increase_max = a[i]
		answer[i] = 0
	elif current_decrease_min > a[i]:
		current_decrease_min = a[i]
		answer[i] = 1
	else:
		print('NO')
		return
print('YES')
print(*answer)


from collections import defaultdict as dd
import math

def nn():
	return int(input())

def li():
	return list(input())

def mi():
	return map(int, input().split())

def lm():
	return list(map(int, input().split()))

def getanswers():
	n = nn()
	seq = lm()
	lower = None
	upper = None
	answers = []
	for i in range(n - 1):
		if seq[i + 1] > seq[i]:
			if lower == None or seq[i] > lower:
				lower = seq[i]
				answers.append(0)
			elif upper == None or seq[i] < upper:
				upper = seq[i]
				answers.append(1)
			else:
				print('No')
				return
		if seq[i + 1] <= seq[i]:
			if upper == None or seq[i] < upper:
				upper = seq[i]
				answers.append(1)
			elif upper == None or seq[i] > lower:
				lower = seq[i]
				answers.append(0)
			else:
				print('No')
				return
	i = n - 1
	if upper == None or seq[i] < upper:
		upper = seq[i]
		answers.append(1)
	elif lower == None or seq[i] > lower:
		lower = seq[i]
		answers.append(0)
	else:
		print('No')
		return
	print('Yes')
	print(*answers)
	return
getanswers()


import sys
import os

def main(args):
	states = []
	queue = [(min(args) - 1, max(args) + 1, None, None)]
	answer_flag = True
	for i in range(len(args)):
		x = args[i]
		h_queue = []
		l_queue = []
		for j in range(len(queue)):
			if queue[j][1] is None or x < queue[j][1]:
				l_queue.append((queue[j][0], x, 1, j))
			else:
				break
		for j in range(len(queue) - 1, -1, -1):
			if queue[j][0] is None or x > queue[j][0]:
				h_queue.append((x, queue[j][1], 0, j))
			else:
				break
		queue = []
		if len(h_queue) > 0:
			queue.append(h_queue[-1])
		if len(l_queue) > 0:
			queue.append(l_queue[-1])
		if len(queue) > 1:
			if queue[0][0] < queue[1][0]:
				t = queue[0]
				queue[0] = queue[1]
				queue[1] = t
		if len(queue) == 0:
			answer_flag = False
			break
		states.append(queue)
	if answer_flag:
		print('YES')
	else:
		print('NO')
	if answer_flag:
		p = 0
		result = [None for i in range(len(states))]
		for i in range(len(states) - 1, -1, -1):
			result[i] = states[i][p][2]
			p = states[i][p][3]
		print(' '.join(map(str, result)))
input()
args = list(map(int, input().strip().split()))
main(args)


import math

def main():
	n = int(input())
	a = list(map(int, input().split()))
	if n == 1:
		print('YES')
		print(0)
	else:
		res = [0] * n
		ok = True
		last_inc = -math.inf
		last_dec = math.inf
		for i in range(n):
			if a[i] > last_inc and a[i] >= last_dec:
				res[i] = 0
				last_inc = a[i]
			elif a[i] < last_dec and a[i] <= last_inc:
				res[i] = 1
				last_dec = a[i]
			elif last_dec <= a[i] <= last_inc:
				ok = False
				break
			elif i < n - 1:
				next_val = a[i + 1]
				if next_val >= a[i]:
					res[i] = 0
					last_inc = a[i]
				else:
					res[i] = 1
					last_dec = a[i]
			else:
				res[i] = 0
				last_inc = a[i]
		if not ok:
			print('NO')
		else:
			print('YES')
			print(' '.join(map(str, res)))
main()


(n, a) = (int(input()), [int(i) for i in input().split()] + [0])
(ans, inc, dec) = ([0 for _ in range(n)], -1, float('inf'))
for i in range(n):
	if inc < a[i] < dec:
		if a[i] < a[i + 1]:
			inc = a[i]
		else:
			dec = a[i]
			ans[i] = 1
	elif inc < a[i]:
		inc = a[i]
	elif dec > a[i]:
		dec = a[i]
		ans[i] = 1
	else:
		print('NO')
		break
else:
	print('YES')
	print(*ans)