213 字
1 分钟
中值滤波
2017-08-15

题目描述#

20170813082609_40094.jpg

输入输出#

20170813082630_74802.jpg

样例输入#

3
0 1 0

样例输出#

1
0 0 0

提示#

20170813082722_12263.jpg

题解#

先打表

会发现规律然后我们按照规律模拟就可以了

具体的可以看代码

#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
int a[500005], b[500005];
int main()
{
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
        scanf("%d", &a[i]);
    a[n+1]=a[n];
    int i = 1, l, r;
    int ans = 0;
    while (i <= n)
    {
        while (i <= n && a[i] == a[i + 1])
        {
            b[i] = a[i];
            i++;
        }
        l = i;
        while (i <= n && a[i] != a[i + 1])
            i++;
        r = i;
        int len = (r - l + 1);
        if (len & 1)
            for (int j = l; j <= r; j++)
                b[j] = a[l];
        else
            for (int j = 1; j * 2 <= len; j++)
                b[l + j - 1] = a[l], b[r - j + 1] = a[r];
        ans = max(ans, (len - 1) / 2);
    }
    printf("%d\n", ans);
    for (int i = 1; i <= n; i++)
        printf("%d ", b[i]);
    //while(1);
}
中值滤波
https://www.nekomio.com/posts/98/
作者
NekoMio
发布于
2017-08-15
许可协议
CC BY-NC-SA 4.0