cf590A Median Smoothing
2 seconds
256 megabytes
standard input
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.
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.
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.
4
0 0 1 1
0
0 0 1 1
5
0 1 0 1 0
2
0 0 0 0 0
In the second sample the stabilization occurs in two steps: , and the sequence 00000 is obviously stable.
比较恶心
很容易注意到对于一段连续的00或者11,他们下一步也一定是00或者11。
而对于每个ai,它的下一步取值跟ai-1,ai,ai+1有关,那么在00/11左边的和右边的是互不影响的。
于是我们可以认为每个00/11中间画一条线,把他们分开,像这样 0|0
于是序列被左右端点和这些我们画的“线”分成很多部分,答案就是这些区间的答案最大值
而这些区间又都没有连续的00或者11,那一定是0101010这样的
所以一段区间的答案只跟左右端点的值和区间长度有关
具体就很容易yy了
#include<set>
#include<map>
#include<cmath>
#include<ctime>
#include<deque>
#include<queue>
#include<bitset>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define LL long long
#define inf 0x7fffffff
#define pa pair<int,int>
#define pi 3.1415926535897932384626433832795028841971
using namespace std;
inline LL read()
{
LL x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,ans,l,a[],mrk;
inline void jud(int l,int r)
{
if (l==r)return;
if(a[l]==a[r])
{
for (int i=l;i<=r;i++)a[i]=a[l];
ans=max(ans,(r-l)/);
return;
}else if (r-l>)
{
for (int i=l;i<=l+(r-l-)/;i++)a[i]=a[l];
for (int i=r-(r-l-)/;i<=r;i++)a[i]=a[r];
ans=max(ans,(r-l+)/-);
}
}
int main()
{
n=read();for (int i=;i<=n;i++)a[i]=read();mrk=-;
l=;
for(int i=;i<=n;i++)
{
if (a[i]==mrk){l=i;continue;}
mrk=-;
if (i==n)jud(l,i);
else if (a[i]==a[i-]&&i!=)jud(l,i-),l=i,mrk=a[i]; }
printf("%d\n",ans);
for (int i=;i<=n;i++)printf("%d ",a[i]);
}
cf590A
cf590A Median Smoothing的更多相关文章
- Codeforces Round #327 (Div. 2) B. Rebranding C. Median Smoothing
B. Rebranding The name of one small but proud corporation consists of n lowercase English letters. T ...
- codeforces 590A A. Median Smoothing(思维)
题目链接: A. Median Smoothing time limit per test 2 seconds memory limit per test 256 megabytes input st ...
- Codeforces Round #327 (Div. 2) C. Median Smoothing 找规律
C. Median Smoothing Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/591/p ...
- Codeforces Round #327 (Div. 2)C. Median Smoothing 构造
C. Median Smoothing A schoolboy named Vasya loves reading books on programming and mathematics. He ...
- 【22.70%】【codeforces 591C】 Median Smoothing
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- Codeforces 590 A:Median Smoothing
A. Median Smoothing time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- CodeForces 590A Median Smoothing
构造题. 答案可以o(n)构造出来.首先要发现规律.只有01交替的串才可能变化,变化规律如下: 1开头,长度为偶数(0结尾):变(len-2)/2次 变完后 前半1 后半01开头,长度为奇数(1结尾) ...
- codeforces590a//Median Smoothing//Codeforces Round #327 (Div. 1)
题意:一个数组,一次操作为:除首尾不变,其它的=它与前后数字的中位数,这样对数组重复几次后数组会稳定不变.问要操作几次,及最后的稳定数组. 挺难的题,参考了别人的代码和思路.总的来说就是找01010, ...
- ACM学习历程—CodeForces 590A Median Smoothing(分类讨论 && 数学)
题目链接:http://codeforces.com/problemset/problem/590/A 题目大意是给一个串,头和尾每次变换保持不变. 中间的a[i]变成a[i-1],a[i],a[i+ ...
随机推荐
- 新一代 PHP 加速插件 Zend Opcache <转>
注: 由于原链接已不存在, 所以我把图片重新整理了一下, 以便看起来更加直观 笔者注: 1> PHP 性能提升之 PHP NG => php next generation wiki ...
- HDU 5037 Frog(贪心)
题意比较难懂,一只青蛙过河,它最多一次跳L米,现在河中有石头,距离不等,上帝可以往里加石头,青蛙非常聪明,它一定会选择跳的次数最少的路径.问怎么添加石头能让青蛙最多的次数.输出青蛙跳的最多的次数. 考 ...
- codevs 1689 搭建高塔
/*机智sort二维转一维*/ #include<iostream> #include<cstdio> #include<cstring> #include< ...
- c# json数据解析——将字符串json格式数据转换成对象
网络中数据传输经常是xml或者json,现在做的一个项目之前调其他系统接口都是返回的xml格式,刚刚遇到一个返回json格式数据的接口,通过例子由易到难总结一下处理过程,希望能帮到和我一样开始不会的朋 ...
- canvas sprite动画 简单封装
function SpritCtx(img, size, pos, turnTime, totalCount, ctx) { size = size || {}; pos = pos || {}; / ...
- javascript MD5加密
/* * Javascript MD5 library - version 0.4 * * Coded (2011) by Luigi Galli - LG@4e71.org - * http://f ...
- 【USACO 1.4.2】时钟
[题目描述] 考虑将如此安排在一个 3 x 3 行列中的九个时钟: |-------| |-------| |-------| | | | | | | | |---O | |---O | | O | ...
- [javascript]js修改title
使用javascript修改title 1.这个在chrome中可以成功,在ie8中报错 <!DOCTYPE html> <html> <head> <tit ...
- 纯js实现分页
原理:所有数据已加载好,js通过遍历部分显示,实现分页效果 html代码 <html> <head> <meta charset='utf-8'> <scri ...
- jQuery常用事件
1.$(document).ready() $(document).ready()是jQuery中响应JavaScript内置的onload事件并执行任务的一种典型方式.它和onload具有类似的效果 ...