D. Array Restoration(树状数组+分类)

本文介绍了一个算法问题,任务是判断一个包含特定值和空白(0)的数组,是否可以通过一系列预定义的操作步骤来实现。文章提供了解决问题的思路及实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

题目大概:

给你一个数组,长度为n,0表示空白位,可以随便填。然后问你原数组是否能够进行q次操作,第  qi  次操作把一个区间中所有数变成  i  。能输出YES,并输出任意一种数组。否则NO。

思路:

1  最一般的情况是只要出现,两个相同的数中间,出现比它小的数,就不合法。这种情况我是用的树状数组维护的。

2  还有如果没有最大的q,如果有0,需要添加最大的q,否则输出NO。

3  如果还有0,直接填充成它前面的数,这样一定不会错。(这里注意把数组的0位置设置成合法的数)

代码:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=2e5+100;
const int mod=1e9+7;
const int INF=1e9+7;
int a[maxn];
int vis[maxn];
int n,m;
int c[maxn];
int lowbit(int x)
{
    return x&(-x);
}
void add(int x,int v)
{
    while(x<m+5)
    {
        c[x]+=v;
        x+=lowbit(x);
    }
}
int sum(int x)
{
    int sum=0;
    while(x>0)
    {
        sum+=c[x];
        x-=lowbit(x);
    }
    return sum;
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
    }
    int flag=0;
    for(int i=1;i<=n;i++)//树状数组维护第一种情况
    {
        if(a[i]==0)continue;

        if(vis[a[i]])
        {

            int flag1=sum(a[i]);
            if(flag1!=vis[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]);
        }

    }
    if(flag)
    {
        printf("NO\n");
    }
    else
    {
        int flag=0;
        int flag1=0;
        for(int i=1;i<=n;i++)//寻找最大值
        {
            if(a[i]==m)flag=1;
            if(a[i]==0)flag1=1;
        }
        if(!flag)
        {
            if(!flag1)
            {
                printf("NO\n");
                return 0;
            }
            else
            {
                for(int i=1;i<=n;i++)//设置最大值
                {
                    if(a[i]==0)
                    {
                        a[i]=m;
                        break;
                    }
                }
            }
        }
        a[0]=1;
        for(int i=1;i<=n;i++)//清除剩余0
        {
            if(a[i]==0)
            {
                a[i]=a[i-1];
            }
        }
        printf("YES\n");
        for(int i=1;i<=n;i++)
        {
            printf("%d ",a[i]);
        }
    }
}
/*
6 5
0 4 0 2 0 3
*/

 

### 使用 `skimage.restoration.circular_psf` 函数的方法 在 Scikit-Image (即 skimage) 库中,`restoration` 模块提供了多种用于图像恢复的技术。其中,`circular_psf` 是一种特定的点扩散函数 (Point Spread Function, PSF),通常用于模拟圆形光圈成像系统的光学模糊效果[^1]。 以下是关于如何使用 `skimage.restoration.circular_psf` 的详细说明: #### 导入必要的模块 为了调用此函数,需先安装并导入 Scikit-Image 库中的相关模块: ```python from skimage import restoration import numpy as np import matplotlib.pyplot as plt ``` #### 创建 Circular PSF 通过指定参数来生成一个圆形 PSF 数组。以下是一个简单的例子: ```python psf_radius = 5 # 圆形半径大小 shape = (100, 100) # 输出数组形状 # 调用 circular_psf 函数 circular_psf_array = restoration.circular_psf(psf_radius, shape=shape) # 可视化结果 plt.imshow(circular_psf_array, cmap='gray') plt.title('Circular PSF Array'), plt.axis('off') plt.show() ``` 上述代码片段展示了如何创建一个具有给定半径和形状的圆形 PSF,并将其可视化为灰度图。需要注意的是,`circular_psf` 参数的具体定义可能依赖于版本差异,请查阅官方文档确认最新行为[^2]。 #### 关键参数解释 - **`radius`**: 定义圆的半径长度。 - **`shape`**: 设置输出二维数组的高度和宽度尺寸,默认情况下会根据半径自动生成正方形区域。 如果未明确指定这些参数,则可能会引发错误或者返回不符合预期的结果。 --- ### 注意事项 尽管 Skimage 提供了许多强大的工具支持高级图像处理需求,但在实际应用过程中仍需注意其局限性和适用范围。例如,在某些高性能计算场景下 OpenCV 或者 GPU 加速框架可能是更好的选择。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值