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

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

题目大概:

给你一个数组,长度为n,0表示空白位,可以随便填。然后问你原数组是否能够进行q次操作,第  qi  次操作把一个区间中所有数变成  i  。能输出YES,并输出任意一种数组。否则NO。

思路:

1  最一般的情况是只要出现,两个相同的数中间,出现比它小的数,就不合法。这种情况我是用的树状数组维护的。

2  还有如果没有最大的q,如果有0,需要添加最大的q,否则输出NO。

3  如果还有0,直接填充成它前面的数,这样一定不会错。(这里注意把数组的0位置设置成合法的数)

代码:

#include
#define ll long long
using namespace std;
const int maxn=2e5+100;
const int mod=1e9+7;
const int INF=1e9+7;
int a[maxn];
int vis[maxn];
int n,m;
int c[maxn];
int lowbit(int x)
{
    return x&(-x);
}
void add(int x,int v)
{
    while(x0)
    {
        sum+=c[x];
        x-=lowbit(x);
    }
    return sum;
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
    }
    int flag=0;
    for(int i=1;i<=n;i++)//树状数组维护第一种情况
    {
        if(a[i]==0)continue;

        if(vis[a[i]])
        {

            int flag1=sum(a[i]);
            if(flag1!=vis[a[i]])
            {
                flag=1;
                break;
            }
            add(a[i],1);
            vis[a[i]]=sum(a[i]);
        }
        else
        {
           add(a[i],1);
           vis[a[i]]=sum(a[i]);
        }

    }
    if(flag)
    {
        printf("NO\n");
    }
    else
    {
        int flag=0;
        int flag1=0;
        for(int i=1;i<=n;i++)//寻找最大值
        {
            if(a[i]==m)flag=1;
            if(a[i]==0)flag1=1;
        }
        if(!flag)
        {
            if(!flag1)
            {
                printf("NO\n");
                return 0;
            }
            else
            {
                for(int i=1;i<=n;i++)//设置最大值
                {
                    if(a[i]==0)
                    {
                        a[i]=m;
                        break;
                    }
                }
            }
        }
        a[0]=1;
        for(int i=1;i<=n;i++)//清除剩余0
        {
            if(a[i]==0)
            {
                a[i]=a[i-1];
            }
        }
        printf("YES\n");
        for(int i=1;i<=n;i++)
        {
            printf("%d ",a[i]);
        }
    }
}
/*
6 5
0 4 0 2 0 3
*/

 

你可能感兴趣的:(Codeforces,树状数组)