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 < ... < x_k]. And strictly decreasing sequence is a sequence of integers [y_1 > y_2 > ... > 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 ≤ n ≤ 2 ⋅ 10^5) — the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (0 ≤ a_i ≤ 2 ⋅ 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, ..., inc_{n_i} in the increasing order of its values (inc_1 < inc_2 < ... < 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, ..., dec_{n_d} in the decreasing order of its values (dec_1 > dec_2 > ... > 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).
Examples
Input
7
7 2 7 3 3 1 4
Output
YES
2
3 7
5
7 4 3 2 1
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to solve problem: Given a sequence a of n integers (up to 2e5). We need to partition the multiset into two sequences: one strictly increasing, one strictly decreasing (both can be empty or single-element). Return any such partition.
Constraints: n up to 2e5. Values up to 2e5.
We need to decide which elements go to inc and which to dec such that inc sorted is strictly increasing (no duplicates allowed) and dec sorted is strictly decreasing (no duplicates allowed). Note that inc and dec are sequences, but after we assign elements, we need to be able to order them appropriately: inc must be strictly increasing, which means we must be able to order the chosen elements in increasing order with strict inequality. Since we can reorder arbitrarily (they are sequences initially but got merged and shuffled), we just need the set (multiset) to be able to form a strictly increasing sequence. That is possible iff there are no duplicate values among the inc group because if there are duplicates, they cannot be placed in strictly increasing order (since equal numbers are not allowed). Similarly for dec, no duplicates among dec. However, note that the same value could appear in both inc and dec? Possibly yes, because inc and dec are separate sequences; the same value can appear in both as long as there are at least two occurrences of that value in the original multiset. For example, in sample 1: they had 7,2,7,3,3,1,4. Output inc: 3,7 (two elements) and dec: 7,4,3,2,1 (five elements). Notice that 7 appears twice, once in inc, once in dec. Also 3 appears twice, once in inc, once in dec. So duplicates across groups are allowed. But within a group, duplicates are not allowed because strictly increasing/decreasing forbids equal adjacent elements; but if there are duplicates in group, you cannot order them strictly. So condition: For each group (inc and dec), the multiset of values assigned must have all distinct values (no repeated values). Also the order can be arranged to be strictly increasing/decreasing if the values are distinct; if they are distinct, we can sort them to get strictly increasing (since strictly increasing just requires each next larger, so if distinct, we can sort ascending for inc). For dec, we need strictly decreasing, which requires distinct values as well, and if distinct we can sort descending.
But wait: Is it enough to be distinct? Consider inc: we need strictly increasing order: inc_1 < inc_2 < ... . If values are distinct, we can always sort them ascending and they will be strictly increasing because all are different. So yes, distinctness is sufficient.
Similarly for dec: distinct values sorted descending yields strictly decreasing.
Thus the problem reduces to: Given multiset of n numbers (with multiplicities), we need to partition the occurrences (i.e., assign each occurrence to either inc or dec) such that in each part, all assigned values are distinct (i.e., no value appears more than once in the same part). And we can output any such partition. It doesn't require anything else about ordering relative to original positions; we just output the sequences sorted accordingly.
So we need to decide for each occurrence of a value v whether to put it in inc or dec, such that each value appears at most once in inc and at most once in dec. If a value appears more than 2 times, impossible because you cannot assign more than 2 copies (one to inc, one to dec). If a value appears exactly 2 times, you must assign one to inc and one to dec. If a value appears exactly 1 time, you can assign it to either inc or dec (or maybe both? but only one copy). If a value appears 0 times, irrelevant.
Thus a necessary condition: For every value v, its frequency f(v) ≤ 2. If any f(v) > 2, output NO.
But is that also sufficient? Not necessarily: we also need to ensure that we can order inc strictly increasing and dec strictly decreasing. Since inc just sorted ascending distinct values, it's fine regardless of which values we pick. However, we have to assign each occurrence to either inc or dec; but we need to output inc and dec sequences. If we assign arbitrarily, inc might have distinct values, but is there any other constraint? The inc sequence is strictly increasing; dec is strictly decreasing. Could there be a conflict when we assign values such that inc and dec both contain some values that cross? For instance, suppose we assign some numbers to inc and some to dec. The inc must be sorted ascending, dec sorted descending. There is no requirement about relationship between inc and dec, they are independent. So as long as each group has distinct values, it's valid. So the only condition is f(v) ≤ 2 for all v.
But wait, check sample 3: 1 1 2 1 2. Frequencies: 1 appears 3 times, 2 appears 2 times. Since 1 appears 3>2, so NO. That matches.
What about sample 2: 4 3 1 5 3. Frequencies: 3 appears twice, others once. So condition holds. Output: inc: 3 (only one element), dec: 5 4 3 1. So 3 appears in dec and inc. Good.
Sample 4: 0 1 2 3 4 all once. Could assign all to inc, then inc sorted ascending; dec empty. Or all to dec? But dec would need to be strictly decreasing, so we could assign all to dec, output 4 3 2 1 0. Both okay. Output shows inc empty, dec all.
Thus seems condition is simply: for each value, count <=2. Then we need to construct a partition: for values with count=2, assign one to inc, one to dec. For values with count=1, can assign to inc or dec arbitrarily.
But is that always valid? Let's think about possible pitfalls: If we assign some values to inc and some to dec, we must output inc in increasing order and dec in decreasing order. No further constraints. So indeed any partition that respects distinctness per group works. So we can just assign each value's occurrences appropriately.
However, is there any hidden constraint from the original problem statement? "They were merged into one sequence a. After that sequence a got shuffled." That suggests that originally there were two sequences: one strictly increasing, one strictly decreasing. They were merged (concatenated?) and then shuffled. So the original sequences themselves were strictly increasing and decreasing respectively. That implies that the inc sequence (original) must be strictly increasing. In our partition, we are constructing inc and dec sequences that are strictly increasing and decreasing respectively. But there is a nuance: The original sequences were not necessarily consisting of distinct values? For a strictly increasing sequence, values must be strictly increasing, so indeed all values in that sequence are distinct. Similarly for decreasing. So our condition that each group has distinct values is necessary. But is it also sufficient that we can arrange the chosen values into a strictly increasing sequence? Yes, if distinct, we can sort them to make increasing. But there is also the possibility of duplicate values across groups: they can be equal across groups because they were separate sequences; that's fine. So condition f(v) ≤ 2 seems necessary and appears sufficient.
But consider a scenario: f(v) = 2 for many values. We assign one copy to inc, one to dec. Then inc will contain exactly one copy of each such value, dec also one copy. But are there any constraints on ordering that might cause conflict? For inc we need to output in increasing order. That's just sorting. Similarly dec decreasing. So fine.
But wait: Could there be a scenario where a value appears twice, and we assign both to inc? That would violate distinctness. So not allowed. So for each value with frequency 2, exactly one goes to inc, one to dec. For frequency 1, it's free.
But is it possible that for a value with frequency 1, we assign it to both inc and dec? No, only one copy.
So we just need to decide for each single-occurrence value whether to put in inc or dec. That seems trivial: we can arbitrarily assign them to achieve any distribution. So the answer always YES if all frequencies ≤ 2. But let's double-check with some edge cases. n=1: one element. Frequency 1. Condition holds. We can put it in inc (or dec). Output inc: that element, dec empty; or inc empty, dec: that element. Both are valid (single element sequence considered both increasing and decreasing). So YES.
n=2: if both same value: then frequency 2, assign one inc one dec. inc = [v], dec = [v]. inc sorted: [v] valid; dec sorted: [v] valid. Output inc: v, dec: v. That works. If they are different: each frequency 1. Can assign both to inc: inc = [a,b] sorted distinct? If a != b, distinct, so inc works; dec empty. But what if a=b? Already considered. So YES.
Thus seems always YES if f(v) ≤ 2. But wait, there might be a subtlety: The problem statement says "Two integer sequences existed initially — one of them was strictly increasing, and the other one — strictly decreasing." That means the original sequences were increasing and decreasing respectively. After merging and shuffling we get a. Now we are to find any two suitable initial sequences. It does not require that the increasing sequence is sorted in increasing order as it appeared originally? It only says "find any two suitable initial sequences". The output requires we print inc in increasing order and dec in decreasing order. So yes.
But could there be additional constraints? For the inc sequence, we need to be able to order its elements in strictly increasing order. That's possible if no duplicates. For dec, same. So indeed the only constraint is that each value appears at most once in inc and at most once in dec. So if any value appears more than 2 times, impossible because we only have two groups. So answer is NO when any frequency >2; otherwise YES.
But let's think: Is there any scenario where frequency ≤2 but still impossible? Suppose we have values [1,2] with each frequency 1. That's fine. But what about [1,1,2]? 1 appears twice, 2 once. That is f(1)=2, f(2)=1. Assign one 1 to inc, one 1 to dec. Now we have 2 can go to inc or dec. If we put 2 to inc, inc would have {1,2} which are distinct and increasing fine; dec would have {1} decreasing fine. If we put 2 to dec, dec would have {2,1} but then dec values: 2 and 1 are distinct and decreasing fine; inc would have {1} fine. So works.
What about values [1,2,2]? f(2)=2, f(1)=1. Works. So seems fine.
But maybe there is additional hidden condition: The original sequences were strictly increasing and strictly decreasing. That means that in the inc sequence, not only are values distinct, but they must be in increasing order. However, when we partition we can reorder arbitrarily, so that's fine. Could there be a scenario where the inc sequence cannot be strictly increasing because of the pattern of original inc? No, because we are allowed to output any order (the problem doesn't require to preserve any order). The original merging and shuffling destroyed order, so we are free.
Thus the problem seems too trivial: just check frequency <=2. But why would the problem be given as part of a contest with n up to 2e5 if it's that simple? It might be that there is more to it: Perhaps the increasing sequence must be strictly increasing, and the decreasing strictly decreasing, but also the sequences themselves, when combined, must be exactly the given multiset. That's our partition. So indeed it's just a partition with distinctness per group.
But I recall a similar Codeforces problem: "Increasing and Decreasing" or something. I think there is a known problem: "Two sequences" maybe from Codeforces Round? Actually there is a problem: "A. Shuffle" maybe? Let's search memory: There is a problem "Increasing and Decreasing" from Codeforces? I'm not sure. But maybe there is an additional twist: The sequences were originally strictly increasing and strictly decreasing, then merged and shuffled. That means that the original sequences were themselves sorted in that order. But after merging and shuffling we lost order. However, the original sequences could have had overlapping values? They can, as long as they are separate sequences. But maybe there's an additional constraint: In the original inc sequence, if two elements have the same value, that's impossible because strictly increasing forbids equality. So that's accounted.
But maybe there is another condition: The union of the two sequences is the given multiset, but also the sequences must be such that if you sort inc ascending and dec descending, there is no conflict like inc containing a value greater than some in dec that would violate something? No, they are independent.
Thus the condition seems too simple. However, maybe I'm missing something: The problem might be that the increasing sequence must be strictly increasing, and the decreasing sequence strictly decreasing, but the sequences themselves, as initially existed, were not allowed to be empty? The problem says "Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing." So empty allowed.
But is there any constraint about the relative order? Could there be a scenario where we assign a value to inc and also to dec, but then inc sorted ascending and dec sorted descending might both include that value? That's fine.
Wait, maybe there is a more subtle point: When we assign elements to inc and dec, we need to output them as sequences, but we must output inc in increasing order and dec in decreasing order. However, is there any guarantee that we can assign each occurrence to either group without causing duplicates within group? That's the only check.
Thus, if every value appears at most twice, we can always assign: For each value that appears twice, assign one to inc and one to dec. For values that appear once, we can assign arbitrarily, but we must also ensure that the resulting inc and dec sequences are strictly monotonic. However, what if we assign a value that appears once to inc, but inc already has that value? Actually inc cannot have duplicates, but if that value appears once and we assign it to inc, inc will have that value only once. That's fine. The only risk is if we assign the singleton to inc and the same value also appears twice? But it's singleton, so not duplicate.
But wait: Could there be a conflict when we have two copies of a value and we assign both to inc? That would be duplicate, not allowed. So we must assign exactly one to inc and one to dec. So for frequencies 2, forced.
Now consider values with frequency 1. They can be placed in either group. But is it always possible to place all such values arbitrarily without creating duplicates? Since each group can contain at most one copy of each distinct value. For a value with frequency 1, there is no other copy, so if we put it in inc, inc will have that value once. That's fine. If we put it in dec, similarly fine. So no conflict.
Thus the assignment always possible if all f(v) ≤ 2.
Thus answer YES iff max frequency ≤ 2.
But wait, let's double-check with a possible counterexample: Suppose we have values: [1, 2, 2, 3, 3]. f(2)=2, f(3)=2, f(1)=1. If we assign forced: one 2 to inc, one 2 to dec; one 3 to inc, one 3 to dec. Then we have 1 to assign. If we assign 1 to inc, inc gets {1,2,3} (distinct, sorted increasing) fine. dec gets {2,3} (distinct, sorted decreasing: 3,2) fine. If assign 1 to dec, dec gets {1,2,3} but dec sorted decreasing would be 3,2,1 distinct fine; inc gets {2,3} fine. So works.
What about [1,1,2,2]? f1=2 f2=2. Forced: inc gets {1,2} (distinct) dec gets {1,2} (distinct). Works.
Thus seems always YES.
But perhaps the problem is more complex because the sequences must be strictly increasing and decreasing, but we also must preserve the relative order of equal elements? No, because they are indistinguishable occurrences.
Thus this appears trivial. Could it be that the input constraint "0 ≤ a_i ≤ 2⋅10^5" and n up to 2e5 is just for implementation efficiency. So solution: count frequencies, if any >2 print NO; else YES and output any partition.
But wait, let's re-read the problem statement carefully. It says: "Two integer sequences existed initially — one of them was strictly increasing, and the other one — strictly decreasing." That means there were two sequences: one increasing, one decreasing. They were merged into one sequence a. After that sequence a got shuffled. So a is a permutation (shuffled) of the concatenation of the two sequences. But note: "merged" could mean they were concatenated (maybe interleaved? But then shuffled, so order lost). So yes.
Now "Your task is to find any two suitable initial sequences. One of them should be strictly increasing and the other one — strictly decreasing." So we need to output the two sequences (the values) in sorted order (inc ascending, dec descending). The union must be exactly the multiset a.
So indeed, as argued.
But maybe there is a nuance: The sequences initially were strictly increasing and strictly decreasing. That means that each individual sequence is sorted in that order, but after merging and shuffling we get a. Could it be that the original sequences had some property that might restrict the assignment beyond just distinctness? For instance, consider that the increasing sequence must be strictly increasing, but also must be a sequence; that means that its elements, when sorted ascending, should be in the same order as they originally were? But we lost the order; we just need to output a sequence that is strictly increasing. There is no requirement to reconstruct the original order, only the multiset.
Thus it's fine.
But maybe the problem expects to also output the sequences in a specific order? The output format: after printing YES, we print n_i, then n_i integers in increasing order. That's inc. Then n_d, then n_d integers in decreasing order. So we just need to produce any partition.
So indeed the solution is simple.
But wait, I've seen a problem similar to this on Codeforces: "Two sequences" maybe problem 1462C? Not sure. Actually, there is a problem "A. Shuffle" but not. Let's think: There is Codeforces Round #131 (Div. 1) problem "Increasing and Decreasing"? Not. However, I recall a problem: "Increasing and Decreasing" from Codeforces Round # (maybe educational). I think it's problem 1154C? Not. Hmm.
Maybe I've misinterpreted: The phrase "strictly increasing sequence is a sequence of integers [x_1 < x_2 < ... < x_k]" and similarly for decreasing. But note that when we output, we must output inc_1 < inc_2 < ... and dec_1 > dec_2 > ... . That is what we do.
Now, check the sample 2: Input: 5, 4 3 1 5 3. Output: inc: 3; dec: 5 4 3 1. They gave inc = 3 (so they assigned one of the 3's to inc, the other 3 to dec). dec: 5,4,3,1 is decreasing (5>4>3>1). Works. So indeed they chose to put the singleton values (4,1,5) all to dec? Actually 4 appears once, 5 once, 1 once. They put them all in dec, plus one 3. inc got the other 3. So assignment for singletons was all to dec. That's okay.
Sample 1: inc: 3,7 ; dec: 7,4,3,2,1. So for 7 (twice) one inc one dec; for 3 (twice) one inc one dec; 4,2,1 appear once and were put in dec; inc also got maybe? inc got 3 and 7, so inc got 3 and 7. So 4,2,1 to dec.
So indeed they just assign singletons arbitrarily.
Thus the problem indeed seems to be checking frequency <=2 and outputting some partition.
But is there any scenario where it's impossible even if freq <=2? Consider the following: Suppose we have three values: 1,2,3 each appear twice? That's f(1)=2, f(2)=2, f(3)=2. Forced: one of each to inc, one of each to dec. So inc gets {1,2,3} distinct, fine. dec gets {1,2,3} distinct, fine. So works.
What about values where there is a pattern that forces both copies of a value to go to same group? No, forced to split.
Thus always possible.
But wait, maybe there is additional requirement that the increasing sequence must be strictly increasing, and the decreasing strictly decreasing, but also the sequences must be such that they were originally sequences (i.e., they had an order). But that doesn't add restrictions.
Given the problem's constraints, maybe there is a trick: The values can be up to 200,000, and n up to 200,000. So we need to count frequencies and output. That's O(n). So it's a simple problem.
But why would they ask this? Perhaps I misread: The sequences were initially increasing and decreasing, and they were merged into one sequence a, then a got shuffled. But maybe the merging preserved the relative order within each sequence? The phrase "merged into one sequence a" could be ambiguous: It might mean that they took the two sequences and interleaved them arbitrarily (like merging two sorted lists) but not necessarily preserving each sequence's order? Actually "merged" could mean they combined them into one sequence by concatenation or interleaving, but then "shuffled" means after merging, the whole sequence got shuffled, so order lost. So original order within each is lost. So no.
Alternatively, maybe they meant that the two sequences were concatenated and then the resulting sequence was shuffled, not that we can arbitrarily reorder; we just know that after shuffling, we get a. So we need to recover the two sequences (as multisets). So it's as we thought.
Given that the problem is rated maybe 1500? Actually I don't know. But it's from some contest; it might be easier than I think.
But let's double-check with a scenario that could be problematic: Suppose we have values: [1, 1, 2]. f1=2, f2=1. Works as earlier.
What about [0,0,0]? f=3 >2, NO.
Thus answer: if any frequency > 2 -> NO; else YES, and we need to output inc and dec. How to output? We need to decide assignment for singletons. We can simply put all singletons into dec (or inc) as long as it's consistent? But we need to ensure that inc and dec are strictly monotonic. If we put all singletons into dec, then dec might contain many values; but dec sorted descending is fine. Similarly inc will contain only the forced ones (one copy of each value with frequency 2). However, we must also consider if we put all singletons into dec, is it possible that dec then contains some value that also appears in inc? That's fine. So that assignment works.
But what if there are no values with frequency 2? Then we can put all into dec (or all inc). That's fine.
So algorithm:
Count frequencies of each value using array of size maybe up to 200000+5.
If any frequency > 2, print NO.
Else:
We will construct inc list and dec list.
For each value v:
if freq[v] == 2:
add v to inc and add v to dec.
else if freq[v] == 1:
add v to dec (or inc; we need to choose one; but maybe we need to ensure something? Perhaps we want inc and dec to be strictly increasing/decreasing, but if we put all singletons to dec, dec will have many values; but dec sorted descending is fine. However, inc might be empty or contain some values. That's acceptable.)
But we must output inc in increasing order, dec in decreasing order. So we need to sort inc ascending and dec descending. However, note that if we put all singletons into dec, dec will include those values, but we also have from freq==2: dec includes one copy of each such value. So dec may contain duplicates? Wait careful: For freq[v]==2, we added one v to inc and one v to dec. That's fine. For freq[v]==1, we add v to dec. So dec may contain v only once. There is no duplicate because if v has freq 1, it's only added once to dec; if v has freq 2, it's added once to dec; so each v appears at most once in dec. Similarly inc gets only from freq==2, each v at most once. So both lists have distinct values. So that's fine.
But we also need to output the sequences with their lengths. So we just build inc and dec.
However, we must be careful: The output format requires that the inc sequence is printed in increasing order, dec in decreasing order. So we need to sort inc ascending, dec descending.
But we must also ensure that inc and dec together are exactly the multiset of a. Since we assigned all copies exactly, that holds.
But wait: For freq[v]==2, we assigned one to inc and one to dec. That uses both copies. For freq[v]==1, we assigned to dec. That uses that copy. So all copies assigned. Good.
But is it always valid to assign all singletons to dec? Consider if inc becomes empty, that's allowed. So yes.
But what about the condition that dec must be strictly decreasing? If we have many singletons, dec sorted descending will be strictly decreasing because all values are distinct. So fine.
Thus answer is trivial: if max frequency <=2, YES, and construct as described.
But let's test with sample 2: Input 4 3 1 5 3. freq: 3:2, 4:1,1:1,5:1. Our algorithm: inc gets [3] (from freq2), dec gets [3 (other),4,1,5] -> dec list = [3,4,1,5]. Sorted descending: [5,4,3,1]. That's exactly sample output (dec: 5,4,3,1). inc: 3. That matches. Good.
Sample 1: Input 7 2 7 3 3 1 4. freq: 7:2, 3:2, 2:1,1:1,4:1. inc gets [7,3] (from freq2). Sort inc ascending: [3,7]. dec gets [7,3,2,1,4] (the other 7, other 3, plus 2,1,4). Sort dec descending: [7,4,3,2,1]. That matches sample output. Good.
Sample 4: Input 0 1 2 3 4 all once. freq all 1. inc gets nothing (since no freq2). dec gets all: [0,1,2,3,4]. Sort dec descending: [4,3,2,1,0]. inc empty. That matches sample output (they printed inc empty line, dec 5 numbers: 4 3 2 1 0). Yes.
So the algorithm works on samples.
But is there any reason why we wouldn't want to put all singletons into dec? For example, what if dec becomes not strictly decreasing because of something? No.
Thus the solution is straightforward.
But wait, I recall there is a Codeforces problem 1418D? Not.
Given the simplicity, maybe the problem originally had an additional constraint: The sequences must be such that the increasing sequence's maximum is less than the decreasing sequence's minimum, or something? But the problem statement doesn't mention that.
Let's re-read: "Two integer sequences existed initially — one of them was strictly increasing, and the other one — strictly decreasing. ... They were merged into one sequence a. After that sequence a got shuffled." It doesn't say anything about relationship between the two sequences. So they can be any.
Thus answer seems trivial.
But is it possible that the problem is from an online judge where the solution isn't so trivial? Actually, I think there is a known Codeforces problem called "Increasing and Decreasing" that is about reconstructing sequences from given values? But maybe it's from AtCoder? I'm trying to remember.
Wait, I recall a problem "A. Andryusha and Socks" no.
Given the input constraints up to 2e5, and the problem description, it might be from Codeforces Round # (maybe problem B). However, many participants might get tricked into thinking it's more complex. But it's plausible.
Let's search memory: There is a problem "Two Sequences" from Codeforces Round #642 (Div. 3) maybe? Actually there is a problem "Two Arrays" but not. I think I've seen a problem where you need to split a multiset into an increasing and a decreasing sequence, and the solution is to check that no value appears more than twice. Because if it appears three times, you would have to put at least two of them in the same sequence, violating strict monotonicity. That's exactly the reasoning. So it's likely the intended solution.
Thus the problem is indeed that simple.
But there is a nuance: The sequences are "strictly increasing" and "strictly decreasing". Could they be empty? Yes. So if all frequencies <= 2, we can always split.
Now we need to produce the output. Implementation details:
Read n, read array a.
Create freq array of size maybe 200001 (since a_i up to 200000). Use vector freq(max_val+1, 0). Actually we need to know the maximum value to allocate? But we can just use map if we want to avoid large memory, but 200001 is fine. But values can be up to 200000, so size 200001 is okay. Use vector freq(200001) or maybe 200005.
Count frequencies.
If any freq > 2, print NO and exit.
Else:
Create two vectors: inc, dec.
For each value v from 0 to 200000 (or iterate over the unique values present, maybe using a set of all values to avoid scanning entire range? But scanning up to 200k is fine, O(200k) which is okay).
if freq[v] == 2:
inc.push_back(v);
dec.push_back(v);
else if freq[v] == 1:
// we can put to dec (or inc). Let's put to dec.
dec.push_back(v);
else: // 0 ignore.
After building, sort inc ascending (which it may already be if we iterate v ascending, but inc gets added in increasing order of v because we iterate v increasing. But if we iterate in ascending v, inc will be naturally sorted ascending (since we push in order). However, careful: we push in ascending order, so inc is already sorted ascending. Similarly, dec if we push in ascending order, it will be ascending, but we need to output dec in decreasing order. So we can sort dec descending. Or we can just push in descending order later. Since we iterate v ascending, dec list will be in ascending order; we can reverse it to get descending. That's fine.
But we must also ensure that the output lengths: n_i = inc.size(), n_d = dec.size().
Print "YES"
Print n_i
If n_i >0, print inc values separated by spaces; else print empty line (just print newline). According to format, you can print empty line or just nothing? Usually you should just print a newline after the number (if zero, the line may be empty). So we can print after printing n_i, then on next line we either print nothing (just newline) or we can skip printing if n_i=0 but still need a newline? Let's see format: "In the third line print n_i integers inc_1, inc_2, ..., inc_{n_i} in the increasing order of its values (inc_1 < inc_2 < ... < inc_{n_i}) — the strictly increasing sequence itself. You can keep this line empty if n_i = 0 (or just print the empty line)." So we can print just a newline (i.e., an empty line). So after printing n_i, we print a line (which may be empty). So we can do: cout << n_i << endl; if (n_i>0) { for ... } cout << endl; (if n_i==0 just an empty line). Similarly for dec.
But careful: In sample output for inc empty, they printed:
YES
0
5
4 3 2 1 0
Actually the sample output shows:
YES
2
3 7
5
7 4 3 2 1
In sample 1, they printed after inc numbers a space after 7? Possibly trailing space. For empty inc, they likely printed just a blank line after the 0. So we need to output a newline after the line of numbers, even if empty. So we can just print a newline after the line.
Implementation details: We'll use printf or cout.
Complexities: O(n + MAX_VAL) which is fine.
Now, one more nuance: We assigned all singletons to dec. But what if we assign all to inc? That would also work. But no issue.
Now, double-check edge: If there are many values with freq=2, inc and dec both get those values. So inc list might be non-empty. If we put all singletons to dec, dec may have many values. That's fine.
But is there a scenario where putting all singletons to dec might cause dec to not be strictly decreasing due to ordering? No.
Thus final answer.
But wait: I recall a known Codeforces problem: "A. Shuffle" maybe? Not.
Alright.
However, before finalizing, let's reflect if there is any hidden condition: The problem states "two suitable initial sequences." The initial sequences were one strictly increasing and the other strictly decreasing. However, consider that the initial sequences themselves might have been in some order, but we are only required to output sequences with the correct order (inc increasing, dec decreasing). That is fine.
But what about the scenario where there is a value that appears twice, and we assign one to inc and one to dec. That's forced. However, if we assign all singletons to dec, inc only contains the forced ones. That's okay.
But is it always possible that inc is strictly increasing? Since inc contains one copy of each v with freq=2. If we iterate v ascending, inc is increasing, yes. However, what if there are multiple values with freq=2, inc will be sorted ascending. So okay.
Now, what if there are no values with freq=2? Then inc is empty; that's allowed. So we can output inc empty.
Thus always works.
Thus solution is O(n). So why is this problem considered? Maybe it's just an easy problem to teach frequency counting.
But I've a doubt: The problem may have originally required to output the sequences in the order they originally appeared? The statement: "Your task is to find any two suitable initial sequences." Not the order, just the sequences. The output format says print inc in increasing order and dec in decreasing order. So we are to output sorted.
Thus it's fine.
Alright.
Now, I'll write the solution.
But wait: Let's test with an edge case: n=200000, all values are 0? That would be freq[0] = 200000 >2 -> NO. So output NO.
Another: n=200000, values random between 0 and 200000, likely some duplicates might exceed 2? But not guaranteed; but algorithm handles.
Thus solution seems correct.
But I've second thoughts: Could there be a situation where frequency <=2 but still impossible because the inc sequence cannot be strictly increasing if it contains values that are not distinct? But we already ensure distinctness. So no.
What about the fact that strictly increasing means each successive element must be larger than previous. If we have inc list with values [2, 1]? But we sort it, so output will be [1,2]. That's increasing. So we output sorted.
Thus no issue.
Now, is there any chance that the problem expects to output the sequences not just the values but also the original indices? No.
Thus final answer.
But wait, there is a nuance: The problem says "Your task is to find any two suitable initial sequences." That might imply we need to reconstruct the actual sequences as they were before merging and shuffling. But they were originally strictly increasing and decreasing. However, if we output inc sorted ascending and dec sorted descending, we are effectively claiming that the original sequences were in sorted order. But were they necessarily sorted? The problem statement says the initial sequences were strictly increasing and strictly decreasing. So they were sorted by definition. So yes, they were in sorted order. So we can output them sorted.
Thus correct.
Thus solution is done.
But hold on: The phrase "strictly increasing sequence is a sequence of integers [x_1 < x_2 < ... < x_k]" implies that the sequence itself is in that order. So the original inc had x_1 < x_2 < ... in that order. So if we output inc in increasing order, we are outputting exactly that sequence. Similarly for dec.
Therefore it's consistent.
Alright.
Now I'll write code.
Implementation steps:
Read n.
vector a(n)
freq array: vector freq(200001, 0) (or maybe 200005)
Actually a_i <= 200000, so index up to 200000. Use size 200001 to be safe.
For each a_i: freq[a_i]++.
Also track max_val for iteration if we want to limit loops.
Check if any freq[i] > 2: if so print "NO" and return.
Else:
vector inc, dec.
For i from 0 to max_val (or up to 200000) but we can find max_val = max element in a to shorten loop.
For each i in range [0, max_val] where freq[i] > 0? But we also need to include zeros if they appear. Better to loop through all possible values (0..200000) but it's okay. 200k loop is fine.
For each v:
if freq[v] == 2:
inc.push_back(v);
dec.push_back(v);
else if freq[v] == 1:
dec.push_back(v); // choose dec
// But note: inc gets only from freq==2. That's fine.
// But we must also consider: what if we have freq[v]==0? ignore.
Sort inc ascending (already ascending if we loop v ascending, but to be safe we can sort). Similarly sort dec ascending then reverse for descending output, or just sort descending using greater().
Output:
cout << "YES\n";
cout << inc.size() << "\n";
if (inc.size() > 0) {
for (int x : inc) cout << x << " ";
}
cout << "\n"; // newline even if empty
cout << dec.size() << "\n";
if (dec.size() > 0) {
// print dec in decreasing order
// we have dec currently maybe unsorted? We'll sort descending.
sort(dec.begin(), dec.end(), greater());
for (int x : dec) cout << x << " ";
}
cout << "\n";
Note: In sample output, they had a trailing space after the last number. That's okay.
Now, one more thing: Could there be a situation where inc is empty but we still need to output an empty line after the zero? Yes.
Now, test with sample 1:
inc = [7,3]? Actually loop v from 0 to max (7) we push when freq==2: v=3 and v=7 (freq 2). inc = [3,7] if we push in order: v=0..7: at v=3 push 3, at v=7 push 7. So inc = [3,7] already ascending. Good.
dec: we push for freq==2: at v=3 push 3, at v=7 push 7; and for freq==1: v=1,2,4. So dec list in order: v=1 push1, v=2 push2, v=3 push3, v=4 push4, v=7 push7? Actually careful: we loop v ascending: we encounter v=1 (freq1) -> push 1; v=2 -> push2; v=3 (freq2) -> push3; v=4 -> push4; v=7 -> push7. So dec = [1,2,3,4,7] in ascending order. Then we sort descending: [7,4,3,2,1]. That's output. Perfect.
Sample 4: all freq1. inc empty. dec = all values 0,1,2,3,4 in ascending; sort descending -> [4,3,2,1,0]. Good.
Thus correct.
Now consider scenario: If we have freq[v]==2 and we push both to inc and dec, that's fine.
But is there any scenario where we might want to assign a singleton to inc to avoid something? No.
Thus final answer.
Now, one more nuance: The problem says "Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing." So for inc with 1 element, it's valid; for dec with 1 element, valid.
Thus all good.
Time complexity: O(n + MAX_VAL) ~ 400k operations, fine.
Now, maybe the problem originally had additional constraint: The sequences must be non-empty? But note: The problem says "the empty sequence ... can be considered as increasing or decreasing." So empty allowed.
Thus done.
But wait, the sample 2 output: they printed inc as "3 " (with trailing space) and dec as "5 4 3 1 " (with trailing space). Our output may also have trailing spaces; it's okay.
Now, we should check if there is any scenario where this assignment might fail because dec cannot be strictly decreasing? Actually, if dec has values that are not all distinct, then it fails. But we ensured distinctness. However, is it possible that the dec list after sorting descending could have equal adjacent? No, because values are distinct. So fine.
But what about values that are equal across groups? That's okay.
Thus we are done.
I will write the solution accordingly.
But wait, we need to be careful: The inc sequence we output must be strictly increasing. If inc contains values that are not in increasing order? We'll output sorted ascending, so it's increasing.
Thus final.
Now, I'll produce final answer.
But before final, let's double-check problem statement from the user: It says "Two integer sequences existed initially — one of them was strictly increasing, and the other one — strictly decreasing." Then they were merged and shuffled. Input is n and a. We need to find any two suitable initial sequences. This is exactly the same as I recall from a known Codeforces problem: "A. Increasing and Decreasing?" Actually the problem title might be "Increasing and Decreasing" but I'm not 100% sure. Let's quickly search memory: There is Codeforces Round # (maybe 1133) problem "A. Middle of the Contest"? Not.
Anyway, the solution is as described.
Given the simplicity, it's likely correct.
Thus I'll provide final code.
Edge Cases:
n=0? But n≥1.
Values up to 200000, so freq array size 200001 is okay.
Large n, many distinct values, all freq1. Then inc empty, dec has many values. Sorting dec descending O(m log m) with m up to 200k, which is okay.
But we might improve: Since we built dec in ascending order if we iterate v descending for dec, we can avoid sorting. For simplicity, we can just sort.
Alternatively, we can create two vectors: one for inc (push v when freq==2, in ascending order) and for dec we can push v when freq==2 or ==1, but we need dec in descending order. So we can iterate v from max_val down to 0 and push v if freq[v] >= 1? But careful: For freq[v]==2, we also need to push one copy to dec. So we can build dec in descending order directly by iterating v descending and pushing v for each occurrence that goes to dec. But for freq[v]==2, we push v once; for freq[v]==1, push v once. That yields dec already descending. That is more efficient (no sorting). For inc, we iterate ascending and push v for freq==2. That yields inc ascending.
Thus we can avoid sorting. Let's implement that to be O(MAX_VAL) with simple loops.
Implementation:
Determine max_val = maximum element in a (or just 200000).
Build inc: for v from 0 to max_val (or 0..200000): if freq[v] == 2: inc.push_back(v).
Build dec: for v from max_val down to 0: if freq[v] == 2 or freq[v] == 1: dec.push_back(v). But careful: For freq[v]==2 we push exactly one copy; for freq[v]==1 we push one. So condition: if freq[v] >= 1: push? But that would push v twice if freq[v]==2? Actually we only want one copy in dec for each occurrence? Wait, dec should contain exactly one copy of each value that we assign to dec. For freq[v]==2, we assign exactly one copy to dec. So we push v once. For freq[v]==1, we assign that single copy to dec, push v once. So yes, condition: if freq[v] >= 1? But then for freq[v]==2, that would also be true, but we only push once (since we are iterating over values, not occurrences). So we need to push v if we assign it to dec. Which is: if freq[v] == 2 or freq[v] == 1 (since we assign all such to dec). But we could also assign some singletons to inc if we wanted; but our plan assigns all singletons to dec. So condition: if freq[v] == 2 or freq[v] == 1 -> push v in dec (once). That's equivalent to freq[v] >= 1? Actually if freq[v]==0, not; if freq[v]==1 or 2, push. So yes, freq[v] >= 1. But careful: If freq[v]==2, we push v once in dec, correct. So condition is simply freq[v] >= 1? That would push v for freq[v]==2, which is correct (once). So we can do: for v descending: if freq[v] > 0: dec.push_back(v). That would assign exactly one copy of each value that appears at least once to dec. But is that correct? For values with freq 2, we need exactly one copy in dec and one in inc. That is satisfied: dec gets one, inc gets one (from inc loop). For values with freq 1, we need that one copy to be assigned either to inc or dec. Our plan is to assign all to dec. So dec gets that one copy. So indeed dec gets all values that appear at least once (once each) if we push each distinct value once. But wait, dec should contain exactly one occurrence per distinct value? Actually, for values with freq 2, there are two occurrences. One goes to inc, one to dec. So dec contains one occurrence of that value. For freq 1, dec contains that one occurrence. So dec's multiset contains exactly one copy of each value that appears (since for freq 2 we include one copy; for freq 1 we include that copy). So yes, dec's set of values is exactly the set of distinct values from the original multiset. But is that always valid? Let's verify: Suppose we have values: [1,2,2,3]. Distinct values: 1,2,3. Freq: 1:1, 2:2, 3:1. Our algorithm: inc gets from freq2: 2. So inc = [2]. dec gets all distinct values: 1,2,3 (in descending order). But dec would then have one copy of 2, one copy of 1, one copy of 3. That uses occurrences: for value 2, we need two occurrences: one in inc, one in dec. So dec gets one occurrence of 2, inc gets the other. That's good. For value 1, we have one occurrence, assigned to dec. For value 3, one occurrence, assigned to dec. So total: inc uses one occurrence of 2; dec uses one occurrence of 2, one of 1, one of 3. Total occurrences used: 1+ (1+1+1) = 4, matches. So dec gets exactly one copy per distinct value, which means that the multiset of dec is the set of distinct values (each once). But is that always correct? What about values with freq 2: they appear twice, dec gets one copy; that's fine. So dec's cardinality equals number of distinct values. inc's cardinality equals number of values with freq 2 (since each contributes one). So total n = inc.size() + dec.size() = (#distinct) + (#freq2). But note that #distinct = (#freq1) + (#freq2). So n = (#freq1) + 2*(#freq2). Indeed inc.size() = #freq2, dec.size() = #freq1 + #freq2 = #distinct. So that works.
Thus we can simply output inc = list of v where freq[v]==2 (sorted ascending), and dec = list of all v where freq[v] > 0 (sorted descending). That's a clean construction.
But wait: For a value with freq 2, dec includes it once, which is fine. For a value with freq 1, dec includes it once. So dec list is just the distinct values. That's a valid assignment: each distinct value appears exactly once in dec (except those with freq 2 which also appear once in inc). But does this use each occurrence exactly? Let's verify: For a value v with freq 2, we need to allocate two copies: one in inc, one in dec. Our dec includes one copy of v. inc includes one copy of v. So both copies allocated. For v with freq 1, dec includes that copy. So all copies allocated. Good.
Thus the construction is: inc = sorted list of values that appear at least twice. dec = sorted list (descending) of all distinct values. That's very simple.
But is this always valid? Check sample 1: distinct values: 1,2,3,4,7. freq2: 3,7. inc = [3,7]. dec (descending) = [7,4,3,2,1]. That's exactly output. Sample 2: distinct values: 1,3,4,5. freq2: 3. inc = [3]; dec descending = [5,4,3,1]. Output matches. Sample 4: distinct: 0,1,2,3,4. inc none; dec descending = [4,3,2,1,0]. Output matches.
Thus we don't even need to worry about assigning singletons to dec; it's automatic.
But wait, what about a value that appears twice but we might consider putting both copies in dec? That would be invalid because dec would have duplicate. So we must assign one to inc. So the above construction forces inc to have exactly the values with frequency 2. That's a forced assignment because if we put both copies in dec it's invalid, and we can't put both in inc. So indeed for freq2, one must go to inc, one to dec. So the only choice is which of the two copies goes to inc? But the value is the same; the inc gets one copy, dec gets one copy. So inc must contain that value (one copy) and dec must contain that value (one copy). So inc necessarily includes all values with frequency 2? Actually, could we assign a value with frequency 2 such that one copy goes to inc and one to dec; that is forced. So inc will contain that value, and dec will contain that value. So inc must contain every value that appears twice? Yes, because if a value appears twice, we cannot avoid putting one copy in inc; if we try to put both in dec, it's invalid; both in inc invalid. So inc must contain exactly one copy of each value with freq=2. So inc is exactly the set of values with frequency 2 (one copy each). Similarly, dec must contain exactly one copy of each value with frequency 2, and also all copies of values with frequency 1. However, for values with frequency 1, we could choose to put them in inc instead of dec. Could we? If we put a singleton value in inc, then dec would not contain it. Is that allowed? Let's see: For a value v with freq=1, we have one copy. If we put it in inc, then inc gets v, dec does not. That would mean dec does not contain v. In our earlier construction, we put all singletons in dec, but we could put some in inc. But does that affect validity? It could, but it's still valid as long as inc remains distinct. Since inc already contains values with freq=2, adding singletons might cause duplicate? No, if v is not already in inc (since if v appears only once, it's not in the freq2 set, so inc currently doesn't have v), so adding it to inc would be fine. And dec would then not contain v. But then the distinctness condition still holds. So there is flexibility. However, our simple construction (inc = freq2 values, dec = all distinct values) is a valid one, because it uses one copy of each freq2 in inc and one copy in dec, and all freq1 in dec. That satisfies distinctness in both groups. So it's correct.
But wait, is it necessary that dec contains all distinct values? In our construction, yes. But is it possible that this assignment could cause a problem with dec being strictly decreasing? Since dec contains all distinct values, if there are many, it's fine. No problem.
Thus solution reduces to: if any frequency > 2 => NO; else output inc = sorted list of values that appear at least twice, dec = sorted list (descending) of all distinct values.
But careful: For values with freq=2, they appear in both inc and dec. That's fine.
Now, verify that this assignment always yields correct counts: inc size = count of values with freq>=2; dec size = number of distinct values. Sum = distinct + count_ge2 = (count_freq1 + count_ge2) + count_ge2 = count_freq1 + 2*count_ge2 = n. Good.
Thus we can implement as:
freq array size 200001 (or use map). Also track distinct values.
Check for any freq>2.
If OK, create inc list: for v from 0 to max_val, if freq[v] >= 2, add v to inc. (Or we can iterate over all distinct values stored in a set to avoid scanning full range, but scanning 200001 is okay.)
Create dec list: for v from max_val down to 0, if freq[v] > 0, add v to dec.
Output.
But one nuance: For values with freq>=2, we add v to inc. However, what if a value appears exactly 2? That's >=2. So add. If a value appears more than 2? Already excluded. So it's fine.
Now, what about value 0? It's fine.
Edge case: If there are no values with freq>=2, inc is empty. That's allowed.
Now, confirm with a test: Input: n=2, a=[1,1]. freq[1]=2. inc = [1], dec distinct = [1] (since freq>0, dec gets 1). dec descending = [1]. Output: inc:1, dec:1. That's valid? inc is [1] (strictly increasing? Single element allowed), dec is [1] (single element allowed). Yes.
Another: n=3, a=[1,2,2]. freq:1=1,2=2. inc = [2], dec = distinct [2,1] descending -> [2,1]. Check: inc uses one copy of 2, dec uses one copy of 2 and the 1. So inc size 1, dec size 2, total 3. Works.
Now, suppose we had a value that appears twice and we try to put both in dec? Not allowed. Our construction is forced.
Thus solution is straightforward.
But wait: Could there be a scenario where the distinct values list includes a value with freq=2, and dec includes that value, but dec also includes that same value? No duplicates. That's fine.
Thus answer is simple.
Now, I'll write final solution code in C++.
Implementation details:
Use fast I/O.
Use vector freq(200001,0). Actually max value is 200000, so size 200001 inclusive.
After reading a, compute max_val for loop limit, but we can also loop full range.
Check freq>2.
Then:
vector inc, dec;
for (int i = 0; i <= 200000; ++i) {
if (freq[i] > 1) inc.push_back(i);
}
for (int i = 200000; i >= 0; --i) {
if (freq[i] > 0) dec.push_back(i);
}
But careful: For inc, we only want values with freq >=2. That's correct.
Alternatively, we could iterate over all i from 0 to max_val. But 200k loop is fine.
Then output.
Time complexity: O(n + MAX_VAL) = 400k ~ fine.
Memory: O(n) but we only store freq and two lists.
Now, check if there is any scenario where inc could have duplicate? No, each i added once.
Now, note: The problem says "strictly increasing sequence is a sequence of integers [x_1 < x_2 < ... < x_k]". So inc must be strictly increasing. Our inc is sorted ascending, so fine.
Now, one more nuance: The output lines must have numbers separated by spaces, and a newline after each line. We'll do that.
Thus final code.
But wait, I'm thinking: Could there be a scenario where the inc list we produce (values with freq >=2) is not strictly increasing because of some weirdness? No.
Now, I'll write final answer.
But let's think: The problem might be from Codeforces Round # (maybe 1791C?). Not.
Anyway, final solution.
Now, I'll produce the final response with explanation and code.
But perhaps the problem has a twist: The input sequence a may have been shuffled, but we need to output the sequences themselves. But what if the original increasing sequence had to be strictly increasing, and the decreasing strictly decreasing, but also the increasing sequence must be the one that was initially increasing, meaning that if there are two copies of a value, one in inc and one in dec, that could violate something? For strictly decreasing, you cannot have equal adjacent. But if dec contains the same value as inc, that's irrelevant because they are separate sequences. So fine.
Now, I'll present the solution.
One more thought: Is it possible that there is a scenario where freq <=2 but still impossible because the inc and dec sequences must be such that they are the original sequences and we don't know which occurrence belongs to which? But since we output the sequences, we just need to partition the multiset. The problem doesn't ask to assign which occurrence went where; just output the sequences as multisets sorted. So it's fine.
Thus final.
Now, I'll write answer.
But wait, let's verify with a random test: n=6, a = [1,2,3,1,2,3]. All values freq=2. inc = [1,2,3]; dec = distinct values descending = [3,2,1]. Check: inc uses one copy of each, dec uses the other copy of each. Works.
Now, what about a = [0,0,1,1,2,2]? inc = [0,1,2]; dec = [2,1,0] distinct descending. Works.
Thus all good.
Therefore answer is as above.
Now, produce final answer.
But the problem might be from a contest where they expected solution with counting frequencies and constructing inc/dec as described. It's indeed trivial but maybe many participants overthought.
Alright.
Now, I'll provide final solution in C++.
Pseudo-code:
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector a(n);
const int MAX = 200000;
vector freq(MAX+1, 0);
int maxVal = 0;
for (int i = 0; i < n; ++i) {
cin >> a[i];
freq[a[i]]++;
if (freq[a[i]] > 2) {
// but we will check after all input
}
maxVal = max(maxVal, a[i]);
}
for (int v = 0; v <= MAX; ++v) {
if (freq[v] > 2) {
cout << "NO\n";
return 0;
}
}
vector inc, dec;
for (int v = 0; v <= MAX; ++v) {
if (freq[v] >= 2) inc.push_back(v);
}
for (int v = MAX; v >= 0; --v) {
if (freq[v] > 0) dec.push_back(v);
}
cout << "YES\n";
cout << inc.size() << "\n";
for (int x : inc) cout << x << " ";
cout << "\n";
cout << dec.size() << "\n";
for (int x : dec) cout << x << " ";
cout << "\n";
return 0;
}
But we need to handle the case where inc.size()=0: print a blank line. The code above prints a newline after the 0 and then an empty line? Actually: we print "0\n" then we loop over inc (empty) and print nothing, then we print "\n". So that results in two newlines after the "0"? Let's check: The output for inc part: line1: "0\n" (the length). Then we output for the sequence: we do for loop (no output) then cout << "\n". That prints an extra newline, resulting in an empty line. So the output becomes:
0
(empty line)
... This matches the format: after the number, next line is the sequence (which may be empty). So we need a newline after the sequence line. So our code is correct: after printing inc.size() we have a newline, then we print the inc elements (if any) and then a newline. So we get exactly two lines for inc: one for size, one for sequence (maybe empty). Good.
Similarly for dec.
Thus final.
But we should also consider that there might be trailing spaces, but that's acceptable.
Now, produce answer.
But I'll also discuss reasoning in the explanation.
Now, final answer. ```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
code
int n;
cin >> n;
const int MAX_VAL = 200000;
vector<int> freq(MAX_VAL + 1, 0);
for (int i = 0; i < n; ++i) {
int x;
cin >> x;
freq[x]++;
}
// check impossibility
for (int v = 0; v <= MAX_VAL; ++v) {
if (freq[v] > 2) {
cout << "NO\n";
return 0;
}
}
// build inc (values that appear at least twice, in increasing order)
vector<int> inc;
for (int v = 0; v <= MAX_VAL; ++v) {
if (freq[v] >= 2)
inc.push_back(v);
}
// build dec (all distinct values, in decreasing order)
vector<int> dec;
for (int v = MAX_VAL; v >= 0; --v) {
if (freq[v] > 0)
dec.push_back(v);
}
// output
cout << "YES\n";
cout << inc.size() << "\n";
for (int x : inc) cout << x << " ";
cout << "\n";
cout << dec.size() << "\n";
for (int x : dec) cout << x << " ";
cout << "\n";
return 0;