zoukankan      html  css  js  c++  java
  • 繁华模拟赛 中值滤波

    #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")))
  • 相关阅读:
    centos 用户管理
    rsync 实验
    文件共享和传输
    PAT 1109 Group Photo
    PAT 1108 Finding Average
    PAT 1107 Social Clusters
    PAT 1106 Lowest Price in Supply Chain
    PAT 1105 Spiral Matrix
    PAT 1104 Sum of Number Segments
    PAT 1103 Integer Factorization
  • 原文地址:https://www.cnblogs.com/hyfer/p/5791478.html
Copyright © 2011-2022 走看看