Codeforces Round #327 (Div. 2) C. Median Smoothing 找规律
C. Median Smoothing
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
http://codeforces.com/contest/591/problem/C
Description
Applying the simplest variant of median smoothing to the 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, 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.
Sample Input
4
0 0 1 1
Sample Output
0
0 0 1 1
HINT
题意
给你n个只含有0和1的数组,每次迭代的时候,b1=a1,bn=an,bi=(ai-1+ai+ai+2)/2
然后问你多少次之后会稳定不变,并且把稳定不变的数组输出
题解:
找找规律就可以知道,我们只要找010101这种间隔的就好了
如果长度为偶数,那么最后会变成111000这种
如果长度为奇数,那么最后会全部变成11111或者00000这种
代码
#include<iostream>
#include<stdio.h>
using namespace std;
#define maxn 500005
int a[maxn];
int b[maxn];
int main()
{
int n;scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
int ans = ;
for(int i=;i<=n;i++)
{
if(i==n)
{
b[i]=a[i];
continue;
}
if(a[i]==a[i+])
{
b[i]=(a[i-]+a[i]+a[i+])/;
continue;
}
int j;
for(j=i;j<n;j++)
{
if(a[j]==a[j+])
break;
}
//cout<<j<<" "<<i<<endl;
if((j-i+)<=)
{
if(i==)
b[i]=a[i];
else
b[i]=(a[i-]+a[i]+a[i+])/;
continue;
}
if(j==n&&(j-i+)<=)
{
ans = max(ans,);
b[i]=(a[i-]+a[i]+a[i+])/;
continue;
} int flag = ;
if((j-i+)%==)
{
ans = max((j-i+)/-,ans);
for(int k=i;k<i+(j-i+)/;k++)
b[k]=a[i];
for(int k=i+(j-i+)/;k<=j;k++)
b[k]=-a[i];
}
else
{
ans = max((j-i+)/,ans);
for(int k=i;k<=j;k++)
b[k]=a[i];
}
i=j;
}
b[]=a[];
b[n]=a[n];
printf("%d\n",ans);
for(int i=;i<=n;i++)
printf("%d ",b[i]);
printf("\n");
}
Codeforces Round #327 (Div. 2) C. Median Smoothing 找规律的更多相关文章
- Codeforces Round #327 (Div. 2)C. Median Smoothing 构造
C. Median Smoothing A schoolboy named Vasya loves reading books on programming and mathematics. He ...
- Codeforces Round #327 (Div. 2) C Median Smoothing(找规律)
分析: 三个01组合只有八种情况: 000 s001 s010 0011 s100 s101 1110 s111 s 可以看出只有010,101是不稳定的.其他都是稳定的,且连续地出现了1或0,标记为 ...
- Codeforces Round #347 (Div. 2) C. International Olympiad 找规律
题目链接: http://codeforces.com/contest/664/problem/C 题解: 这题最关键的规律在于一位的有1989-1998(9-8),两位的有1999-2098(99- ...
- 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 ...
随机推荐
- Ruby基础数据类型
#数字分为证书Integer,浮点数Float(对应与其他语言中的double),和复数Complex #整数又分为Fixnum和Bignum,Fixnum和Bignum会互相转换,这些都是ruby自 ...
- Docker管理面板Crane开源了!
导读 数人云容器管理面板 Crane 开源啦!Crane 包含着数人云工程师对 Docker 最新技术的热爱和实践.希望借助开源社区的力量,让 Crane 完善自身,更好地成长起来,让更多的国内用户体 ...
- POJ 3744 Scout YYF I (概率dp+矩阵快速幂)
题意: 一条路上,给出n地雷的位置,人起始位置在1,向前走一步的概率p,走两步的概率1-p,踩到地雷就死了,求安全通过这条路的概率. 分析: 如果不考虑地雷的情况,dp[i],表示到达i位置的概率,d ...
- PHP Module中获取$_GET/$_POST/$_COOKIE的方法研究
假设要获取$_GET['c']; 首先,先介绍下http_globals; 1.http_globals,定义在php_globals.h中: zval * http_globals[6]; 其中的索 ...
- Geodesic-based robust blind watermarking method for three-dimensional mesh animation by using mesh segmentation and vertex trajectory
之前因为考试,中断了实验室的工作,现在结束考试了,不能再荒废了. 最近看了一篇关于序列水印的文章,大体思想是:对于一个网格序列,首先对第一帧进行处理,在第一帧上,用网格分割算法(SDF)将网格分割成几 ...
- 第2课 讲解主流三大web服务器之Apache服务器(httpd服务器) - 大型网站高并发架构与自动化运维实战(六)
基本配置 配置固定IP地址 打开默认的网卡配置文件 cd /etc/sysconfig/network-script/ cp ifcfg-eth0 ifcfg-eth1 vim ifcfg-eth0 ...
- 在ASP.NET下做了一个实验MVC的小东西
星期五下班前一时兴起,对MVC发表了一点看法.后来就想干脆弄个小东西来验证一些自己的想法帮组理清思路.所要源代码的免了,3个小时的急就章实在是乱得可以,既没有好的架构,也没有任何代码质量可言,主要是实 ...
- C++ 我想这样用(七)
话接前篇,继续基于对象编程语法的剩余部分: 6.类的const成员函数和const对象 const数据成员:跟const常量一样,只是一个在类里(而且是在构造函数里),一个在类外而已,都必须初始化. ...
- 在NodeJS中配置aws ec2
获取access key和secret access key 自己账户下有security credentials的选项 然后点击Acce ...
- j2ee的十三个规范
转载 今天在做连接oracle数据库的时候,感受到了什么是规范.平时听到别人说学习j2ee一定要学习他的十三个规范,大概的知道每个规范是做什么的,每个“接口”是做什么的. 很早就听过 ...