time limit per test2 seconds

memory limit per test256 megabytes

inputstandard input

outputstandard 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, …, 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.

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

Note

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

【题目链接】:http://codeforces.com/contest/591/problem/C

【题解】



按照题目的要求;

只要出现了连续的1和连续的0(连续的个数大于等于2);

则这些连续的数字肯定不会再发生变化了;

然后再考虑那些01交替出现的情况;

比如0000010101010111111

中间的10101010是交替出现的,这些都会发生变化;

显然变一次会变成

01010101即全部取反;

则最左边的0和最右边的1会和原本这个01串两边的连续串“融合”在一起;

这样就缩小了规模

变成了

0000001010101111111

然后会发生变化的就变成了

101010

再变

010101(取反)

则最左边和最优边分别又有一个0和1和边界“融合”了;

就这样i-j/2次之后显然就不会再发生变化了;

而最左边和最右边会相应的变成这个01串原本的左边连续串和右边连续串的值;

然后在所有的01串中取相应的(i-j)/2的max值即可,即为答案;



【完整代码】

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <set>
#include <map>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>
#include <string>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long using namespace std; const int MAXN = 509999;
const int dx[5] = {0,1,-1,0,0};
const int dy[5] = {0,0,0,-1,1};
const double pi = acos(-1.0); int n;
int a[MAXN]; void rel(LL &r)
{
r = 0;
char t = getchar();
while (!isdigit(t) && t!='-') t = getchar();
LL sign = 1;
if (t == '-')sign = -1;
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
r = r*sign;
} void rei(int &r)
{
r = 0;
char t = getchar();
while (!isdigit(t)&&t!='-') t = getchar();
int sign = 1;
if (t == '-')sign = -1;
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
r = r*sign;
} int main()
{
//freopen("F:\\rush.txt","r",stdin);
rei(n);
for (int i = 1;i <= n;i++)
rei(a[i]);
int m = 0;
for (int i = 1;i <=n-1;i++)
if (a[i]!=a[i+1])
{
int j = i+1;
while (j+1<=n && a[j+1]!=a[j]) j++;
m = max((j-i)/2,m);
int l = i+1,r = j-1;
while (l <= r)
{
a[l] = a[i];
a[r] = a[j];
l++;r--;
}
i = j;
}
cout << m<<endl;
for (int i = 1;i <= n;i++)
printf("%d ",a[i]);
return 0;
}

【22.70%】【codeforces 591C】 Median Smoothing的更多相关文章

  1. 【 BowWow and the Timetable CodeForces - 1204A 】【思维】

    题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...

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

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

  3. 【55.70%】【codeforces 557A】Ilya and Diplomas

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  4. 【32.22%】【codeforces 602B】Approximating a Constant Range

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

  5. 【22.73%】【codeforces 606D】Lazy Student

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

  6. 【codeforces 766E】Mahmoud and a xor trip

    [题目链接]:http://codeforces.com/contest/766/problem/E [题意] 定义树上任意两点之间的距离为这条简单路径上经过的点; 那些点上的权值的所有异或; 求任意 ...

  7. 【codeforces 733F】Drivers Dissatisfaction

    [题目链接]:http://codeforces.com/problemset/problem/733/F [题意] 给你n个点m条边; 让你从中选出n-1条边; 形成一个生成树; (即让n个点都联通 ...

  8. 【codeforces 799D】Field expansion

    [题目链接]:http://codeforces.com/contest/799/problem/D [题意] 给你长方形的两条边h,w; 你每次可以从n个数字中选出一个数字x; 然后把h或w乘上x; ...

  9. 【codeforces 22C】 System Administrator

    [题目链接]:http://codeforces.com/problemset/problem/22/C [题意] 给你n个点; 要求你构造一个含m条边的无向图; 使得任意两点之间都联通; 同时,要求 ...

随机推荐

  1. POJ 2284 That Nice Euler Circuit (LA 3263 HDU 1665)

    http://poj.org/problem?id=2284 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&a ...

  2. 毕业两年做到测试经理的经历总结- 各个端的自动化,性能测试结合项目具体场景实战,分析客户反馈的Bug

    前言 最近看到行业的前辈都分享一些过往的经历来指导我们这些测试人员,我很尊敬我们的行业前辈,没有他们在前面铺路,如今我们这帮年轻的测试人估计还在碰壁或摸着石头过河,结合前辈们的经验,作为年轻的测试人也 ...

  3. 介绍linux设备驱动编程

    目前,Linux软件工程师大致可分为两个层次: (1)Linux应用软件工程师(Application Software Engineer):       主要利用C库函数和Linux API进行应用 ...

  4. Altium Designer如何移动选中的所有对象

    用shift去选中, 快捷键M S移动整体

  5. UVA 11859 - Division Game

    看题传送门 题目大意 有一个n * m的矩阵,每个元素均为2~10000之间的正整数,两个游戏者轮流操作.每次可选一行中的1个或者多个大于1的整数把它们中的每个数都变成它的某个真因子,比如12可以变成 ...

  6. Nginx系列(二)--模块化

    高度模块化的设计设Nginx架构的基础. 在Nginx中,除了少量的核心代码,其它一切皆为模块.模块化设计具有下面特点: 1.高度抽象的模块接口 2.灵活性 3.配置模块的设计使Nginx提供了高可配 ...

  7. swift项目第七天:构建访客界面以及监听按钮点击

    一:访客界面效果如图 二:xib封装访客视图的view 1:业务逻辑分析:1:由于用户未登录时要显示访客视图,要先进行判断用户是否登录,未登录则显示访客视图,登录则显示正常的登陆界面,由于要在四个子控 ...

  8. [WPF自定义控件库]排序、筛选以及高亮

    1. 如何让列表的内容更容易查找 假设有这么一个列表(数据源在本地),由于内容太多,要查找到其中某个想要的数据会比较困难.要优化这个列表,无非就是排序.筛选和高亮. 改造过的结果如上. 2. 排序 在 ...

  9. SetForegroundWindow

    SetForegroundWindow 函数功能:该函数将创建指定窗口的线程设置到前台,并且激活该窗口.键盘输入转向该窗口,并为用户改各种可视的记号.系统给创建前台窗口的线程分配的权限稍高于其他线程. ...

  10. Envelope

    IEnvelope Interface Provides access to methods and properties of envelopes. Note: the IEnvelope inte ...