ZCMU—1660

来源:互联网 发布:joker和uu知乎 编辑:程序博客网 时间:2024/04/29 09:32

1660: = ̄ω ̄= 题目真的是英文

Time Limit: 1 Sec  Memory Limit: 128 MB
[Submit][Status][Web Board]

Description

We have a sequence of numbers a1, a2, ..., an will result a new sequence b1, b2, ..., bn obtained by the following algorithm:

  • b1 = a1bn = 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 - 1ai 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, We decided to apply the method to sequences consisting of zeros and ones only.

Having made the procedure once, We 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? We tried a couple of examples and found out that after some number of median  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 We 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 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
5
0 1 0 1 0

Sample Output

0
0 0 1 1
2
0 0 0 0 0

HINT

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

【分析】

题意:给一个串,可以进行操作,每次操作头和尾每次变换保持不变,中间的a[i]变成a[i-1],a[i],a[i+1]的中位数,并且串中只有01.
对于01串
0 0 0中位数是0
0 0 1中位数是0
0 1 1中位数是1
1 1 1中位数是1
所以可以发现,如果某个串中有相邻一样的串,那么这个串是不会变的,所以我们需要处理的就是010101这种间隔的串,中间所有相邻的串都可以跳过,并且对其中某一个串进行变换,最终的结果就是使这个串前一半跟头一样,后一半跟尾一样。
答案就是找其中那个需要转化次数最多的串,也就是长度最长的01间隔串,答案就是这个串的一半。
所以并不是一开始认为的是模拟题...根本不用考虑时间复杂度...

【代码】
#include <stdio.h>  int a[510000], f[510000];  int main()  {      int res, len, i, j, n, mid, k;      while (~scanf("%d", &n)){     for(int i=0;i<n;i++) scanf("%d",&a[i]);      int ans=0;      for(int i=0;i<n;i++)      {          f[i]=a[i];int j;          for(j=i;j<n-1;j++)            if(a[j]==a[j+1]) break;          int len=j-i-1;          mid=(j+i)/2;          for(int k=i;k<=j;k++)              if(k<=mid) f[k]=a[i];              else f[k]=a[j];          if(len>ans) ans=len;          i=j;      }      printf("%d\n",/2+ans%2);      for(int i=0;i<n-1;i++) printf("%d ",f[i]);  printf("%d\n",f[n-1]);}return 0;}  


0 0