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 = 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, 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
Sample Output
HINT
In the second sample the stabilization occurs in two steps: , and the sequence 00000 is obviously stable.
【分析】
对于01串
0 0 0中位数是0
0 0 1中位数是0
0 1 1中位数是1
1 1 1中位数是1
#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;
}