zoukankan      html  css  js  c++  java
  • Codeforces Round #327 (Div. 2) C. Median Smoothing 找规律

    C. Median Smoothing

    Time Limit: 20 Sec

    Memory Limit: 256 MB

    题目连接

    http://codeforces.com/contest/591/problem/C

    Description

    A schoolboy named Vasya loves reading books on programming and mathematics. He has recently read an encyclopedia article that described the method of median smoothing (or median filter) and its many applications in science and engineering. Vasya liked the idea of the method very much, and he decided to try it in practice.

    Applying the simplest variant of median smoothing to the sequence of numbers a1, a2, ..., an will result a new sequence b1, b2, ..., bn obtained by the following algorithm:

        b1 = a1, bn = an, that is, the first and the last number of the new sequence match the corresponding numbers of the original sequence.
        For i = 2, ..., n - 1 value bi is equal to the median of three values ai - 1, ai and ai + 1.

    The median of a set of three numbers is the number that goes on the second place, when these three numbers are written in the non-decreasing order. For example, the median of the set 5, 1, 2 is number 2, and the median of set 1, 0, 1 is equal to 1.

    In order to make the task easier, Vasya decided to apply the method to sequences consisting of zeros and ones only.

    Having made the procedure once, Vasya looked at the resulting sequence and thought: what if I apply the algorithm to it once again, and then apply it to the next result, and so on? Vasya tried a couple of examples and found out that after some number of median smoothing algorithm applications the sequence can stop changing. We say that the sequence is stable, if it does not change when the median smoothing is applied to it.

    Now Vasya wonders, whether the sequence always eventually becomes stable. He asks you to write a program that, given a sequence of zeros and ones, will determine whether it ever becomes stable. Moreover, if it ever becomes stable, then you should determine what will it look like and how many times one needs to apply the median smoothing algorithm to initial sequence in order to obtain a stable one.

    Input

    The first input line of the input contains a single integer n (3 ≤ n ≤ 500 000) — the length of the initial sequence.

    The next line contains n integers a1, a2, ..., an (ai = 0 or ai = 1), giving the initial sequence itself.

    Output

    If the sequence will never become stable, print a single number  - 1.

    Otherwise, first print a single integer — the minimum number of times one needs to apply the median smoothing algorithm to the initial sequence before it becomes is stable. In the second line print n numbers separated by a space  — the resulting sequence itself.

    Sample Input

    4
    0 0 1 1

    Sample Output

    0
    0 0 1 1

    HINT

    题意

    给你n个只含有0和1的数组,每次迭代的时候,b1=a1,bn=an,bi=(ai-1+ai+ai+2)/2

    然后问你多少次之后会稳定不变,并且把稳定不变的数组输出

    题解:

    找找规律就可以知道,我们只要找010101这种间隔的就好了

    如果长度为偶数,那么最后会变成111000这种

    如果长度为奇数,那么最后会全部变成11111或者00000这种

    代码

    #include<iostream>
    #include<stdio.h>
    using namespace std;
    #define maxn 500005
    int a[maxn];
    int b[maxn];
    int main()
    {
        int n;scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        int ans = 0;
        for(int i=1;i<=n;i++)
        {
            if(i==n)
            {
                b[i]=a[i];
                continue;
            }
            if(a[i]==a[i+1])
            {
                b[i]=(a[i-1]+a[i]+a[i+1])/2;
                continue;
            }
            int j;
            for(j=i;j<n;j++)
            {
                if(a[j]==a[j+1])
                    break;
            }
            //cout<<j<<" "<<i<<endl;
            if((j-i+1)<=2)
            {
                if(i==1)
                    b[i]=a[i];
                else
                    b[i]=(a[i-1]+a[i]+a[i+1])/2;
                continue;
            }
            if(j==n&&(j-i+1)<=3)
            {
                ans = max(ans,1);
                b[i]=(a[i-1]+a[i]+a[i+1])/2;
                continue;
            }
    
            int flag = 0;
            if((j-i+1)%2==0)
            {
                 ans = max((j-i+1)/2-1,ans);
                for(int k=i;k<i+(j-i+1)/2;k++)
                    b[k]=a[i];
                for(int k=i+(j-i+1)/2;k<=j;k++)
                    b[k]=1-a[i];
            }
            else
            {
                 ans = max((j-i+1)/2,ans);
                for(int k=i;k<=j;k++)
                    b[k]=a[i];
            }
            i=j;
        }
        b[1]=a[1];
        b[n]=a[n];
        printf("%d
    ",ans);
        for(int i=1;i<=n;i++)
            printf("%d ",b[i]);
        printf("
    ");
    }
  • 相关阅读:
    python操作csv,对比两个csv文件某列值
    监控端口和僵尸进程脚本
    openldap创建只读账号
    shell 判断文件内容是否改变
    golang调用shell命令标准输出阻塞管道
    fexpect 源码
    python pexpect 免交互自动恢复gitlab数据
    consul client agent 本地读取key value
    pip 安装三方库报超时
    微信小程序滚动tab的实现
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4910233.html
Copyright © 2011-2022 走看看