zoukankan      html  css  js  c++  java
  • CodeForces 590A Median Smoothing

    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.

    Sample test(s)
    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
    Note:

    In the second sample the stabilization occurs in two steps: , and the sequence 00000 is obviously stable.

    题意:给出一个长度为n且只含有0和1的序列a,求出这个序列变成一个稳定序列所需要的最少步数,每一个变换都是a[0]和a[n-1]不变,

    假设每次改变后序列变成b,那么当i=1~n-2时,b[i] =(a[i-1],a[i], a[i+1])的中位数,持续这样的变换,直到无法再改变序列的值。

    通过观察发现当一个数和它相邻的数相等时是不用被改变的,只有出现01010101或者10101010这种序列才需要被改变,这些序列最终被改变为11110000,00001111;

    #include<stdio.h>
    #include<string.h>
    #include<queue>
    #include<math.h>
    #include<stdlib.h>
    #include<algorithm>
    using namespace std;
    
    const int N=1e6+10;
    const int INF=0x3f3f3f3f;
    
    int a[N];
    
    int main ()
    {
        int n, i, j, x, p, q, ans;
    
        while (scanf("%d", &n) != EOF)
        {
            ans = -INF;
    
            for (i = 0; i < n; i++)
                scanf("%d", &a[i]);
    
            i = 1;
            while (i < n-1)
            {
                j = i;
    
                while ((a[j-1] == a[j] || a[j+1] == a[j]) && j < n-1)
                    j++; ///如果这个数和它相邻的数一样就不用被改变
    
                x = j; ///x标记出现相邻不相等序列的起始位置
    
                while ((a[j-1] != a[j] && a[j+1] != a[j]) && j < n-1)
                    j++; ///j最终标记相邻不相等序列的终止位置
    
                for (p = x, q = j-1; p <= q; p++, q--)
                {
                    a[p] = a[p-1];
                    a[q] = a[q+1];
                } ///找到一个这样的序列,将这些数改变为最终的结果
    
                ans = max(ans, (j-x+1)/2); ///由于这样的序列可能不止一个,那么我们只需要找到最长序列就行
    
                i = j;
            }
    
            printf("%d
    ", ans);
            printf("%d", a[0]);
            for (i = 1; i < n; i++)
                printf(" %d", a[i]);
            printf("
    ");
        }
    
        return 0;
    }
  • 相关阅读:
    1725最少硬币问题(DP)
    3358高数Umaru系列(9)——哈士奇(DP)
    1018骨牌铺方格(分治算法)
    3664顺序表应用7:最大子段和之分治递归法(分治算法)
    1722整数因子分解问题(分治算法)
    剑指offer JZ-11
    剑指offer JZ-10
    剑指offer JZ-9
    剑指offer JZ-8
    剑指offer JZ-7
  • 原文地址:https://www.cnblogs.com/syhandll/p/4914378.html
Copyright © 2011-2022 走看看