zoukankan      html  css  js  c++  java
  • 【UNR #1】奇怪的线段树

    SOL:

       好像可以贪心啊,一颗树的左儿子的右儿子和右儿子的左儿子合并。其他递归处理。

    #pragma GCC optimize("-Ofast")
    #include<bits/stdc++.h>
    using namespace std;
    int n,lx,rx,mx,ux,ans;
    int sol(int op,int l,int r,int &lx,int &rx,int &mx,int &ux) {
        int x; scanf("%d",&x);
        if (!op&&x) {puts("OwO");exit(0);}//it->fa=white && it=black is impossible
        if (l<r){
            int mid,ll,rr,l1,r1,l2,r2,m1,m2,u1,u2;
            scanf("%d",&mid);
            ll=sol(x,l,mid,l1,r1,m1,u1);
            rr=sol(x,mid+1,r,l2,r2,m2,u2);
            int mn=min(r1,l2); //it->son[0]->son[1] and it->son[1]->son[0] merge fisrt
            rx=r1-mn,lx=l2-mn; ans+=mn; ux=mn;
            if (!ll) {ans+=lx; lx=0; } // it->son[0] can't merge
            if (!rr) {ans+=rx; rx=0; }
            if (m1&&!l2) {
                if (u2) {u2--; ans--; ++lx; ++rx;}// as the father of seq so cancel the hypothesis
                else ++lx;
            } 
            if (m2&&!r1) {
                if (u1) {u1--; ans--; ++lx; ++rx;}
                else ++rx;
            }
            if (!ll&&!rr) mx=x; else mx=0; //whether this seq can merge
            lx+=l1; rx+=r2;
            if (ll) ux+=u2; if (rr) ux+=u1; // ux's mean is the number of merged seq 
        }  else {lx=rx=ux=0; mx=x;}
        return x;
    }
    signed main () {
    //    freopen("u2.in","r",stdin);
        scanf("%d",&n);
        sol(1,1,n,lx,rx,mx,ux);
        printf("%d",ans+lx+rx+mx);
        return 0;
    }
  • 相关阅读:
    第二次结对编程作业
    第一次结对编程作业
    第一次个人编程作业
    第一次博客作业
    第09组 Alpha冲刺(4/6)
    第09组 Alpha冲刺(3/6)
    第09组 Alpha冲刺(2/6)
    第09组 Alpha冲刺(1/6)
    第09组 团队Git现场编程实战
    第09组 团队项目-需求分析报告
  • 原文地址:https://www.cnblogs.com/rrsb/p/9335776.html
Copyright © 2011-2022 走看看