G. Two Merged Sequences

G. Two Merged Sequences

           time limit per test

           2 seconds

           memory limit per test

           256 megabytes


Two integer sequences existed initially, one of them was strictly increasing, and another 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.

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][1,3,4] and [10,4,2][10,4,2] can produce the following resulting sequences: [10,1,3,4,2,4][10,1,3,4,2,4], [1,3,4,10,4,2][1,3,4,10,4,2]. The following sequence cannot be the result of these insertions: [1,10,4,4,3,2][1,10,4,4,3,2] because the order of elements in the increasing sequence was changed.

Let the obtained sequence be aa. This 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 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 aa into one increasing sequence and one decreasing sequence, print "NO".

Input

扫描二维码关注公众号,回复: 9054749 查看本文章

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 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 nn integers res1,res2,…,resnres1,res2,…,resn, where resiresishould be either 00 or 11 for each ii from 11 to nn. The ii-th element of this sequence should be 00 if the ii-th element of aa belongs to the increasing sequence, and 11 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

转自一位大神的代码    codeforces   ID:vjudge3

AC代码:

#include<bits/stdc++.h>
#define MAXN 200005
using namespace std;
int up,down,n,a[MAXN];
int main()
{
	scanf("%d",&n);
	for(int i = 1;i <= n;++i)
		scanf("%d",&a[i]);
	up = -1,down = MAXN,a[n+1] = MAXN;
	for(int i = 1;i <= n;++i)
	{
		if(a[i] < down && a[i] > up)  // 贪心的思想
		{
			if(a[i] < a[i+1])
				up = a[i],a[i] = 0;
			else
				down = a[i],a[i] = 1;
		}
		else if(a[i] < down)
			down = a[i],a[i] = 1;
		else if(a[i] > up)
			up = a[i],a[i] = 0;
		else
			{printf("NO");return 0;}
	}
	printf("YES\n");
	for(int i = 1;i <= n;++i)
		printf("%d ",a[i]);
	return 0;//
}
发布了46 篇原创文章 · 获赞 81 · 访问量 5551

猜你喜欢

转载自blog.csdn.net/zfq17796515982/article/details/88982375
今日推荐