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

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 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 的序列,可以对这 n 个序列进行 q 次操作,第 i 次操作选择一个区间 [li,ri][li,ri],将这个区间内的所有数字用 i 替换,每个位置上的数字至少被一个区间覆盖。现在题目给出最终的序列,问是否存在合法的 q 次操作,能够得到给出的序列,求 q 次操作前的序列。原序列中的元素必须在(1 —— q)

线段树解法:

我们利用线段树维护区间的最小值,用线段树查询一个数字出现的两个端点之间是否有比它小的数,如果有输出no,如果每个数都没有输出yes。

对于题目要求的原序列元素必须在( 1 —— q )。我们在输入的时候统计0的数量与最大值。在建树前,进行特判并且按要求生成新的序列即可。

#pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
#define clr(a) memset(a,0,sizeof(a))
#define line cout<<"-----------------"<<endl;

typedef long long ll;
const int maxn = 2e5+10;
const int MAXN = 1e6+10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9+7;
const int N = 1010;

int n, q, num;
int a[MAXN];
struct Node{
	int l, r;
}p[MAXN];
struct node{
	int l, r, minn;
	int mid(){
		return (l + r) >> 1;
	}
}tree[MAXN] ;
void pushup(int x){
	tree[x].minn = min(tree[x<<1].minn, tree[x<<1|1].minn);
}
void build(int x, int l, int r){
	tree[x].l = l;
	tree[x].r = r;
	if(l == r){
		tree[x].minn = a[l];
		return ;
	}
	int mid = tree[x].mid();
	build(x<<1, l , mid);
	build(x<<1|1, mid+1, r);
	pushup(x);
}
int querymin(int x, int l, int r){
	if(tree[x].l == l && tree[x].r == r)
		return tree[x].minn;
	int mid = tree[x].mid();
	if(r <= mid) return querymin(x<<1, l, r);
	else if(l > mid) return querymin(x<<1|1, l, r);
	else{
		return min(querymin(x<<1, l, mid), querymin(x<<1|1, mid+1, r));
	}
}
int main(){
	while(scanf("%d%d", &n, &q) != EOF){
		clr(tree);clr(p);clr(a);
		num = 1;
		int cnt = 0;
		int maxx = -1;
		for(int i = 1; i <= n; i++) {
			scanf("%d", &a[i]);
			p[a[i]].l = min(i,p[a[i]].l);
			if(a[i] == 0) cnt ++;
			if(maxx < a[i]) maxx = a[i];
		}
		for(int i = 1; i <= n; i++){
			if(p[a[i]].l == 0) p[a[i]].l = p[a[i]].r = i;
			else p[a[i]].r = i;
		}
		if(cnt == n){
			for(int i = 1; i <= n; i++) a[i] = q;
		}
		if(maxx != q && cnt == 0){
			printf("NO\n");continue;
		}
		else if(maxx != q && cnt != 0){
			for(int i = 1; i <= n; i++){
				if(a[i] == 0){
					a[i] = q;break;
				}
			}
		}
		for(int i = 1; i <= n; i++){
			if(a[i] == 0) a[i] = a[i-1];
		}
		for(int i = n; i >= 1; i--){
			if(a[i] == 0) a[i] = a[i+1];
		}
		build(1, 1, n);
		int flag = 1;
		for(int i = 0; i <= q ; i++){
            if(p[i].l == 0 && p[i].r == 0) continue;
			if(querymin(1,p[i].l,p[i].r) < i){
				flag = 0; break;
			}
		}
		if(flag){
			printf("YES\n");
			for(int i = 1; i <= n; i++){
                printf("%d ", a[i]);
			}
			printf("\n");
		}
		else printf("NO\n");
	}
	return 0;
}

 

最哥的解法

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
 
const int MAXN = 1e6+10;
 
 
int a[MAXN], l[MAXN], r[MAXN], b[MAXN],ad[MAXN], de[MAXN], del[MAXN];
priority_queue<int, vector<int>, less<int> > que;
 
int n, p, ma, zero;
int main() {
    scanf("%d%d", &n, &p);
    for(int i=1;i<n+1;i++){
        scanf("%d",&a[i]);
        if(l[a[i]] == 0)
            l[a[i]] = i;
        ma = max(ma, a[i]);
        r[a[i]] = i;
        zero += a[i] == 0 ? 1 : 0;
    }
    for(int i=1;i<p+1;i++)
        ad[l[i]] = de[r[i]] = i;
    for(int i=1;i<n+1;i++){
        if(ad[i] > 0)
            que.push(ad[i]);
        while(!que.empty() && del[que.top()])
            que.pop();
        b[i] = que.empty() ? 0 : que.top();
        if(de[i] > 0)
            del[de[i]] = 1;
    }
    bool flag = (ma == p || zero);
    for(int i=1;i<n+1;i++){
        if((a[i] != b[i]) && (a[i] != 0)) {
            flag = false;
            break;
        }
    }
    if(!flag) puts("NO");
    else {
        puts("YES");
        flag = ma != p;
        for(int i=1;i<n+1;i++){
            if(a[i] == 0) {
                if(flag) {
                    a[i] = p;
                    printf("%d ",a[i]);
                    flag = false;
                }
                else {
                    if(i == 1) {
                        int j = i;
                        while(j <= n && a[j] == 0)
                            ++j;
                        for(int k=i;k<j;k++){
                            a[i] = a[j];
                            printf("%d ",a[i]);
                        }
                        i = j - 1;
                    }
                    else {
                        a[i] = a[i-1];
                        printf("%d ",a[i]);
                    }
                }
            }
            else    printf("%d ",a[i]);
        }
    }
    printf("\n");
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值