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

题目链接:http://codeforces.com/contest/1023/problem/D                         

                             D. Array Restoration

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 00 then 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

4 3
1 0 2 3

Output

YES
1 2 2 3

Input

3 10
10 10 10

Output

YES
10 10 10 

Input

5 6
6 5 6 2 2

Output

NO

Input

3 5
0 0 0

Output

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.

题意:给定数组及q个操作,第i个操作表示把区间[li,ri]里的所有数变为i(1 <= i <= q),现在给定一个序列,其中可能含有0,0可以被任何数替代,问是否可以替换掉所有的零使得该序列可以由上述q个操作得到,并且要且数组中的任何一个数都被至少操作一次。若可能,输出YES及q次操作后的序列,否则输出NO;

解法:不防将0换成序列中离该0最近的非零数,如1 0 0 2 3就可以把两个零换成2或者1,若全部都为零,则将全部0换为q(因为最后一次操作将区间值换为q,所以可以直接把全部值换为q),因为3 2 3这种是不合法的,然后用RMQ查询两个相同数之间的最小数,若最小数<该数,则不能构造该序列,否则在保证序列至少有一个q的情况下输出序列;

AC code:

#include<iostream>
#include<cstdio>
#include<vector>
#include<bitset>
#include<stack>
#include<set>
#include<queue>
#include<map>
#include<cmath>
#include<string>
#include<cstring>
#include<ctime>
#include<fstream>
#include<cstdlib>
#include<algorithm>

using namespace std;

#define pii pair<int, int>
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a))
#define per(i,a,b) for(int i=a;i<=b;i++)
#define rep(i,a,b) for(int i=a;i>=b;i--)
#define PI 3.1415926535897932384626433832795
#define inf 0x3f3f3f3f
typedef long long ll;
const double eps=1.0e-5;
const int maxn=200000+10;

int n,q,a[maxn],b[maxn],dp[maxn][20];
bool vis[maxn];
map<int ,int>mp;

void rmq()
{
	per(i,1,n) dp[i][0]=a[i];
	for(int j=1;(1<<j)<=n;j++){
		for(int i=1;(i+(1<<j)-1)<=n;i++){
			dp[i][j]=min(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
		}
	}
}


int query(int le,int ri)
{
	if(le>ri) return 0;
	int k=0;
	while(le+(1<<(k+1))-1<=ri) k++;
	return max(dp[le][k],dp[ri-(1<<k)+1][k]);
}

int main()
{
	//freopen("C:\\Users\\MAC\\Desktop\\in.txt","r",stdin);
	scanf("%d%d",&n,&q);
	
	per(i,1,n){
		scanf("%d",&b[i]);
		a[i]=b[i];
	}
	bool flag=true;
	for(int i=1;i<=n;i++){
		if(a[i]==0){
			int f=i;
			while(a[f]==0&&f<=n) f++;
			if(f==n+1){
				fill(a+i,a+n+1,i-1==0?q:a[i-1]);
				break;
			}else{
				fill(a+i,a+f,a[f]);
				i=f;
			}
		}
	}
	
	rmq();
	mem(vis,false);
	per(i,1,n){
		if(vis[a[i]]){
			int tmp=query(mp[a[i]],i);
			if(tmp<a[i]&&tmp){
				flag=false;
				break;
			}
		}
		mp[a[i]]=i;
		vis[a[i]]=true;
	}
	
	if(!flag) puts("NO");
	else{
		int fl=inf;
		if(!vis[q]){
			per(i,1,n){
				if(b[i]==0){
					fl=i;
					break;
				}
			}
			if(fl==inf){
				puts("NO");
				return 0;
			}
			a[fl]=q;
		}
		puts("YES");
		per(i,1,n) printf("%d%c",a[i],i==n?'\n':' ');
	}
}

猜你喜欢

转载自blog.csdn.net/qq_40791842/article/details/81807705
今日推荐