zoukankan      html  css  js  c++  java
  • codeforces 590A A. Median Smoothing(思维)

    题目链接:

    A. Median Smoothing

    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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, ..., bnobtained 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.

     
    Examples
     
    input
    4
    0 0 1 1
    output
    0
    0 0 1 1
    input
    5
    0 1 0 1 0
    output
    2
    0 0 0 0 0


    题意:

    给一个01串,每次每一位的变换都是这一位相邻的左右两个然后三人表决器的结果;问经过最少多少次变换会稳定,且输出最后的状态;


    思路:

    可以方向如果多于两个的相同的在一起,那么这些相邻的就不会变化了,这样就把这个串分成了多个子串,然后可以发现这些字串的最后状态和其左右界有关,最少的操作数等于(r-l)/2;
    具体的看代码;

    AC代码:

    //#include <bits/stdc++.h>
    #include <vector>
    #include <iostream>
    #include <queue>
    #include <cmath>
    #include <map>
    #include <cstring>
    #include <algorithm>
    #include <cstdio>
    
    using namespace std;
    #define Riep(n) for(int i=1;i<=n;i++)
    #define Riop(n) for(int i=0;i<n;i++)
    #define Rjep(n) for(int j=1;j<=n;j++)
    #define Rjop(n) for(int j=0;j<n;j++)
    #define mst(ss,b) memset(ss,b,sizeof(ss));
    typedef long long LL;
    template<class T> void read(T&num) {
        char CH; bool F=false;
        for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
        for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
        F && (num=-num);
    }
    int stk[70], tp;
    template<class T> inline void print(T p) {
        if(!p) { puts("0"); return; }
        while(p) stk[++ tp] = p%10, p/=10;
        while(tp) putchar(stk[tp--] + '0');
        putchar('
    ');
    }
    
    const LL mod=1e9+7;
    const double PI=acos(-1.0);
    const LL inf=1e14;
    const int N=5e5+15;
    int n,a[N],p[N],nex[N];
    int fun(int l,int r)
    {
        if(a[r]==a[l])
        {
            for(int i=l;i<=r;i++)a[i]=a[l];
        }
        else
        {
            for(int i=l;i<=l+(r-l)/2;i++)a[i]=a[l];
            for(int i=l+(r-l)/2+1;i<=r;i++)a[i]=a[r];
        }
            return (r-l)/2;
    }
    int main()
    {
        read(n);
        Riep(n)read(a[i]),p[i]=nex[i]=i;
        for(int i=2;i<=n;i++)
        {
            if(a[i]==a[i-1])p[i]=p[i-1],nex[p[i]]=i;
        }
        int l=nex[1],r=n;
        int ans=0;
        for(int i=nex[1]+1;i<n;i++)
        {
            if(nex[i]!=i)
            {
                r=i;
                ans=max(ans,fun(l,r));
                i=nex[i];
                l=i;
            }
        }
        r=p[n];
        ans=max(ans,fun(l,r));
        cout<<ans<<"
    ";
        Riep(n)printf("%d ",a[i]);
            return 0;
    }


  • 相关阅读:
    Jmeter4.0压测实战
    tomcat只部署一个index.html 文件
    redis 查看当前连接数
    公众号入口-H5测试要点
    windows 下启动redis && Python 操作 redis
    Jmeter4.0之beanshell引用外部jar
    算法练习之存在重复元素
    python 递归查找jpg文件并打印
    hadoop单节点安装
    windows 下获取文件夹下的文件名称
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5565510.html
Copyright © 2011-2022 走看看