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 = 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;}
- ZCMU—1660
- ZCMU—1678
- ZCMU—1605
- ZCMU—1067
- ZCMU—1607
- ZCMU—1069
- ZCMU—1611
- ZCMU—1606
- ZCMU—1774
- ZCMU—1766
- ZCMU—1752
- ZCMU—1724
- ZCMU—1623
- ZCMU—1689
- ZCMU—1667
- ZCMU—1639
- ZCMU—1668
- ZCMU—1540
- 2012届华为上机考试题目
- windows mobile 6怎么调用COM组件
- 欢迎使用CSDN-markdown编辑器
- 如何利用Visio画标准的正弦或余弦曲线
- C内存分布详解
- ZCMU—1660
- 数据结构-循环队列
- maven web 项目目录不全
- 有没有朋友介绍一下forte下怎样集成混淆器,谢谢!
- 用DW写ASP的时候,为什么没有代码提示?
- java IO流(二) ----将一个文件拆分为多个子文件
- sql server 2012远程链接的方法及步骤
- 大家帮我看看这个正则错在哪了??
- 怎样在eclipse中打war包