Two Shuffled Sequences (Codeforces 1144C)
Tags: Others, sortings

Two integer sequences existed initially — one of them was strictly increasing, and the other 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.They were merged into one sequence $$$a$$$. After that sequence $$$a$$$ got shuffled. For example, some of the possible resulting sequences $$$a$$$ for an increasing sequence $$$[1, 3, 4]$$$ and a decreasing sequence $$$[10, 4, 2]$$$ are sequences $$$[1, 2, 3, 4, 4, 10]$$$ or $$$[4, 2, 1, 10, 4, 3]$$$.This shuffled 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 the other 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$$$ to increasing and decreasing sequences, 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$$$ to increasing and decreasing sequences, print "NO" in the first line.Otherwise print "YES" in the first line and any two suitable sequences. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.In the second line print $$$n_i$$$ — the number of elements in the strictly increasing sequence. $$$n_i$$$ can be zero, in this case the increasing sequence is empty.In the third line print $$$n_i$$$ integers $$$inc_1, inc_2, \dots, inc_{n_i}$$$ in the increasing order of its values ($$$inc_1 < inc_2 < \dots < inc_{n_i}$$$) — the strictly increasing sequence itself. You can keep this line empty if $$$n_i = 0$$$ (or just print the empty line).In the fourth line print $$$n_d$$$ — the number of elements in the strictly decreasing sequence. $$$n_d$$$ can be zero, in this case the decreasing sequence is empty.In the fifth line print $$$n_d$$$ integers $$$dec_1, dec_2, \dots, dec_{n_d}$$$ in the decreasing order of its values ($$$dec_1 > dec_2 > \dots > dec_{n_d}$$$) — the strictly decreasing sequence itself. You can keep this line empty if $$$n_d = 0$$$ (or just print the empty line).$$$n_i + n_d$$$ should be equal to $$$n$$$ and the union of printed sequences should be a permutation of the given sequence (in case of "YES" answer).


Sample Inputs:
7
7 2 7 3 3 1 4

5
4 3 1 5 3

5
1 1 2 1 2

5
0 1 2 3 4

Sample Outputs:
YES
2
3 7 
5
7 4 3 2 1

YES
1
3 
4
5 4 3 1

NO

YES
0

5
4 3 2 1 0

Solution (Python):
import sys
input = sys.stdin.readline
from collections import Counter
n = int(input())
w = Counter(map(int, input().split()))
if max(w.values()) > 2:
    print('NO')
else:
    d = [i for i in w if w[i] == 2]
    print('YES')
    print(len(d))
    print(' '.join(map(str, sorted(d))))
    print(len(set(w)))
    print(' '.join(map(str, sorted(w, reverse=True))))

Solution (Python):
def ii():
    return int(input())

def fi():
    return float(input())

def si():
    return input()

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

def li():
    return list(mi())
n = ii()
a = li()
s1 = set()
s2 = set()
for i in a:
    if i not in s1:
        s1.add(i)
    elif i not in s2:
        s2.add(i)
    else:
        print('NO')
        exit()
print('YES')
print(len(s2))
print(' '.join([str(x) for x in sorted(s2)]))
print(len(s1))
print(' '.join([str(x) for x in sorted(s1, reverse=True)]))

Solution (Python):
import sys
import time
start = time.time()
n = int(input())
arr = list(map(int, input().split()))
inc = set()
dec = set()
flag = True
for a in arr:
    if a not in inc:
        inc.add(a)
    elif a not in dec:
        dec.add(a)
    else:
        print('No')
        flag = False
        break
if flag:
    print('Yes')
    print(len(inc))
    print(' '.join(list(map(str, sorted(list(inc))))))
    print(len(dec))
    print(' '.join(list(map(str, sorted(list(dec), reverse=True)))))
end = time.time()