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

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");
}
上一篇:python--爬取豆瓣热门国产电视剧保存为文件


下一篇:【转帖】Alpha、Beta、RC、GA版本的区别