题目链接:

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.

 
Examples
 
input
  1. 4
    0 0 1 1
output
  1. 0
    0 0 1 1
input
  1. 5
    0 1 0 1 0
output
  1. 2
    0 0 0 0 0
  2.  
  3. 题意:
  4.  
  5. 给一个01串,每次每一位的变换都是这一位相邻的左右两个然后三人表决器的结果;问经过最少多少次变换会稳定,且输出最后的状态;
  6.  
  7. 思路:
  8.  
  9. 可以方向如果多于两个的相同的在一起,那么这些相邻的就不会变化了,这样就把这个串分成了多个子串,然后可以发现这些字串的最后状态和其左右界有关,最少的操作数等于(r-l)/2;
    具体的看代码;
  10.  
  11. AC代码:
  1. //#include <bits/stdc++.h>
  2. #include <vector>
  3. #include <iostream>
  4. #include <queue>
  5. #include <cmath>
  6. #include <map>
  7. #include <cstring>
  8. #include <algorithm>
  9. #include <cstdio>
  10.  
  11. using namespace std;
  12. #define Riep(n) for(int i=1;i<=n;i++)
  13. #define Riop(n) for(int i=0;i<n;i++)
  14. #define Rjep(n) for(int j=1;j<=n;j++)
  15. #define Rjop(n) for(int j=0;j<n;j++)
  16. #define mst(ss,b) memset(ss,b,sizeof(ss));
  17. typedef long long LL;
  18. template<class T> void read(T&num) {
  19. char CH; bool F=false;
  20. for(CH=getchar();CH<''||CH>'';F= CH=='-',CH=getchar());
  21. for(num=;CH>=''&&CH<='';num=num*+CH-'',CH=getchar());
  22. F && (num=-num);
  23. }
  24. int stk[], tp;
  25. template<class T> inline void print(T p) {
  26. if(!p) { puts(""); return; }
  27. while(p) stk[++ tp] = p%, p/=;
  28. while(tp) putchar(stk[tp--] + '');
  29. putchar('\n');
  30. }
  31.  
  32. const LL mod=1e9+;
  33. const double PI=acos(-1.0);
  34. const LL inf=1e14;
  35. const int N=5e5+;
  36. int n,a[N],p[N],nex[N];
  37. int fun(int l,int r)
  38. {
  39. if(a[r]==a[l])
  40. {
  41. for(int i=l;i<=r;i++)a[i]=a[l];
  42. }
  43. else
  44. {
  45. for(int i=l;i<=l+(r-l)/;i++)a[i]=a[l];
  46. for(int i=l+(r-l)/+;i<=r;i++)a[i]=a[r];
  47. }
  48. return (r-l)/;
  49. }
  50. int main()
  51. {
  52. read(n);
  53. Riep(n)read(a[i]),p[i]=nex[i]=i;
  54. for(int i=;i<=n;i++)
  55. {
  56. if(a[i]==a[i-])p[i]=p[i-],nex[p[i]]=i;
  57. }
  58. int l=nex[],r=n;
  59. int ans=;
  60. for(int i=nex[]+;i<n;i++)
  61. {
  62. if(nex[i]!=i)
  63. {
  64. r=i;
  65. ans=max(ans,fun(l,r));
  66. i=nex[i];
  67. l=i;
  68. }
  69. }
  70. r=p[n];
  71. ans=max(ans,fun(l,r));
  72. cout<<ans<<"\n";
  73. Riep(n)printf("%d ",a[i]);
  74. return ;
  75. }
  1.  

codeforces 590A A. Median Smoothing(思维)的更多相关文章

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

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

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

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

  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. Codeforces 590 A:Median Smoothing

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

  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 590A Median Smoothing

    构造题. 答案可以o(n)构造出来.首先要发现规律.只有01交替的串才可能变化,变化规律如下: 1开头,长度为偶数(0结尾):变(len-2)/2次 变完后 前半1 后半01开头,长度为奇数(1结尾) ...

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

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

随机推荐

  1. 图片 文字 input等垂直居中对齐

    span::before { width:100%; height:1px; font-size:1em; content:''; background:#fff; position:absolute ...

  2. 函数 MultiByteToWideChar() 详解

    函数原型: int MultiByteToWideChar( UINT CodePage, DWORD dwFlags, LPCSTR lpMultiByteStr, int cchMultiByte ...

  3. javac 命令

    HelloWorld.java:1: 需要为 class.interface 或 enum 锘缝ublic class HelloWorld{ ^ 1 错误 这个错误出现的原因主要是在中文操作系统中, ...

  4. HTML第九天学习笔记

    今天就继续看了下浮点float属性,代码如下: <html> <head> <title>CSS float属性</title> <meta ht ...

  5. Oracle中decode方法的作用

    DECODE(条件,值1,翻译值1,值2,翻译值2,...值n,翻译值n,缺省值) 该函数含义如下: IF 条件=值1 THEN    RETURN (翻译值1) ELSIF 条件=值2 THEN   ...

  6. IOS开发之路四(UITabBarController)

    前两天看了看斯坦福大学的iphone开发公开课,讲的倒是不错,可看的我云里雾里的,不怎么讲基础和原理,不太适合初学者.今天看了一上午ios5基础教程这本书感觉有点头绪了....废话少说,讲一讲我上午做 ...

  7. 闲话Cache:始篇

    Caching(缓存)在现代的计算机系统中是一项最古老最基本的技术.它存在于计算机各种硬件和软件系统中,比如各种CPU, 存储系统(IBM ESS, EMC Symmetrix…),数据库,Web服务 ...

  8. Android开发代码混淆经验(Eclipse)

    为了防止自己的劳动成果被别人窃取,混淆代码能有效防止被反编译,下面来总结以下混淆代码的步骤: 2.编辑项目下的proguard-project.txt,添加不需要混淆的规则(model.泛型.反射.第 ...

  9. iOS开发——UI篇Swift篇&UIWebView

    UIWebView //返回按钮事件 @IBAction func backButtonClick() { self.navigationController?.popViewControllerAn ...

  10. js获取非行间样式/定义样式

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