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.

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.

比较恶心

很容易注意到对于一段连续的00或者11,他们下一步也一定是00或者11。

而对于每个ai,它的下一步取值跟ai-1,ai,ai+1有关,那么在00/11左边的和右边的是互不影响的。

于是我们可以认为每个00/11中间画一条线,把他们分开,像这样 0|0

于是序列被左右端点和这些我们画的“线”分成很多部分,答案就是这些区间的答案最大值

而这些区间又都没有连续的00或者11,那一定是0101010这样的

所以一段区间的答案只跟左右端点的值和区间长度有关

具体就很容易yy了

 #include<set>
#include<map>
#include<cmath>
#include<ctime>
#include<deque>
#include<queue>
#include<bitset>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define LL long long
#define inf 0x7fffffff
#define pa pair<int,int>
#define pi 3.1415926535897932384626433832795028841971
using namespace std;
inline LL read()
{
LL x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,ans,l,a[],mrk;
inline void jud(int l,int r)
{
if (l==r)return;
if(a[l]==a[r])
{
for (int i=l;i<=r;i++)a[i]=a[l];
ans=max(ans,(r-l)/);
return;
}else if (r-l>)
{
for (int i=l;i<=l+(r-l-)/;i++)a[i]=a[l];
for (int i=r-(r-l-)/;i<=r;i++)a[i]=a[r];
ans=max(ans,(r-l+)/-);
}
}
int main()
{
n=read();for (int i=;i<=n;i++)a[i]=read();mrk=-;
l=;
for(int i=;i<=n;i++)
{
if (a[i]==mrk){l=i;continue;}
mrk=-;
if (i==n)jud(l,i);
else if (a[i]==a[i-]&&i!=)jud(l,i-),l=i,mrk=a[i]; }
printf("%d\n",ans);
for (int i=;i<=n;i++)printf("%d ",a[i]);
}

cf590A

cf590A Median Smoothing的更多相关文章

  1. 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. T ...

  2. codeforces 590A A. Median Smoothing(思维)

    题目链接: A. Median Smoothing time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  3. Codeforces Round #327 (Div. 2) C. Median Smoothing 找规律

    C. Median Smoothing Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/591/p ...

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

    C. Median Smoothing   A schoolboy named Vasya loves reading books on programming and mathematics. He ...

  5. 【22.70%】【codeforces 591C】 Median Smoothing

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  6. Codeforces 590 A:Median Smoothing

    A. Median Smoothing time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  7. CodeForces 590A Median Smoothing

    构造题. 答案可以o(n)构造出来.首先要发现规律.只有01交替的串才可能变化,变化规律如下: 1开头,长度为偶数(0结尾):变(len-2)/2次 变完后 前半1 后半01开头,长度为奇数(1结尾) ...

  8. codeforces590a//Median Smoothing//Codeforces Round #327 (Div. 1)

    题意:一个数组,一次操作为:除首尾不变,其它的=它与前后数字的中位数,这样对数组重复几次后数组会稳定不变.问要操作几次,及最后的稳定数组. 挺难的题,参考了别人的代码和思路.总的来说就是找01010, ...

  9. ACM学习历程—CodeForces 590A Median Smoothing(分类讨论 && 数学)

    题目链接:http://codeforces.com/problemset/problem/590/A 题目大意是给一个串,头和尾每次变换保持不变. 中间的a[i]变成a[i-1],a[i],a[i+ ...

随机推荐

  1. spring 中StoredProcedure的用法--转载

    StoredProcedure是一个抽象类,必须写一个子类来继承它,这个类是用来简化JDBCTemplate执行存储过程操作的. 首先我们写一个实现类: package com.huaye.frame ...

  2. searchbar的使用介绍

    searchBar的使用介绍 首先如何创建一个SearchBar实例: self.searchBar = [[UISearchBar alloc] initWithFrame: CGRectMake( ...

  3. 连接管理VMware SphereESXi

    连接管理VMware SphereESXi 1. 准备 下载VMware-viclient-all-5.5.0-1993072,并按照提示安装 2. 使用VMware Sphere Client链接事 ...

  4. MySQL存储过程详解 mysql 存储过程(二)

    mysql存储过程详解 1.      存储过程简介 我们常用的操作数据库语言SQL语句在执行的时候需要要先编译,然后执行,而存储过程(Stored Procedure)是一组为了完成特定功能的SQL ...

  5. IIS 服务器 支持.apk文件的下载

    IIS服务器不能下载.apk文件的解决办法:既然.apk无法下载是因为没有MIME,那么添加一个MIME类型就可以了 随着智能手机的普及,越来越多的人使用手机上网,很多网站也应手机上网的需要推出了网站 ...

  6. IOS开发常用的linux命令

    pwd 在Linux层次结构中,用户可以在被授权的任意目录下利用mkdir命令创建新目录,也可以利用cd命令从一个目录转换到另一个目录.然而,没有提示符来告知用户目前处于哪一个目录中.想要知道当前所处 ...

  7. 自定义控件出现“loaded nib but the view outlet was not set”

    我出现这个错误是因为我的自定义控件的名字和项目中一个控制器的名字很像 控制器 DDGuessYourLikeViewController 自定义控件 DDGuessYourLikeView 默认的, ...

  8. /root/.bashrc与/etc/profile的异同

    要搞清bashrc与profile的区别,首先要弄明白什么是交互式shell和非交互式shell,什么是loginshell 和non-loginshell. 交互式模式就是shell等待你的输入,并 ...

  9. javaScript 手写图片轮播

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  10. centos U盘安装

    1.版本 LiveCD 和 LiveDVD 是可以直接进入运行系统,类似win PE, 进入系统后有一个图标 install - HHD(从硬盘安装). netinstall 是用于网络安装和系统救援 ...