题目链接:

A. Median Smoothing

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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, ..., bnobtained 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.

 
Examples
 
input
4
0 0 1 1
output
0
0 0 1 1
input
5
0 1 0 1 0
output
2
0 0 0 0 0 题意: 给一个01串,每次每一位的变换都是这一位相邻的左右两个然后三人表决器的结果;问经过最少多少次变换会稳定,且输出最后的状态; 思路: 可以方向如果多于两个的相同的在一起,那么这些相邻的就不会变化了,这样就把这个串分成了多个子串,然后可以发现这些字串的最后状态和其左右界有关,最少的操作数等于(r-l)/2;
具体的看代码; AC代码:
//#include <bits/stdc++.h>
#include <vector>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <cstring>
#include <algorithm>
#include <cstdio> using namespace std;
#define Riep(n) for(int i=1;i<=n;i++)
#define Riop(n) for(int i=0;i<n;i++)
#define Rjep(n) for(int j=1;j<=n;j++)
#define Rjop(n) for(int j=0;j<n;j++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
typedef long long LL;
template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<''||CH>'';F= CH=='-',CH=getchar());
for(num=;CH>=''&&CH<='';num=num*+CH-'',CH=getchar());
F && (num=-num);
}
int stk[], tp;
template<class T> inline void print(T p) {
if(!p) { puts(""); return; }
while(p) stk[++ tp] = p%, p/=;
while(tp) putchar(stk[tp--] + '');
putchar('\n');
} const LL mod=1e9+;
const double PI=acos(-1.0);
const LL inf=1e14;
const int N=5e5+;
int n,a[N],p[N],nex[N];
int fun(int l,int r)
{
if(a[r]==a[l])
{
for(int i=l;i<=r;i++)a[i]=a[l];
}
else
{
for(int i=l;i<=l+(r-l)/;i++)a[i]=a[l];
for(int i=l+(r-l)/+;i<=r;i++)a[i]=a[r];
}
return (r-l)/;
}
int main()
{
read(n);
Riep(n)read(a[i]),p[i]=nex[i]=i;
for(int i=;i<=n;i++)
{
if(a[i]==a[i-])p[i]=p[i-],nex[p[i]]=i;
}
int l=nex[],r=n;
int ans=;
for(int i=nex[]+;i<n;i++)
{
if(nex[i]!=i)
{
r=i;
ans=max(ans,fun(l,r));
i=nex[i];
l=i;
}
}
r=p[n];
ans=max(ans,fun(l,r));
cout<<ans<<"\n";
Riep(n)printf("%d ",a[i]);
return ;
}

最新文章

  1. 使用HTML5的History API
  2. LATTICE 存储之FIFO的使用
  3. Daject初探 - 从Table模型得到Record模型
  4. Windows基于Apache的svn服务器配置
  5. sort-based shuffle的核心:org.apache.spark.util.collection.ExternalSorter
  6. 关于js封装框架类库之选择器引擎(二)
  7. 我们究竟什么时候可以使用Ehcache缓存(转)
  8. .Net程序员学用Oracle系列(16):访问数据库(ODP.NET)
  9. 【BZOJ4653】【NOI2016】区间(线段树)
  10. C++ 左值与右值 右值引用 引用折叠 =&gt; 完美转发
  11. nim读写注册表的小例子
  12. Django基础回顾与补充(79-80)
  13. 吴裕雄 python深度学习与实践(17)
  14. C++程序设计方法2:基本语法
  15. 网页中的数据的4个处理方式:CRUD(Creat, Retrive, Update, Delete)
  16. :after 写三角形 border
  17. Android逆向 APK文件组成
  18. 获取select的 text
  19. System.Web.Mvc.AJAX缺少程序集引用
  20. Revit二次开发示例:AutoStamp

热门文章

  1. Windows下使用NIF扩展Erlang方法
  2. IE=EmulateIE8和IE=IE8的区别
  3. IE6常见bug整理
  4. ActiveMQ集成到Spring
  5. JSP中的TAG
  6. svn的搭建
  7. flume ng配置拓扑图
  8. IE6下z-index失效
  9. 解决从内部存储设备安装apk提示Permission Denied
  10. 数据对象ajax学习篇9