Codeforces Round #327 (Div. 2)C. Median Smoothing 构造
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
In the second sample the stabilization occurs in two steps: , and the sequence 00000 is obviously stable.
题意:给你一个n的01串,在一次变换中a[i]=(a[i-1],a[i],a[i+1])的中值,问你经过几次变换,使得a稳定;
题解:我们列举可以发现只有 01010...,1010...,才会变换,对于长度len为偶数 变换次数就是 (len-1)/2;
对于奇数只能变为 00000或者11111....
对于偶数只能变为 000111或者111000...
所以我们遍历一遍就能找到答案0(n);
///
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll;
#define mem(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,127,sizeof(a));
#define inf 100000007
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;
}
//****************************************
#define maxn 500000+5
int a[maxn],len;
int main()
{ int n=read();
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
}
int ans=;
for(int i=;i<n;i++){
int j=i;
while(abs(a[j]-a[j-])==&&j<=n){
j++;
}if(j-(i-)<)continue;//cout<<i-1<<" "<<j<<endl;
if((j-(i-))%){
len=j-(i-);
ans=max(ans,(len-)/);
for(int k=i;k<j;k++){
a[k]=a[i-];
}i=j-;
}else {
len=j-(i-);
ans=max(ans,(len-)/);
for(int k=i;k<=len/+i-;k++){
a[k]=a[i-];
}
for(int k=len/+i-;k<j;k++){
a[k]=a[j-];
}
}
}cout<<ans<<endl;
for(int i=;i<=n;i++){
cout<<a[i]<<" ";
}
return ;
}
代码
Codeforces Round #327 (Div. 2)C. Median Smoothing 构造的更多相关文章
- 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(找规律)
分析: 三个01组合只有八种情况: 000 s001 s010 0011 s100 s101 1110 s111 s 可以看出只有010,101是不稳定的.其他都是稳定的,且连续地出现了1或0,标记为 ...
- Codeforces Round #275 (Div. 1)A. Diverse Permutation 构造
Codeforces Round #275 (Div. 1)A. Diverse Permutation Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 ht ...
- 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 Round #327 (Div. 2)
题目传送门 水 A - Wizards' Duel 题目都没看清就写了,1e-4精度WA了一次... /************************************************ ...
- Codeforces Round #327 (Div. 1), problem: (A) Median Smoothing
http://codeforces.com/problemset/problem/590/A: 在CF时没做出来,当时直接模拟,然后就超时喽. 题意是给你一个0 1串然后首位和末位固定不变,从第二项开 ...
- codeforces590a//Median Smoothing//Codeforces Round #327 (Div. 1)
题意:一个数组,一次操作为:除首尾不变,其它的=它与前后数字的中位数,这样对数组重复几次后数组会稳定不变.问要操作几次,及最后的稳定数组. 挺难的题,参考了别人的代码和思路.总的来说就是找01010, ...
- Codeforces Round #327 (Div. 2) A. Wizards' Duel 水题
A. Wizards' Duel Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/591/prob ...
- Codeforces Round #327 (Div. 2) E. Three States BFS
E. Three States Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/591/probl ...
随机推荐
- [Windows Server 2008] IP安全策略限制端口方法
★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com ★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频. ★ 本节我们将带领大家:限制143 ...
- 基于spring 3.0mvc 框架的文件上传实现
Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块.使用 Spring 可插入的 MVC 架构,可以选择是使用内置的 Spring Web 框架还是 Struts 这样的 Web 框 ...
- jsp学习笔记 - 内置对象 pageContext
1.pageContext几乎可以操作所有的页面内置对象 pageContext.getRequest(); 得到的对象只是属于ServletRequest类,httpServletReques ...
- java如何区分同时继承的父类和实现的接口中相同的方法
基类代码: public class Father { public Father() { System.out.println("基类构造函数{"); show(); Syste ...
- Flask框架 之第一个Flask程序
from flask import Flask # 创建flask应用对象 # __name__ 代表当前模块名称 # flask以当前目录为总目录,static目录为静态目录,templates为模 ...
- JAVA程序员面试笔试宝典1
1.为什么Java中有些接口没有任何方法? 这些没有任何方法声明的接口又被称为标识接口,标识接口对于实现它的类没有任何语义上的要求,它仅仅充当一个标识的作用,用来表明它的类属于一个特定的类型. 2.j ...
- Spring框架系列(三)--Bean的作用域和生命周期
Bean的作用域 Spring应用中,对象实例都是在Container中,负责创建.装配.配置和管理生命周期(new到finalize()) Spring Container分为两种: 1.BeanF ...
- [转]Linux中进程内存与cgroup内存的统计
From: http://hustcat.github.io/about/ Linux中进程内存与cgroup内存的统计 在Linux内核,对于进程的内存使用与Cgroup的内存使用统计有一些相同和不 ...
- gson序列化后整形变浮点问题解决方案
字段值是json格式的字符串.我需要将这个字段反序列化为List<Map>形式,但是在反序列化后,id变为了1.0. 百度了很多然并卵,最后改用了阿里的fastjson,没问题.(jack ...
- C++ 迭代器运算符 箭头运算符->
所有标准库容器都支持迭代器,只有少数几种才支持下标运算 迭代器运算符 运算符 作用 *iter 返回迭代器iter所指元素的引用 iter -> mem 解引用iter,并获取元素名为mem的成 ...