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. 【有趣~】SFOJ-1711 Obey的恋爱、NYOJ-739 笨蛋难题

    笨蛋难题四 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 这些日子笨蛋一直研究股票,经过调研,终于发现xxx公司股票规律,更可喜的是 笨蛋推算出这家公司每天的股价, ...

  2. WIND2003 安装Zend studio 报错

    在安装zend stutio是系统报错,貌似是签章检验没有通过,去查了一下网上的解决方式多种多样,经过验证后发现以下可以解决我的问题特做记录 机器配置是E2160+4G 异常信息是:File c:\W ...

  3. UE 使用技巧

    一.关于正则表达式的使用 删除空行: 替换 %[ ^t]++^p 为 空串 替换回车换行符:替换^p 为 空串 删除行尾空格: 替换 [ ^t]+$ 为 空串 删除行首空格: 替换 %[ ^t]+ 为 ...

  4. HDU-4035 Maze

    http://acm.hdu.edu.cn/showproblem.php?pid=4035 树上的概率dp.   Maze Time Limit: 2000/1000 MS (Java/Others ...

  5. VS2010生成Qt程序图标修改方法

    转自:http://blog.csdn.net/simeone18/article/details/7344547 1.准备ico文件,nuistcard.ico 2.在nuistcard工程目录,建 ...

  6. Python十分钟入门

    [简介] Python是一种动态解释型的编程语言.Python可以在Windows.UNIX.MAC等多种操作系统上使用,也可以在Java..NET开发平台上使用. [特点] 1. Python使用C ...

  7. 采集网页数据---Using Java

    http://www.cnblogs.com/longwu/archive/2011/12/24/2300110.html 1).学习网页数据采集,首先必不可少的是学习java的正则表达式(Regex ...

  8. 坚持自学的第二天,bootstrap初入门

    前言 昨天,初步学完了jekyll目录结构与Liquid语法的应用与认识. 日志 今天刚入门,做了一个bootstrap导航栏,但是选中状态不行,找了JS中写好的API,写法与视频中讲的有点不一样,但 ...

  9. Cloudera CDH5 部署实战指南(离线安装)

    配置软件源服务器 1.安装createreporpm -ivh deltarpm-3.5-0.5.20090913git.el6.x86_64.rpm rpm -ivh python-deltarpm ...

  10. Spring Framework 中启动 Redis 事务操作

    背景: 项目中遇到有一系列对Redis的操作,并需要保持事务处理. 环境: Spring version 4.1.8.RELEASE Redis Server 2.6.12 (64位) spring- ...