D. Array Restoration(树状数组+思维)

Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final)

http://codeforces.com/contest/1023/problem/D

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 a

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

) — the number of elements of the array and the number of queries perfomed on it.

The second line contains n

integer numbers a1,a2,…,an (0≤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) 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

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 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个初始数字的序列。

问能不能通过一系列操作将0转化后,变成合法的串。一系列操作就是第i次可以将某段闭区间都变成i。一共要操作q次(必须操作q次)

思路:1.该串必须要有最大的数字q。否则就是不合法的。如果原串没有0,并且没有最大值,说明就是不合法的(坑点).

2.原串两个相同的数字之间所有的数字都不能小于它,因为小于它的先刷,而它是后来刷的(连续一段).这种情况我用的树状数组巧妙判断。

3.剩下的0的位置,如果没有q就找一个填成q,剩下的都跟前边的一样就好了。这样一定是可以的。

4.LHJ大佬告诉我的思路。

代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn=200000+100;
typedef long long LL;
int a[maxn];
int C[maxn];
int vis[maxn];
int n,q;
void add(int i,int x){
    for(;i<q+10;i+=(i&(-i))){
        C[i]+=x;
    }
}
LL sum(int i){
    LL he=0;
    while(i>0){
        he+=C[i];
        i-=(i&(-i));
    }
    return he;
}
int main()
{
    int flag;
    scanf("%d%d",&n,&q);
    memset(vis,0,sizeof(vis));flag=0;
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
    }
    for(int i=1;i<=n;i++){
    if(!a[i])
        continue;
        if(vis[a[i]]){///之前出现过了
            if(vis[a[i]]!=sum(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]);///到当前位置比a[i]小的有几个
        }
    }
    if(flag)
    {
        printf("NO\n");
        return 0;
    }
    else{
     ///cout<<"&&&"<<endl;
    int maxx=0;///标志一下最大的有没有出现过
    int zero=0;///标记是否有0.如果没有最大的值,并且没有0,这样也是不合法的
    for(int i=1;i<=n;i++){
        if(a[i]==q)
            maxx=1;
        else if(a[i]==0)
            zero=1;
    }
    if(maxx==0 &&zero==0)
        puts("NO\n");
    else{
    int haha=0;
    a[0]=1;
    for(int i=1;i<=n;i++){
        if(a[i]==0){
            if(haha==0){
            haha=1;
            if(maxx==0)
                a[i]=q;
            else
                a[i]=a[i-1];
            }
            else
                a[i]=a[i-1];
        }
    }
    puts("YES\n");
    for(int i=1;i<=n;i++){
        printf("%d ",a[i]);
    }
    }
    }


}

/*
107854 47111 114588 21671 36392 156894
128126 74098 198201 143279 15789 66256
49169 197800 162554 199904 44769 19997
155103 55600 46905 73175 67434 176529
118427 51103 25256 124011 172317 33196
57612 12935 142852 161416 138272 45479
108017 95397 10967 119944 13672 64365
85687 30296 142339 145577 123946 42965
 167809 23286 80411 160750 150886 137216
  130611 90546 63460 108786 73059 151800
   57321 143182 136259 131707 192505 167966
    121878 70796 108222 193143 170094 33571
    160404 131574 80241 196312 57527 1...


*/

猜你喜欢

转载自blog.csdn.net/xianpingping/article/details/81808854