Codeforces Round #504-D-Array Restoration(线段树)

D. Array Restoration

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Initially there was an array aa consisting of nn integers. Positions in it are numbered from 11 to nn.

Exactly qq queries were performed on the array. During the ii-th query some segment (li,ri)(li,ri) (1≤li≤ri≤n)(1≤li≤ri≤n) was selected and values of elements on positions from lili to riri inclusive got changed to ii. The order of the queries couldn't be changed and all qq queries were applied. It is also known that every position from 11 to nn got covered by at least one segment.

We could have offered you the problem about checking if some given array (consisting of nn integers with values from 11 to qq) can be obtained by the aforementioned queries. However, we decided that it will come too easy for you.

So the enhancement we introduced to it is the following. Some set of positions (possibly empty) in this array is selected and values of elements on these positions are set to 00.

Your task is to check if this array can be obtained by the aforementioned queries. Also if it can be obtained then restore this array.

If there are multiple possible arrays then print any of them.

Input

The first line contains two integers nn and qq (1≤n,q≤2⋅1051≤n,q≤2⋅105) — the number of elements of the array and the number of queries perfomed on it.

The second line contains nn integer numbers a1,a2,…,ana1,a2,…,an (0≤ai≤q0≤ai≤q) — the resulting array. If element at some position jj is equal to 00then the value of element at this position can be any integer from 11 to qq.

Output

Print "YES" if the array aa can be obtained by performing qq queries. Segments (li,ri)(li,ri) (1≤li≤ri≤n)(1≤li≤ri≤n) are chosen separately for each query. Every position from 11 to nn should be covered by at least one segment.

Otherwise print "NO".

If some array can be obtained then print nn integers on the second line — the ii-th number should be equal to the ii-th element of the resulting array and should have value from 11 to qq. This array should be obtainable by performing exactly qq queries.

If there are multiple possible arrays then print any of them.

Examples

input

Copy

4 3
1 0 2 3

output

Copy

YES
1 2 2 3

input

Copy

3 10
10 10 10

output

Copy

YES
10 10 10 

input

Copy

5 6
6 5 6 2 2

output

Copy

NO

input

Copy

3 5
0 0 0

output

Copy

YES
5 4 2

Note

In the first example you can also replace 00 with 11 but not with 33.

In the second example it doesn't really matter what segments to choose until query 1010 when the segment is (1,3)(1,3).

The third example showcases the fact that the order of queries can't be changed, you can't firstly set (1,3)(1,3) to 66 and after that change (2,2)(2,2) to 55. The segment of 55 should be applied before segment of 66.

There is a lot of correct resulting arrays for the fourth example.

题意:给你一个长为n序列,然后有q次操作,每次操作可以选择任意一个非空区间,令该区间所有的值为i,然后给你一个最终序列,问你q次操作后是否能构造出该序列,若某一位上是0,则可以给他任意定值。

题解:我们可以想到有以下几种非法情况:

(1)结果序列的最大值小于q

(2)某两个值一样的位置中间有比该值小的数。

对于(1)我们直接特判即可,对于(2)可以想用id数组标记每个值上次出现的位置,当这次出现时,直接线段树查询这个区间里的最小值,然后和该值比较即可。

#include<cstdio>
int a[800010];
struct node 
{
	int left,right,min,max;
}num[800010];
 int id[200005];
int MIN(int a,int b)
{
	return a<b?a:b;
}
 
int MAX(int a,int b)
{
	return a>b?a:b;
}
 
int buildmin(int left,int right,int cnt)
{
	int mid;
	num[cnt].left=left;
	num[cnt].right=right;
	if(left==right)
		return num[cnt].min=a[left];
	mid=(left+right)>>1;
	return num[cnt].min=MIN(buildmin(left,mid,cnt*2),buildmin(mid+1,right,cnt*2+1));
}
 
int buildmax(int left,int right,int cnt)
{
	int mid;
	num[cnt].left=left;
	num[cnt].right=right;
	if(left==right)
		return num[cnt].max=a[left];
	mid=(left+right)>>1;
	return num[cnt].max=MAX(buildmax(left,mid,cnt*2),buildmax(mid+1,right,cnt*2+1));
}
 
int querymin(int left,int right,int cnt)
{
	int mid;
	if(left==num[cnt].left&&right==num[cnt].right)
		return num[cnt].min;
	mid=(num[cnt].left+num[cnt].right)>>1;
	if(right<=mid)
		return querymin(left,right,cnt*2);
	else if(left>mid)
		return querymin(left,right,cnt*2+1);
	else 
		return MIN(querymin(left,mid,cnt*2),querymin(mid+1,right,cnt*2+1)); 
}
 
int querymax(int left,int right,int cnt)
{
	int mid;
	if(left==num[cnt].left&&right==num[cnt].right)
		return num[cnt].max;
	mid=(num[cnt].left+num[cnt].right)>>1;
	if(right<=mid)
		return querymax(left,right,cnt*2);
	else if(left>mid)
		return querymax(left,right,cnt*2+1);
	else
		return MAX(querymax(left,mid,cnt*2),querymax(mid+1,right,cnt*2+1));
}
 
int main()
{
	int q,maxs=0,mins=100000000;
	int t,n,m,i,j,C,L,R;
	int flag=0;
	scanf("%d%d",&n,&q);
	for(i=1;i<=n;++i)
	{
		scanf("%d",&a[i]);
		if(a[i]) flag=1;
		maxs=MAX(maxs,a[i]);
		mins=MIN(mins,a[i]);
	}
	if(maxs<q && mins>0)
	{
		printf("NO\n");
		return 0;
	}
	if(flag==0)
	{
		printf("YES\n");
		for(int i=1;i<=n;i++)
			printf("%d ",q);
		printf("\n");
		return 0;
	}
	for(int i=1;i<=n;i++)
		if(maxs<q && a[i]==0)
		{
			a[i]=q;
			break;
		}
	if(a[1]==0) a[1]=1;
	for(int i=2;i<=n;i++)
		if(a[i]==0)
			a[i]=a[i-1];
	buildmin(1,n,1); int mark=0;
	for(int i=1;i<=n;i++)
	{
		if(id[a[i]]!=0)
		{
			int tmp=querymin(id[a[i]],i,1);
			if(tmp<a[i]) mark=1;
		}
		id[a[i]]=i;
	}
	if(mark) printf("NO\n");
	else
	{
		printf("YES\n");
		for(int i=1;i<=n;i++)
			printf("%d ",a[i]);
		printf("\n");
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/haut_ykc/article/details/81807671
今日推荐