DAY1 Two Shuffled Sequences

C. Two Shuffled Sequences

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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 [x1<x2<⋯<xk][x1<x2<⋯<xk]. And strictly decreasing sequence is a sequence of integers [y1>y2>⋯>yl][y1>y2>⋯>yl]. 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 aa. After that sequence aa got shuffled. For example, some of the possible resulting sequences aa for an increasing sequence [1,3,4][1,3,4] and a decreasing sequence [10,4,2][10,4,2] are sequences [1,2,3,4,4,10][1,2,3,4,4,10] or [4,2,1,10,4,3][4,2,1,10,4,3].

This shuffled sequence aa 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 aa to increasing and decreasing sequences, print “NO”.

Input
The first line of the input contains one integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the number of elements in aa.

The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤2⋅1050≤ai≤2⋅105), where aiai is the ii-th element of aa.

Output
If there is a contradiction in the input and it is impossible to split the given sequence aa 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 nini — the number of elements in the strictly increasing sequence. nini can be zero, in this case the increasing sequence is empty.

In the third line print nini integers inc1,inc2,…,incniinc1,inc2,…,incni in the increasing order of its values (inc1<inc2<⋯<incniinc1<inc2<⋯<incni) — the strictly increasing sequence itself. You can keep this line empty if ni=0ni=0 (or just print the empty line).

In the fourth line print ndnd — the number of elements in the strictly decreasing sequence. ndnd can be zero, in this case the decreasing sequence is empty.

In the fifth line print ndnd integers dec1,dec2,…,decnddec1,dec2,…,decnd in the decreasing order of its values (dec1>dec2>⋯>decnddec1>dec2>⋯>decnd) — the strictly decreasing sequence itself. You can keep this line empty if nd=0nd=0 (or just print the empty line).

ni+ndni+nd should be equal to nn and the union of printed sequences should be a permutation of the given sequence (in case of “YES” answer).

Examples
inputCopy
7
7 2 7 3 3 1 4
outputCopy
YES
2
3 7
5
7 4 3 2 1
inputCopy
5
4 3 1 5 3
outputCopy
YES
1
3
4
5 4 3 1
inputCopy
5
1 1 2 1 2
outputCopy
NO
inputCopy
5
0 1 2 3 4
outputCopy
YES
0

5
4 3 2 1 0

思路&过程

先排序,然后有重复的话就升序丢一个降序丢一个,如果连着三个一样就不符合。
一开始WA了,之后发现是有个地方数组下标越界了,然后如果遇到0这种极端情况就会出问题。

#include<iostream>
#include<algorithm>
using namespace std;

int main()
{
	int n,m,i,j,k,p,q;
	bool t;
	static int a[200010],b[200010],c[200010];
	cin>>n;
	for(i=0;i<n;i++)
	{
		cin>>a[i];
	}
	sort(a,a+n);
	i=0;
	p=0;q=0;
	t=true;
	while((i<n)&&(t))
	{
		if((i<n-2)&&(a[i]==a[i+1])&&(a[i+1]==a[i+2]))
		{
			cout<<"NO"<<endl;
			t=false;	
		}
		if((a[i]==a[i+1])&&(i+1<n))
		{
			p=p+1;
			q=q+1;
			b[p]=a[i];
			c[q]=a[i];
			i=i+2;
		}	
		else
		{
			q=q+1;
			c[q]=a[i];
			i=i+1;
		}
	}
	if (t)
	{
		cout<<"YES"<<endl;
		cout<<p<<endl;
		for(i=1;i<=p;i++) cout<<b[i]<<" ";
		cout<<endl;
		cout<<q<<endl;
		for(i=q;i>=1;i--) cout<<c[i]<<" ";
	}
	return 0;
}在这里插入代码片

猜你喜欢

转载自blog.csdn.net/ArwenNi/article/details/88997440