codeforces 1023 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 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 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.

#include<bits/stdc++.h>
using namespace std;
const int MAX = 2e5 + 7;
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r
int a[MAX << 2];
int cnt[MAX];
void pushup(int rt){
    a[rt] = a[rt << 1] + a[rt << 1 | 1];
}
int lz[MAX << 2], x[MAX];
void pushdown(int rt, int l, int r){
    if(lz[rt]){
        int mid = (l + r) >> 1;
        lz[rt << 1] = lz[rt << 1 | 1] = 1;
        a[rt << 1] = mid - l + 1;
        a[rt << 1 | 1] = r - mid;
    }
}
void update(int rt, int l, int r, int x, int y){
    if(x <= l && r <= y){
        a[rt] = r - l + 1;
        lz[rt] = 1;
        return;
    }
    pushdown(rt, l, r);
    int mid = (l + r) >> 1;
    if(x <= mid) update(lson, x, y);
    if(mid < y) update(rson, x, y);
    pushup(rt);
}
int query(int rt, int l, int r, int x, int y){
    if(x <= l && r <= y) return a[rt];
    pushdown(rt, l, r);
    int mid = (l + r) >> 1;
    int ans = 0;
    if(x <= mid) ans += query(lson, x, y);
    if(mid < y) ans += query(rson, x, y);
    return ans;
}
vector <int> v[MAX];
int main(){
    int n, m;
    memset(a, 0, sizeof a);
    scanf("%d%d", &n, &m);
    int pos = -1, flag = 0;
    for(int i = 0; i < n; i++){
        scanf("%d", &x[i]);
        if(x[i] == 0 && pos == -1) pos = i;
        if(x[i] == m) flag = m;
    }
    if(!flag && pos != -1) x[pos] = m;
    for(int i = 1; i < n; i++) if(x[i] == 0) x[i] = x[i - 1];
    for(int i = n - 2; i >= 0; i--) if(x[i] == 0) x[i] = x[i + 1];
    for(int i = 0; i < n; i++){
        cnt[x[i]]++;
        if(v[x[i]].size() == 0) v[x[i]].push_back(i + 1);
    }
    for(int i = n - 1; i >= 0; i--)
        if(v[x[i]].size() == 1) v[x[i]].push_back(i + 1);
    if(v[m].size() == 0){
        puts("NO");
        return 0;
    }
    flag = 0;

    for(int i = m; i >= 1; i--){
        if(v[i].size() == 0) continue;
        if(query(1, 1, n, v[i][0], v[i][1]) + cnt[i] != v[i][1] - v[i][0] + 1){
            flag = 1;
            break;
        }
        else update(1, 1, n, v[i][0], v[i][1]);
    }
    if(flag) puts("NO");
    else{
        puts("YES");
        for(int i = 0; i < n; i++) printf("%d ", x[i]);
    }
    return 0;
}

 

CodeForces - 616D是一个关于找到一个序列中最长的第k好子段的起始位置和结束位置的问题。给定一个长度为n的序列和一个整数k,需要找到一个子段,该子段中不超过k个不同的数字。题目要求输出这个序列最长的第k好子段的起始位置和终止位置。 解决这个问题的方法有两种。第一种方法是使用尺取算法,通过维护一个滑动窗口来记录\[l,r\]中不同数的个数。每次如果这个数小于k,就将r向右移动一位;如果已经大于k,则将l向右移动一位,直到个数不大于k。每次更新完r之后,判断r-l+1是否比已有答案更优来更新答案。这种方法的时间复杂度为O(n)。 第二种方法是使用枚举r和双指针的方法。通过维护一个最小的l,满足\[l,r\]最多只有k种数。使用一个map来判断数的种类。遍历序列,如果当前数字在map中不存在,则将种类数sum加一;如果sum大于k,则将l向右移动一位,直到sum不大于k。每次更新完r之后,判断i-l+1是否大于等于y-x+1来更新答案。这种方法的时间复杂度为O(n)。 以上是两种解决CodeForces - 616D问题的方法。具体的代码实现可以参考引用\[1\]和引用\[2\]中的代码。 #### 引用[.reference_title] - *1* [CodeForces 616 D. Longest k-Good Segment(尺取)](https://blog.csdn.net/V5ZSQ/article/details/50750827)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Codeforces616 D. Longest k-Good Segment(双指针+map)](https://blog.csdn.net/weixin_44178736/article/details/114328999)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值