繁华模拟赛 中值滤波

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
int n,a[500005],b[500005],fz[500005],fo[500005],ans;
char cmd;
int main(){
    freopen("median.in","r",stdin);
    freopen("median.out","w",stdout);
    cin>>n;
    for(int i = 1;i <= n;i++){
        scanf("%d",&a[i]);
    }
    for(int i = 3;i <= n;i++){
        if(a[i-2] == 0 && a[i-1] == 1 && a[i] == 0) fz[i] = 1;
        if(a[i-1] == 1 && a[i] == 0 && fz[i-2]) fz[i] = max(fz[i],fz[i-2] + 1);
        ans = max(ans,fz[i]);
        if(a[i-2] == 1 && a[i-1] == 0 && a[i] == 1) fo[i] = 1;
        if(a[i-1] == 0 && a[i] == 1 && fo[i-2]) fo[i] = max(fo[i],fo[i-2] + 1);
        ans = max(ans,fo[i]);
    }
    cout<<ans<<endl;
    int cnt = n,st = 0,to0 = 0,to1 = 0,cl = 0,op = 0;
    while(cnt){
        b[cnt] = a[cnt];
        if((fz[cnt] && fo[cnt-1] == fz[cnt]) && !to0 && !to1 && !cl){
            to0 = to1 = fz[cnt] + 1;
            st = 0;
        }
        if((fo[cnt] && fz[cnt-1] == fo[cnt]) && !to0 && !to1 && !cl){
            to0 = to1 = fo[cnt] + 1;
            st = 1;
        }
        if((st == 1 && to1) || (!to0 && to1)){
            b[cnt] = 1;
            to1--;
        }else if((st == 0 && to0) || (!to1 && to0)){
            b[cnt] = 0;
            to0--;
        }
        if(fz[cnt] && fz[cnt] == fo[cnt-1] + 1 && !cl && !to0 && !to1){
            cl = fz[cnt];
            op = 0;
        }
        if(fo[cnt] && fo[cnt] == fz[cnt-1] + 1 && !cl && !to0 && !to1){
            cl = fo[cnt];
            op = 1;
        }
        if(cl && op == 1 && a[cnt] == 0){
            cl--;
            b[cnt] = 1;
        }else if(cl && op == 0 && a[cnt] == 1){
            cl--;
            b[cnt] = 0;
        }
        cnt--;
    }
    for(int i = 1;i <= n;i++) printf("%d ",b[i]);
    return 0;
}

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<cstdlib>
#include<string>
#include<bitset>
#define INF 1000000000
#define N 500005
#define fi first
#define se second
#define debug(x) cout<<#x<<"="<<x<<endl
#define MP(x,y) make_pair(x,y)
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;
int a[N];
int main()
{
    int ans=0,i,n,cnt,l,r;
    freopen("median.in","r",stdin);
    freopen("median.out","w",stdout);
    cin>>n;
    for(i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
    }
    cnt=0;
    a[0]=a[1];
    a[n+1]=a[n];
    n++;
    for(i=1;i<=n;i++)
    {
        //debug(cnt);
        if(a[i]!=a[i-1])
            cnt++;
        else
        {
            ans=max(ans,cnt/2);
            if(cnt>=2)
            {
                if(cnt%2==0)
                    for(r=i;r>=i-cnt;r--)
                        a[r]=a[i];
                else
                {
                    l=i-cnt-2;
                    r=i;
                    for(;r>l;r--,l++)
                        a[r]=a[i],a[l]=a[i-cnt-2];
                }
            }
            cnt=0;
        }
    }
    //ans=max(ans,cnt/2);
    cout<<ans<<endl;
    for(i=1;i<=n-1;i++)
        printf("%d ",a[i]);
    return 0;
}
// davidlee1999WTK 2015/
// srO myk Orz
//ios::sync_with_stdio(false);
//#pragma comment(linker, "/STACK:102400000,102400000")
//#define O2 __attribute__((optimize("-O2")))

 

posted @ 2016-08-20 23:04  ACforever  阅读(213)  评论(0编辑  收藏  举报