ZCMU—1660

原创 2017年01月02日 22:19:56

1660: = ̄ω ̄= 题目真的是英文

Time Limit: 1 Sec  Memory Limit: 128 MB
[Submit][Status][Web Board]

Description

We have a sequence of numbers a1, a2, ..., an will result a new sequence b1, b2, ..., bn obtained by the following algorithm:

  • b1 = a1bn = 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 - 1ai 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, We decided to apply the method to sequences consisting of zeros and ones only.

Having made the procedure once, We 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? We tried a couple of examples and found out that after some number of median  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 We 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 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
5
0 1 0 1 0

Sample Output

0
0 0 1 1
2
0 0 0 0 0

HINT

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

【分析】

题意:给一个串,可以进行操作,每次操作头和尾每次变换保持不变,中间的a[i]变成a[i-1],a[i],a[i+1]的中位数,并且串中只有01.
对于01串
0 0 0中位数是0
0 0 1中位数是0
0 1 1中位数是1
1 1 1中位数是1
所以可以发现,如果某个串中有相邻一样的串,那么这个串是不会变的,所以我们需要处理的就是010101这种间隔的串,中间所有相邻的串都可以跳过,并且对其中某一个串进行变换,最终的结果就是使这个串前一半跟头一样,后一半跟尾一样。
答案就是找其中那个需要转化次数最多的串,也就是长度最长的01间隔串,答案就是这个串的一半。
所以并不是一开始认为的是模拟题...根本不用考虑时间复杂度...

【代码】
#include <stdio.h>  
int a[510000], f[510000];  
int main()  
{  
    int res, len, i, j, n, mid, k;  
    while (~scanf("%d", &n))
	{ 
	    for(int i=0;i<n;i++) scanf("%d",&a[i]);  
	    int ans=0;  
	    for(int i=0;i<n;i++)  
	    {  
	        f[i]=a[i];
			int j;  
	        for(j=i;j<n-1;j++)
	            if(a[j]==a[j+1]) 
					break;  
	        int len=j-i-1;  
	        mid=(j+i)/2;  
	        for(int k=i;k<=j;k++)  
	            if(k<=mid) 
					f[k]=a[i];  
	            else 
					f[k]=a[j];  
	        if(len>ans) ans=len;  
	        i=j;  
	    }  
	    printf("%d\n",/2+ans%2);  
	    for(int i=0;i<n-1;i++) printf("%d ",f[i]);  
		printf("%d\n",f[n-1]);
	}
	return 0;
}
  


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

相关文章推荐

UltraEdit在Win2003一运行就崩溃的解决办法

最近在Win2003安装了新版的UltraEdit, 但一运行就跳出错误对话框:“配置文件更新: Uedit32.exe - 应用程序错误: 应用程序发生异常 未知的软件异常 (0xc0000417)...
  • tanaya
  • tanaya
  • 2011-06-03 09:47
  • 5310

1030. 完美数列(25):要考虑全面

第一次非AC代码 逻辑不够严谨 #include #include #include using namespace std; #define Max 100000 long long arr[...

ZCMU—1434

1434: 糖果迷阵 Time Limit: 1 Sec  Memory Limit: 128 MB [Submit][Status][Web Board] Description I...

ZCMU—1403

1403: Arne Saknussemm Time Limit: 1 Sec  Memory Limit: 128 MB [Submit][Status][Web Board] Descr...

uva1660 最大流

点连通度和边连通度问题都可以转化为最小割最大流问题。用拆点法,相同点连接一条边,容量为1,不同点连2条边i+1→j,j+1→i,容量为INF。 注意scanf的用法,还可以这么用,以前不知道。 ...

bsoj 1660 选课(树形DP)

【模拟试题】选课   Time Limit:10000MS  Memory Limit:65536K Total Submit:365 Accepted:166  Case T...
  • fp_hzq
  • fp_hzq
  • 2012-09-26 14:02
  • 8420

CSU 1660 搜索 在图中寻找一个长度为K的环

题目的大概意思就是在给出的图里面寻找一个长度为K的环。。。       想了一想,肯定是用DFS,然后就考虑实现,可是以什么作为参数呢,以下是我考虑不恰当的地方:       我考虑的DFS参数有3个...

UVA 1660 Cable TV Network [最小割] [图的连通度] [拆点]

Cable TV Network Time Limit: 3000MS 64bit IO Format: %lld & %llu 求无向图的点连通度,也就是说求任意两点最...

【BZOJ 1660】 [Usaco2006 Nov]Bad Hair Day 乱发节

单调队列~
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)