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:

40 0 1 1

output:

00 0 1 1

input:

50 1 0 1 0

output:

20 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\n", ans);
        printf("%d", a[0]);
        for (i = 1; i < n; i++)
            printf(" %d", a[i]);
        printf("\n");
    }

    return 0;
}
时间: 2024-07-31 15:58:03

CodeForces 590A Median Smoothing的相关文章

Codeforces Round #327 (Div. 2) B. Rebranding C. Median Smoothing

B. Rebranding The name of one small but proud corporation consists of n lowercase English letters. The Corporation has decided to try rebranding — an active marketing strategy, that includes a set of measures to change either the brand (both for the

Codeforces Round #327 (Div. 2)C. Median Smoothing 构造

C. 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 eng

Codeforces Round #327 (Div. 1), problem: (A) Median Smoothing

http://codeforces.com/problemset/problem/590/A: 在CF时没做出来,当时直接模拟,然后就超时喽. 题意是给你一个0 1串然后首位和末位固定不变,从第二项开始到倒数第二项,当前的a[i]=(a[i-1],a[i],a[i+1])三项排序后的中间项,比如连续3项为 1 0 1,那么中间的就变为1,然后题目让你输出达到稳定状态时所需的最小步数,不能的话输出-1. 无论给你啥数列,都能达到稳态.所以不可能输出-1: 还有一开始就稳定不变,或经过几次变换而稳定

CodeForces 590A

题意:给你一个含有n个数的数组a,a的值只有0和1:有一个变换规则的到数组b,规则为: 1) b[1]=a[1],b[n]=a[n]; 2) b[i]=中位数(a[i-1],a[i],a[i-1]); b数组继续变换下去,直到得到的数组与原数组一样,输出需要经过几次变换,如果无解则输出-1: 题解:此题没有无解的情况,可以看出数组中只有出现如下两种情况可以变换 1 0 1 -->1 1 1 0 1 0 -->0 0 0 因此,只要从起点s开始往后寻找到一个终点e,使得a[e]=a[e+1]或者

Median Smoothing

#include<bits/stdc++.h> using namespace std; const int maxn=500011; const int inf=1<<27; #define LL long long #define P pair<int,int> #define pb push_back #define cl(a,b) memset(a,b,sizeof(a)); int a[maxn]; int main(){ int n; while(~scan

Codeforces Round #327 (Div2)

CodeForces 591A 题意:在距离为L的两端A,B,相向发射魔法,a(以P1的速度)-->B,A<--b(以P2的速度).假设a-->B,途中相遇,则返回到原点A<--a. 后又继续,a-->B,速度不变. b亦是如此.求第二次相遇时a的位移. 思路:因为速度不变,所以第二次相遇地点与第一次相遇地点一样. res= n/(Va+Vb)*Va 代码: 1 #include <iostream> 2 #include <cstdio> 3 #in

【OpenCV教程之九】平滑/模糊图片 Smooth / Blur Images及 彩色图转 灰度图和二值化

这一节,谈一谈如何对图像进行平滑,也可以叫做模糊.平滑图像的主要目的是减少噪声,这样采用平滑图像来降低噪声是是非常常见的预处理方法. 1.归一化滤波平滑-Homogeneous Smoothing 2.高斯滤波平滑-Gaussian Smoothing 3.中值滤波平滑-Median Smoothing 4.双边滤波平滑-Bilateral Smoothing 平滑是通过滑动窗口(内核或过滤器)扫描整个图像窗口,计算每个像素的基于核的值的和重叠的原始图像的像素的值的值来完成.这个过程在数学上称为

Codeforces 1005E1&amp;2 Median on Segments (General Case &amp; Permutations Edition)

E1 想到的O(n)做法,因为m只会出现一次,所以subarray里必须包括m.可以想像合法的subarray是m左边一个连续区间+m+m右边一个连续区间组成.然后把左区间预处理,枚举右区间就行了.(根据性质:一个subarray的median是m,那说明有0个数净比m大,或有1个数净比m大)[净大指的是2个比m小,1个比m大,算-1个比m净大] 1 #include<iostream> 2 #include<map> 3 using namespace std; 4 5 long

Codeforces #496 E1. Median on Segments (Permutations Edition)

http://codeforces.com/contest/1005/problem/E1 题目 https://blog.csdn.net/haipai1998/article/details/80985281  原博客 对样例1: 5 42 4 5 3 1 m=4,所以下标pos=2: 从pos往右遇到比m大的就cnt++,遇到小的就cnt--: 刚开始cnt=0;  mp[cnt]++ 于是从pos开始到 n:   mp[0]=1,   mp[1]=1,   mp[0]=2 ,   mp[