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 ...
随机推荐
- Oracle 课程一之Oracle体系结构
课程目标 •理解ORACLE数据库体系架构—内存结构和进程 •理解SQL在数据库中的运作流程 •理解UNDO&REDO原理 •理解commit原理 1.Oracle数据库概述 •数据库:物 ...
- 反转链表 --剑指offer
题目:定义一个函数,输入一个链表的头结点,反转该链表并输出反正后链表的头结点. #include<stdio.h> #include<malloc.h> typedef str ...
- OutputFormat中OutputCommitter解析
在hadoop中,由于一个Task可能由多个节点同时运行,当每个节点完成Task时,一个Task可能会出现多个结果,为了避免这种情况的出现,使用了OutPutCommitter.所以OutPutCom ...
- setFocusable、setEnabled、setClickable区别
setClickable 设置为true时,表明控件可以点击,如果为false,就不能点击:“点击”适用于鼠标.键盘按键.遥控器等:注意,setOnClickListener方法会默认把控件的set ...
- Twenty Questions
题意: 有n个长度为m的二进制串,每个都是不同的. 为了把所有字符串区分开,你可以询问,每次可以问某位上是0还是1. 问最少提问次数,可以把所有字符串区分开来. 分析: dp[s1][s2]: 表示提 ...
- Dubbo服务重载方法在JDK1.8上调用出错的问题(待解决)
据说是javassist版本太低不支持JDK1.8,但是测试升级了还是调用出错.预留,待解决.
- LeetCode题解——Longest Common Prefix
题目: 给定一系列的字符串,找出这些字符串的最长公共前缀. 解法: 暴力法,依次比较每个字符串的每个字符,碰到第一个不同的就返回之前找到的前缀. 代码: class Solution { public ...
- Python 学习笔记(四)正则、闭合、生成器
(一)正则表达式 基本规则: ^ 匹配字符串开始位置. $ 匹配字符串结束位置. \b 匹配一个单词边界. \d 匹配一个数字. \D 匹配一个任意的非数字字符. x? 匹配可选的x字符.换句话说,就 ...
- Java集合排序(看完秒懂)
比如将一个List<Student>排序,则有两种方式: 1:Student实现Comparable接口: 2:给排序方法传递一个Comparator参数: 请看下面的举例: Studen ...
- (R)?ex - A simple framework to simplify system administration and datacenter automation
找工作-互联网招聘求职网-拉勾网 5-10年 (R)?ex - A simple framework to simplify system administration and datacenter ...