Codeforces Round 1023 (#504 rated, Div. 1 + Div. 2, based on VK Cup 2018 Final) 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 n integers. Positions in it are numbered from 1 to n.

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

We could have offered you the problem about checking if some given array (consisting of n integers with values from 1 to q) 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 0.

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 n and q (1≤n,q≤2⋅10^51≤n,q≤2⋅10^5) — the number of elements of the array and the number of queries perfomed on it.

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

Output

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

Otherwise print "NO".

If some array can be obtained then print n integers on the second line — the i-th number should be equal to the i-th element of the resulting array and should have value from 1 to q. This array should be obtainable by performing exactly q 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 0 with 1 but not with 3

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

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

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

题意:

有一个长度为n的序列,你有q次操作,每一次操作可以对一段区间的值都变成i,i的值为操作的序数,即:你可以在第i次操作让l到r区间的数变成i。给你一个可能不完整的序列(a[i]等于0的时候表示a[i]我们是未知的),问是否存在这个序列是否存在一个状态能满足条件,存在输出"YES"和补全的序列,否则输出"NO"。

思路:

有两种做法:数据结构维护复杂度O(q*log(n)),模拟复杂度O(q+n)

等一下放上线段树的做法,先讲O(q+n)怎么做?

首先我们找出序列的最大值,如果最大值不等于q且没有a[i]等于0,那么它一定不合法。

之后扫一遍求出对于每个1~q的值的区间范围,如果没有q的话,把它随便赋给a[i]等于0的位置上,对于其他位上的0我们可以取它前一个数或者后一个数,这样保证不会因为改变区间范围而导致不合法的情况发生。可以在纸上画画,这里就不举例了。

我们从大到小遍历1~q的值的区间范围,设这个值为i,对于每个区间我们暴力去跑区间内是否存在小于i的值,如果没有就是合法的,否则就不合法。其中有很多操作我们是可以省略的,当枚举到a[l]>i且a[r]>i时,此时一定合法(仔细想想),直接跳出循环去跑下一个i。

示例程序

我都是把q填到最后出现0的地方,最左边和最右边出现0我填的是1。

 

 

上是线段树,下是模拟,好像差别不是很大。。。

O(q*log(n))做法

#include <bits/stdc++.h>
#define LDQ 1000000007
#define QAQ 0x3f3f3f3f
#define PI 3.14159265358979323846
using namespace std;
struct jj
{
    int l,r;
}b[200001];
int a[200000];
struct segtree
{
    int l,r,num;
}sega[800000];
void push(int pos)
{
    sega[pos-1].num=min(sega[pos*2-1].num,sega[pos*2].num);
}
void build(int l,int r,int pos)
{
    int mid;
    sega[pos-1].l=l;
    sega[pos-1].r=r;
    if(l==r)
    {
        sega[pos-1].num=a[l];
        return;
    }
    mid=(l+r)/2;
    build(l,mid,pos*2);
    build(mid+1,r,pos*2+1);
    push(pos);
}
int query(int l,int r,int pos)
{
    int ans=QAQ,mid;
    if(sega[pos-1].l>=l&&sega[pos-1].r<=r)
    {
        return sega[pos-1].num;
    }
    if(sega[pos-1].l>r||sega[pos-1].r<l)
    {
        return QAQ;
    }
    mid=(sega[pos-1].l+sega[pos-1].r)/2;
    if(l<=mid)
    {
        ans=min(ans,query(l,r,pos*2));
    }
    if(mid<r)
    {
        ans=min(ans,query(l,r,pos*2+1));
    }
    return ans;
}
int main()
{
    int n,q,i,maxx=0,flag=-1,l,r;
    scanf("%d %d",&n,&q);
    memset(b,-1,sizeof(b));
    for(i=0;n>i;i++)                                       //a[i]数组处理和区间处理
    {
        scanf("%d",&a[i]);
        if(a[i]==0)
        {
            flag=i;
        }
        if(a[i]==0)
        {
            if(i==0||n-1==i)
            {
                a[i]=1;
            }
            else
            {
                a[i]=a[i-1];
            }
        }
        if(b[a[i]].l==-1)
        {
            b[a[i]].l=b[a[i]].r=i;
        }
        else
        {
            b[a[i]].l=min(b[a[i]].l,i);
            b[a[i]].r=max(b[a[i]].r,i);
        }
        maxx=max(maxx,a[i]);
    }
    if(flag==-1&&maxx!=q)
    {
        puts("NO");
    }
    else
    {
        if(maxx!=q)                                 //填q
        {
            b[q].l=b[q].r=flag;
            a[flag]=q;
        }
        build(1,n,1);
        for(i=1;q>=i;i++)
        {
            if(b[i].l!=-1)
            {
                if(query(b[i].l,b[i].r,1)<i)                               //查询合法性
                {
                    printf("NO");
                    return 0;
                }
            }
        }
        puts("YES");
        for(i=0;n>i;i++)
        {
            if(i!=0)
            {
                printf(" ");
            }
            printf("%d",a[i]);
        }
    }
    return 0;
}

O(q+n)做法

#include <bits/stdc++.h>
#define LDQ 1000000007
#define QAQ 0x3f3f3f3f
#define PI 3.14159265358979323846
using namespace std;
struct jj
{
    int l,r;
}b[200001];
int a[200000];
int main()
{
    int n,q,i,maxx=0,flag=-1,l,r;
    scanf("%d %d",&n,&q);
    memset(b,-1,sizeof(b));
    for(i=0;n>i;i++)
    {
        scanf("%d",&a[i]);
        if(a[i]==0)                     //记录最后一个0的位置
        {
            flag=i;
        }
        maxx=max(maxx,a[i]);            //找最大值
    }
    if(flag==-1&&maxx!=q)               //没有q且q也无法填进去时,输出NO
    {
        puts("NO");
    }
    else
    {
        if(maxx!=q)                             //放入q
        {
            b[q].l=b[q].r=flag;
            a[flag]=q;
        }
        for(i=0;n>i;i++)                                 //记录区间信息
        {
            if(a[i]==0)                                   //填数
            {
                if(i==0||n-1==i)
                {
                    a[i]=1;
                }
                else
                {
                    a[i]=a[i-1];
                }
            }
            if(b[a[i]].l==-1)
            {
                b[a[i]].l=b[a[i]].r=i;
            }
            else
            {
                b[a[i]].l=min(b[a[i]].l,i);
                b[a[i]].r=max(b[a[i]].r,i);
            }
        }
        for(i=q;i>=1;i--)
        {
            if(b[i].l!=-1)                      //对于没出现过的数就不跑了
            {
                for(l=b[i].l,r=b[i].r;r>=l;)
                {
                    if(a[l]==i)
                    {
                        l++;
                    }
                    else if(a[l]<i)
                    {
                        puts("NO");
                        return 0;
                    }
                    if(a[r]==i)
                    {
                        r--;
                    }
                    else if(a[r]<i)
                    {
                        puts("NO");
                        return 0;
                    }
                    if(a[l]>i&&a[r]>i)                      //剪枝
                    {
                        break;
                    }
                }
            }
        }
        puts("YES");
        for(i=0;n>i;i++)
        {
            if(i!=0)
            {
                printf(" ");
            }
            printf("%d",a[i]);
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值