Codeforces Round #327 (Div. 2) C Median Smoothing(找规律)

分析:

三个01组合只有八种情况:

000 s
001 s
010 0
011 s
100 s
101 1
110 s
111 s

可以看出只有010,101是不稳定的。其他都是稳定的,且连续地出现了1或0,标记为s。

考虑连续的不稳定串的,例子:

11010100
  s        s
  110100
    s    s
    1100

只有两种情况,两个边界是不同(11和00)或者相同(11或者00)。

前者中间的不稳定串的长度是2*k,且经过k次将变成11110000,而后者的不稳定串的长度是2*k+1,中间经过k+1将变得和两边一样。

变化次数取所有情况的最大值。

 熄灯了~碎觉 QAQ ~

(做这种题的科学方法就是,打打表,写程序枚举枚举样例就有思路了。(人工枚举可能会选择性漏掉某些情况

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;

const int maxn = 5e5;
int x[maxn];

//#define LOCAL
int main()
{
#ifdef LOCAL
    freopen("in.txt","r",stdin);
#endif
    int n; scanf("%d",&n);
    for(int i = 0; i < n; i++){
        scanf("%d", x+i);
    }
    int ans = 0;
    for(int i = 1; i < n-1; i++){
        if(x[i] != x[i-1]){
            int j = i;
            while(j<n-1 && x[j] != x[j+1] && x[j] != x[j-1]) j++;
            ans = max(ans, (j-i+1)>>1);
            int p = i, q = j-1;
            while(p<=q){
                x[p++] = x[i-1];
                x[q--] = x[j];
            }
            i = j;
        }
    }
    printf("%d
", ans);
    for(int i = 0; i < n; i++){
        printf("%d%c", x[i], i == n-1? '
': ' ');
    }
    return 0;
}

 

原文地址:https://www.cnblogs.com/jerryRey/p/4909972.html