#P9139. Partition into Twin Subsequences
Partition into Twin Subsequences
Partition into Twin Subsequences
Given a sequence of length \(4n\) where each integer from \(1\) to \(n\) appears exactly \(4\) times, determine whether it is possible to partition the sequence into two subsequences so that the two subsequences are identical. In other words, decide if the given sequence is a shuffle of twins.
More formally, for a sequence \(a_1,a_2,\ldots,a_{4n}\), determine whether there exists a partition of indices into two sets \(I\) and \(J\), each of size \(2n\), such that if \(I = \{i_1 < i_2 < \cdots < i_{2n}\}\) and \(J = \{j_1 < j_2 < \cdots .
This can be reformulated in terms of non‐crossing matchings. Namely, one wants to pair up the \(4n\) indices into \(2n\) pairs \((i,j)\) with \(i<j\) and \(a_i=a_j\) such that, when the pairs are sorted by their first element, the sequence of second elements is also increasing. If such a non‐crossing pairing exists then one may assign the first elements of the pairs to one subsequence and the second elements to the other, obtaining two identical sequences.
If a valid partition exists, output Yes
; otherwise, output No
.
inputFormat
The first line contains a single integer \(n\) (\(1 \le n \le 50\)). The second line contains \(4n\) space‐separated integers representing the sequence. It is guaranteed that for each integer \(x\) from \(1\) to \(n\), \(x\) appears exactly 4 times.
outputFormat
Output a single line with either Yes
if the sequence can be partitioned into two identical subsequences, or No
otherwise.
sample
1
1 1 1 1
Yes