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.

Sample test(s)
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
Note

In the second sample the stabilization occurs in two steps: ,
and the sequence 00000 is obviously stable.

题意是给出了一段 0 1 组成的波,然后这个波的值可能会发生变化,排除起点与终点,如果某一位置上的值不等于其左右位置加上自己的中位数话,那么它会变成中位数,这样导致波会振荡一次,问波最终是否会稳定,不会输出-1。会,就输出其振荡次数与最终波的值。

首先可以判断最终波是一定会稳定的,不可能最终不稳定,因为只有0与1,边缘值a1与an不变了,所以他们只会往中间延伸这种稳定的状态,然后我做的方法是叠加,如果前者不稳定,那么后者的不稳定程度+1,其实这么做是不对的,波振荡的这一系列的值只可能是一个山峰形状的,如1 2 1 或者 1 2 2 1.但是因为都是 0 1组成所以不用管那么多,只需管每一位置振荡次数的奇偶即可。然后就是判断每一段振荡最终位置的值,奇数不用管了,1
2 3 4 5和1 2 3 2 1最终形成的效果是一样的。偶数需要调整,1 2 3 4要调整为 1 2 2 1这样的效果。

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; int n;
int a[5];
int val[500005];
int stable[500005]; int main()
{
//freopen("i.txt", "r", stdin);
//freopen("o.txt", "w", stdout); int i, k, maxn;
scanf("%d", &n); for (i = 0; i < n; i++)
scanf("%d", val + i); maxn = 0;
memset(stable, 0, sizeof(stable)); for (i = 1; i < n - 1; i++)
{
a[0] = val[i - 1];
a[1] = val[i];
a[2] = val[i + 1]; sort(a, a + 3); if (val[i] != a[1])
{
stable[i] = stable[i - 1] + 1;
maxn = max(maxn, stable[i]);
}
else
{
stable[i] = 0;
}
}
int flag = 0;
for (i = n - 2; i >= 1; i--)
{
if (stable[i] == 0)
{
flag = 0;
}
else
{
if (flag == 0 && stable[i])
{
flag = 1;
if (stable[i] % 2 == 0)
{
int temp = stable[i];
for (k = temp; k > temp / 2; k--)
{
stable[i]++;
i--;
}
i++;
}
}
}
} printf("%d\n", (maxn + 1) / 2);
for (i = 0; i < n; i++)
{
if (i == 0)
{
printf("%d", val[i]);
}
else
{
if (stable[i] & 1)
{
printf(" %d", (val[i] + 1) & 1);
}
else
{
printf(" %d", val[i]);
}
}
}
printf("\n");
//system("pause");
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

Codeforces 590 A:Median Smoothing的更多相关文章

  1. 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 ...

  2. 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 ...

  3. codeforces 590A A. Median Smoothing(思维)

    题目链接: A. Median Smoothing time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  4. Codeforces Round #327 (Div. 2)C. Median Smoothing 构造

    C. Median Smoothing   A schoolboy named Vasya loves reading books on programming and mathematics. He ...

  5. 【22.70%】【codeforces 591C】 Median Smoothing

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  6. cf590A Median Smoothing

    A. Median Smoothing time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  7. ACM学习历程—CodeForces 590A Median Smoothing(分类讨论 && 数学)

    题目链接:http://codeforces.com/problemset/problem/590/A 题目大意是给一个串,头和尾每次变换保持不变. 中间的a[i]变成a[i-1],a[i],a[i+ ...

  8. Codeforces Round #327 (Div. 1), problem: (A) Median Smoothing

    http://codeforces.com/problemset/problem/590/A: 在CF时没做出来,当时直接模拟,然后就超时喽. 题意是给你一个0 1串然后首位和末位固定不变,从第二项开 ...

  9. CodeForces - 1005E2:Median on Segments (General Case Edition) (函数的思想)

    You are given an integer sequence a1,a2,…,ana1,a2,…,an. Find the number of pairs of indices (l,r)(l, ...

随机推荐

  1. 使用YUM安装软件时提示PackageKit睡眠中解决方法!

    报错如图所示: 解决方法一:移除var/run/yum.pid文件 方法二:直接杀掉进程号 报错的时候会跟进程号 直接利用kill   -9  +进程号

  2. Idea 隐藏不必要的文件或文件夹 eg:(.idea,.gitignore,*.iml)

    在使用Idea的时候,有一些文件是不必要的,可以将他们隐藏起来 方法:打开File–>Settings–>Editor如图,在File Types 中的 Ignore files and ...

  3. redhat7.6 httpd配置php模块

    1.安装php yum install "*php*"   -y 2.编辑httpd.conf配置文件 找到LoadModule foo_module modules/mod_fo ...

  4. connection String加密

    aspnet_regiis -pe "connectionStrings" -app "/HG" -prov "ChrisProvider" ...

  5. sublime3常用环境配置

    如何设置侧边栏颜色 Ctrl+Shift+P -> install -> 搜索安装包SyncedSidebarBg,自动同步侧边栏底色为编辑窗口底色. 设置快捷键让html文件在浏览器窗口 ...

  6. SpringMVC笔记三

    课程安排: 第一天:springmvc的基础知识 什么是springmvc? springmvc框架原理(掌握) 前端控制器.处理器映射器.处理器适配器.视图解析器 springmvc入门程序 目的: ...

  7. [转]Shell编程

    原文链接 Shell编程其实真的很简单(一) 如今,不会Linux的程序员都不意思说自己是程序员,而不会shell编程就不能说自己会Linux.说起来似乎shell编程很屌啊,然而不用担心,其实she ...

  8. ubuntu---CUDA 安装注意点总结

    安装CUDA前的基础准备: 1.查看内核.gcc版本并记住. 最好 禁止内核更新,以防止以后工作中意外的系统更新使内核自动更新了,与驱动版本不兼容了.   2.禁用 nouveau驱动.   3.多下 ...

  9. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 显示代码:按键提示

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  10. http的3次握手与4次挥手

    Http的3次握手: 第一次握手:客户端发送一个带SYN的TCP报文到服务器,表示客户端想要和服务器端建立连接. 第二次握手:服务器端接收到客户端的请求,返回客户端报文,这个报文带有SYN和ACK确认 ...