Codeforces Round #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 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 31 0 2 3

output

Copy

YES1 2 2 3

input

Copy

3 1010 10 10

output

Copy

YES10 10 10 

input

Copy

5 66 5 6 2 2

output

Copy

NO

input

Copy

3 50 0 0

output

Copy

YES5 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个数的排列,然后我们随机让一些数变成了0,这些数的范围都是1-q,然后让我们求原来这个排列,他的排列求来的方法是染色,它总共有i次染色,每次

染色一个区间,让这个区间的数都变成i

思路:我们第i次染色会让这个区间的数都变成i,然后我们仔细想想,相同的数是连续的,如果不是连续的,肯定是后面再次在他们中间染了色,所以有规律

两个相同 的数中间肯定都是比它大的数不然就是错的,然后我们可以判断这一段的区间最小值是不是等于它就好,用线段树维护区间最小值,然后我们再来解决0的问题

因为他是随机让一些数变成的0,所以我们可以让0变成1-q中间的任意一个数,很容易发现,我们把0变成两边的一个数就行,就不要在意相同数区间有比他小的数

我们还要注意,因为我们是按顺序染色,所以最后我们肯定有一个数q,最后一次染色留下来的,如果没有这个数,我们要把其中一个0变成q,如果也没有那就直接判断错

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct sss
{
    int left;
    int right;
    int mn;
}a[200001*4];
int n,m;
int c[200001];
int f[200001];
void build(int cnt,int left,int right)
{
    a[cnt].left=left;
    a[cnt].right=right;
    if(left==right)
    {
        a[cnt].mn=c[left];
        return;
    }
    int mid=(left+right)/2;
    build(cnt*2,left,mid);
    build(cnt*2+1,mid+1,right);
    a[cnt].mn=min(a[cnt*2].mn,a[cnt*2+1].mn);
}
int updata(int cnt,int left,int right)
{
    if(left<=a[cnt].left&&right>=a[cnt].right)
    {
        return a[cnt].mn;
    }
    int mid=(a[cnt].left+a[cnt].right)/2;
    if(mid>=right) return updata(cnt*2,left,right);
    else if(left>mid) return updata(cnt*2+1,left,right);
    else{
        return min(updata(cnt*2,left,mid),updata(cnt*2+1,mid+1,right));
    }
}
int main()
{
    scanf("%d%d",&n,&m);
    int flag1=0,flag2=0;
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&c[i]);
        if(c[i]==m) flag1=1;
        if(c[i]==0) flag2=1;
    }
    if(flag1==0&&flag2==0)
    {
        printf("NO");
        return 0;
    }
    if(flag1==0)
    {
        for(int i=1;i<=n;i++)
        {
            if(c[i]==0)
            {
                c[i]=m;
                break;
            }
        }
    }
    int ans=1;
    for(int i=1;i<=n;i++)
    {
        if(c[i]==0) c[i]=ans;
        else ans=c[i];
    }
    build(1,1,n);
    for(int i=1;i<=n;i++)
    {
        if(f[c[i]]==0) f[c[i]]=i;
        else{
            if(updata(1,f[c[i]],i)==c[i])
            {
                f[c[i]]=i;
                 continue;
            }
            printf("NO");
            return 0;
        }
    }
    printf("YES\n");
    for(int i=1;i<=n;i++)
    {
        if(i==1) printf("%d",c[i]);
        else printf(" %d",c[i]);
    }
}

原文地址:https://www.cnblogs.com/Lis-/p/9496676.html

时间: 2024-07-30 23:10:28

Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final) D. Array Restoration的相关文章

Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final) A. Single Wildcard Pattern Matching B. Pair of Toys C. Bracket Subsequence D. Array Restoration-区间查询最值(RMQ(ST))

Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final) A. Single Wildcard Pattern Matching 题意就是匹配字符的题目,打比赛的时候没有看到只有一个" * ",然后就写挫了,被hack了,被hack的点就是判一下只有一个" * ". 1 //A 2 #include<iostream> 3 #include<cstdio&g

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

考场上只做出了ABDE C都挂了... 题解: A 题解: 模拟 判断前面一段是否相同,后面一段是否相同,长度是否够(不能有重叠) Code: 1 #include<stdio.h> 2 #include<cstring> 3 #include<cstdlib> 4 #include<algorithm> 5 #include<vector> 6 #include<map> 7 #include<set> 8 #inclu

Codeforces Round #505 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final) -B C(GCD,最长连续交替序列)

B. Weakened Common Divisor time limit per test 1.5 seconds memory limit per test 256 megabytes input standard input output standard output During the research on properties of the greatest common divisor (GCD) of a set of numbers, Ildar, a famous mat

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

A : A. Doggo Recoloring time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Panic is rising in the committee for doggo standardization — the puppies of the new brood have been born multi-colore

【Codeforces Round #505 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final) B】Weakened Common Divisor

[链接] 我是链接,点我呀:) [题意] 给你n个数对(ai,bi). 让你求一个大于1的数字x 使得对于任意的i x|a[i] 或者 x|b[i] [题解] 求出第一个数对的两个数他们有哪些质因子. 显然用这些质因子去试2..n就可以了. 看哪个可以满足 就输出对应的就可以了. (一开始我求出这个数的所有因子(TLE了)..其实没有必要...因为假设y是x的倍数..然后y满足的话,显然x也能满足要求..所以只要用质因子搞就行了. [代码] #include <bits/stdc++.h> #

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

真心简单的一场比赛 就是坑比较多(自己太蠢) A是一个水题 3分钟的时候过了 B也是一个比较简单的题 类似的套路见得多了 但是我当时可能比较困 想了一会才想出来 19分钟的时候过掉了 C同样很显然 性质不难发现 我在30分钟的时候通过了pretest 但是由于自己的愚蠢 忘记写了一句话 导致FST了... D本来是一个简单的dp题 但是我一直没往dp上想 在网络流上刚了1h之后终于换了思路 在1:45的时候通过了他 然后就时间不多了 E都没看 就去hack 成功hack了2个之后比赛就结束了 题

codeforces cf round#505(based on vk cup 2018 final) C. Plasticine zebra

构造题,把整个串想象成一个环.每次把环断开并反转的时候从切口处看进去的顺序是和刚开始从头到尾的顺序是一样的.于是每次不管如何翻转最后都要找到这个环上最大的连续子段长度 #include<bits/stdc++.h> using namespace std; string s; int main() { cin>>s; int tmp=s.size(); s=s+s; int ans=0; int len=1; for(int i=0;i<(int)s.size()-1;i++

Codeforces Round #470 (rated, Div. 2, based on VK Cup 2018 Round 1)C. Producing Snow+差分标记

题目链接:C. Producing Snow 题意:给两个数组v[N],T[N],v[i]表示第i天造的雪,T[i],表示第i天的温度,一堆雪如果<=T[i],当天就会融完,否则融化T[i],要求输出每天的融雪总量. 题解:我对T数组求个前缀和,就可以二分找到每堆雪在那一天(pos)融化,余下的要加进答案中ans[i],然后用一个an数组在a[i]+1,a[pos]-1,最后求再求一次前缀和. ans[i]再加上an[i]*t[i].每次操作二分logn,N次操作.复杂度O(nlogn) #in

Codeforces Round #472 (rated, Div. 2, based on VK Cup 2018 Round 2)

A. Tritonic Iridescence 题解:分类讨论.注意题目要求,至少有两种方案. 1 #pragma warning(disable:4996) 2 #include<cstdio> 3 #include<string> 4 #include<cstring> 5 #include<iostream> 6 #include<algorithm> 7 using namespace std; 8 9 int n, m; 10 stri