C. Median Smoothing

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/591/problem/C

Description

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.

Sample Input

4
0 0 1 1

Sample Output

0
0 0 1 1

HINT

题意

给你n个只含有0和1的数组,每次迭代的时候,b1=a1,bn=an,bi=(ai-1+ai+ai+2)/2

然后问你多少次之后会稳定不变,并且把稳定不变的数组输出

题解:

找找规律就可以知道,我们只要找010101这种间隔的就好了

如果长度为偶数,那么最后会变成111000这种

如果长度为奇数,那么最后会全部变成11111或者00000这种

代码

#include<iostream>
#include<stdio.h>
using namespace std;
#define maxn 500005
int a[maxn];
int b[maxn];
int main()
{
int n;scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
int ans = ;
for(int i=;i<=n;i++)
{
if(i==n)
{
b[i]=a[i];
continue;
}
if(a[i]==a[i+])
{
b[i]=(a[i-]+a[i]+a[i+])/;
continue;
}
int j;
for(j=i;j<n;j++)
{
if(a[j]==a[j+])
break;
}
//cout<<j<<" "<<i<<endl;
if((j-i+)<=)
{
if(i==)
b[i]=a[i];
else
b[i]=(a[i-]+a[i]+a[i+])/;
continue;
}
if(j==n&&(j-i+)<=)
{
ans = max(ans,);
b[i]=(a[i-]+a[i]+a[i+])/;
continue;
} int flag = ;
if((j-i+)%==)
{
ans = max((j-i+)/-,ans);
for(int k=i;k<i+(j-i+)/;k++)
b[k]=a[i];
for(int k=i+(j-i+)/;k<=j;k++)
b[k]=-a[i];
}
else
{
ans = max((j-i+)/,ans);
for(int k=i;k<=j;k++)
b[k]=a[i];
}
i=j;
}
b[]=a[];
b[n]=a[n];
printf("%d\n",ans);
for(int i=;i<=n;i++)
printf("%d ",b[i]);
printf("\n");
}

Codeforces Round #327 (Div. 2) C. Median Smoothing 找规律的更多相关文章

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

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

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

    分析: 三个01组合只有八种情况: 000 s001 s010 0011 s100 s101 1110 s111 s 可以看出只有010,101是不稳定的.其他都是稳定的,且连续地出现了1或0,标记为 ...

  3. Codeforces Round #347 (Div. 2) C. International Olympiad 找规律

    题目链接: http://codeforces.com/contest/664/problem/C 题解: 这题最关键的规律在于一位的有1989-1998(9-8),两位的有1999-2098(99- ...

  4. 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 ...

  5. Codeforces Round #327 (Div. 2)

    题目传送门 水 A - Wizards' Duel 题目都没看清就写了,1e-4精度WA了一次... /************************************************ ...

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

    http://codeforces.com/problemset/problem/590/A: 在CF时没做出来,当时直接模拟,然后就超时喽. 题意是给你一个0 1串然后首位和末位固定不变,从第二项开 ...

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

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

  8. Codeforces Round #327 (Div. 2) A. Wizards' Duel 水题

    A. Wizards' Duel Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/591/prob ...

  9. Codeforces Round #327 (Div. 2) E. Three States BFS

    E. Three States Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/591/probl ...

随机推荐

  1. linux下打开chm文件的方法

    windows中,通常情况下,chm文件可以使用系统自带的程序打开,但是linux就没有那么幸运了,那么,如何在linux下打开chm 文件呢?有小编来为您介绍介绍,本篇,小编以ubuntu环境为例 ...

  2. sql给数据库加锁问题

    加锁是在操作数据时进行了,不能事后加锁. 例: begin   tran           insert   表   with(TABLOCKX)     --加锁           (字段列表) ...

  3. ECshop 二次开发模板教程1

    本教程适用于了解 ECshop 和 ECshop模板DIY 以及它们的日常使用,在查看前阁下需要至少会使用一种编辑器(exp:Dreamweaver, editplus, emacs, vi, ee  ...

  4. C# WinForm中 让控件全屏显示的实现代码

    夏荣全 ( lyout(at)163.com )原文 C#中让控件全屏显示的实现代码(WinForm) 有时候需要让窗口中某一块的内容全屏显示,比如视频播放.地图等等.经过摸索,暂时发现两种可行方法, ...

  5. 两个数组a[N],b[N],其中A[N]的各个元素值已知,现给b[i]赋值,b[i] = a[0]*a[1]*a[2]…*a[N-1]/a[i];

    转自:http://blog.csdn.net/shandianling/article/details/8785269 问题描述:两个数组a[N],b[N],其中A[N]的各个元素值已知,现给b[i ...

  6. login:用户登陆的意思

    login:用户登陆的意思 在思科的设备上有两种登录方式: 一种是本地方式,使用console口: 一种是远程方式(或者叫做网络方式):使用的是telnet等 1.默认情况下,思科的远程访问是禁止的. ...

  7. 转储oracle的redo文件

    1.确定当前使用的redo文件 SQL> select member from v$logfile where group# = ( select group# from v$log where ...

  8. CSS计算样式的获取

    一般来说我们获取CSS的样式的时候会优先采用Elment.style.cssName 这种方法,这种方法类似于对象设置get,set属性获取,例如Elment.style.cssName是获取,Elm ...

  9. 题目1069:查找学生信息(STL的map简单应用)

    题目描述: 输入N个学生的信息,然后进行查询. 输入:                        输入的第一行为N,即学生的个数(N<=1000) 接下来的N行包括N个学生的信息,信息格式如 ...

  10. Classes and Objects :类和对象(2)

    类内部可以有另一个类,也就是内部类,如果带static则为静态内部类静态内部类当然不能直接访问实例变量内部类修饰符可以有四种,而外部类只有两种 内部类的意义:这个内部类只适用于这个外部类因为外部类的某 ...