• bzoj 1208: [HNOI2004]宠物收养所


    1208: [HNOI2004]宠物收养所

    Time Limit: 10 Sec  Memory Limit: 162 MB
    Submit: 10068  Solved: 4020
    [Submit][Status][Discuss]

    Description

    最近,阿Q开了一间宠物收养所。收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物。每个领养者都希望领养到自己满意的宠物,阿Q根据领养者的要求通过他自己发明的一个特殊的公式,得出该领养者希望领养的宠物的特点值a(a是一个正整数,a<2^31),而他也给每个处在收养所的宠物一个特点值。这样他就能够很方便的处理整个领养宠物的过程了,宠物收养所总是会有两种情况发生:被遗弃的宠物过多或者是想要收养宠物的人太多,而宠物太少。 1. 被遗弃的宠物过多时,假若到来一个领养者,这个领养者希望领养的宠物的特点值为a,那么它将会领养一只目前未被领养的宠物中特点值最接近a的一只宠物。(任何两只宠物的特点值都不可能是相同的,任何两个领养者的希望领养宠物的特点值也不可能是一样的)如果有两只满足要求的宠物,即存在两只宠物他们的特点值分别为a-b和a+b,那么领养者将会领养特点值为a-b的那只宠物。 2. 收养宠物的人过多,假若到来一只被收养的宠物,那么哪个领养者能够领养它呢?能够领养它的领养者,是那个希望被领养宠物的特点值最接近该宠物特点值的领养者,如果该宠物的特点值为a,存在两个领养者他们希望领养宠物的特点值分别为a-b和a+b,那么特点值为a-b的那个领养者将成功领养该宠物。 一个领养者领养了一个特点值为a的宠物,而它本身希望领养的宠物的特点值为b,那么这个领养者的不满意程度为abs(a-b)。【任务描述】你得到了一年当中,领养者和被收养宠物到来收养所的情况,希望你计算所有收养了宠物的领养者的不满意程度的总和。这一年初始时,收养所里面既没有宠物,也没有领养者。

    Input

    第一行为一个正整数n,n<=80000,表示一年当中来到收养所的宠物和领养者的总数。接下来的n行,按到来时间的先后顺序描述了一年当中来到收养所的宠物和领养者的情况。每行有两个正整数a, b,其中a=0表示宠物,a=1表示领养者,b表示宠物的特点值或是领养者希望领养宠物的特点值。(同一时间呆在收养所中的,要么全是宠物,要么全是领养者,这些宠物和领养者的个数不会超过10000个)

    Output

    仅有一个正整数,表示一年当中所有收养了宠物的领养者的不满意程度的总和mod 1000000以后的结果。

    Sample Input

    5
    0 2
    0 4
    1 3
    1 2
    1 5

    Sample Output

    3
    (abs(3-2) + abs(2-4)=3,最后一个领养者没有宠物可以领养)
    #include<iostream>
    #include<cstdio>
    #define maxn 80010
    #define mod 1000000
    using namespace std;
    int rt,size,sz[maxn],son[maxn][2],fa[maxn],val[maxn],cnt[maxn],ans;
    int t1,t2;
    void update(int x){
        sz[x]=sz[son[x][0]]+sz[son[x][1]]+cnt[x];
    }
    void clear(int x){
        son[x][0]=son[x][1]=fa[x]=val[x]=cnt[x]=sz[x]=0;
    }
    void rotate(int x){
        int y=fa[x],z=fa[fa[x]],l,r;
        if(son[y][0]==x)l=0;else l=1;r=l^1;
        son[y][l]=son[x][r];fa[son[y][l]]=y;
        son[x][r]=y;fa[y]=x;
        fa[x]=z;
        if(z)son[z][son[z][1]==y]=x;
        if(rt==y)rt=x;
        update(y),update(x);
    }
    void splay(int x){
        while(x!=rt){
            int y=fa[x],z=fa[fa[x]];
            if(y!=rt){
                if((son[z][0]==y)^(son[y][0]==x))rotate(x);
                else rotate(y);
            }
            rotate(x);
        }
    }
    void insert(int x){
        int k=rt,f=0;
        while(val[k]!=x&&k)f=k,k=son[k][x>val[k]];
        if(k)cnt[k++];
        else{
            size++;k=size;
            son[k][0]=son[k][1]=0;
            sz[k]=cnt[k]=1;
            fa[k]=f;val[k]=x;
            if(f)son[f][x>val[f]]=k;
        }
        splay(k);
    }
    int find(int x){
        int now=rt,ans=0;
        while(1){
            if(val[now]>x)now=son[now][0];
            else{
                if(val[now]==x){splay(now);return ans+1;}
                ans+=cnt[now];
                now=son[now][1];
            }
        }
    }
    void pre(int k,int x){
        if(!k)return;
        if(val[k]<=x){t1=k;pre(son[k][1],x);}
        else pre(son[k][0],x);
    }
    void nxt(int k,int x){
        if(!k)return;
        if(val[k]>=x){t2=k;nxt(son[k][0],x);}
        else nxt(son[k][1],x);
    }
    int askpre(int x){
        int k=son[rt][0];
        while(son[k][1])k=son[k][1];
        return k;
    }
    void del(int x){
        find(x);
        if(cnt[rt]>1){cnt[rt]--;sz[rt]--;return;}
        if(!son[rt][0]||!son[rt][1]){int old=rt;rt=son[rt][0]+son[rt][1];clear(old);fa[rt]=0;return;}
        int old=rt,leftbig=askpre(val[rt]);
        splay(leftbig);
        son[rt][1]=son[old][1];fa[son[old][1]]=rt;
        clear(old);
        update(rt);
    }
    int main(){
        freopen("Cola.txt","r",stdin);
        int n,opt,x,kind;
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            scanf("%d%d",&opt,&x);
            if(!rt){kind=opt;insert(x);}
            else if(kind==opt)insert(x);
            else{
                t1=t2=-1;
                pre(rt,x);nxt(rt,x);
                if(t1==-1){ans+=val[t2]-x;ans%=mod;del(val[t2]);}
                else if(t2==-1){ans+=x-val[t1];ans%=mod;del(val[t1]);}
                else{
                    if(val[t2]-x<x-val[t1]){ans+=val[t2]-x;ans%=mod;del(val[t2]);}
                    else {ans+=x-val[t1];ans%=mod;del(val[t1]);}
                }
            }
        }
        printf("%d",ans);
    }
    第二次做
    #include<iostream>
    #include<cstdio>
    #define maxn 80010
    using namespace std;
    int n,size,rt,kind,t1,t2,top;
    long long ans;
    int tr[maxn][2],num[maxn],fa[maxn];
    void rotate(int x,int &k){
        int y=fa[x],z=fa[y],l,r;
        if(tr[y][0]==x)l=0;else l=1;r=l^1;
        if(y==k)k=x;
        else{if(tr[z][0]==y)tr[z][0]=x;else tr[z][1]=x;}
        fa[x]=z;fa[y]=x;fa[tr[x][r]]=y;
        tr[y][l]=tr[x][r];tr[x][r]=y;
    }
    void splay(int x,int &k){
        int y,z;
        while(x!=k){
            y=fa[x],z=fa[y];
            if(y!=k){
                if((tr[y][0]==x)^(tr[z][0]==y))rotate(x,k);
                else rotate(y,k);
            }
            rotate(x,k);
        }
    }
    void insert(int &k,int x,int last){
        if(k==0){size++;k=size;num[k]=x;fa[k]=last;splay(k,rt);return;}
        if(x<num[k])insert(tr[k][0],x,k);
        else insert(tr[k][1],x,k);
    }
    void del(int x){
        splay(x,rt);//把x旋转到根部 
        if(tr[x][0]*tr[x][1]==0){rt=tr[x][0]+tr[x][1];}//只有左子树或只有右子树 
        else{
            int k=tr[x][1];
            while(tr[k][0])k=tr[k][0];
            tr[k][0]=tr[x][0];fa[tr[x][0]]=k;
            rt=tr[x][1];
        }
        fa[rt]=0;
    }
    void ask_before(int k,int x){
        if(k==0)return;
        if(num[k]<=x){t1=k;ask_before(tr[k][1],x);}
        else ask_before(tr[k][0],x);
    }
    void ask_after(int k,int x){
        if(k==0)return;
        if(num[k]>=x){t2=k;ask_after(tr[k][0],x);}
        else ask_after(tr[k][1],x);
    }
    int main(){
        freopen("Cola.txt","r",stdin);
        scanf("%d",&n);
        int f,x;
        for(int i=1;i<=n;i++){
            scanf("%d%d",&f,&x);
            if(!rt){kind=f;insert(rt,x,0);}//三个判断句用来保证同一时间呆在收养所中的,要么全是宠物,要么全是领养者 
            else if(kind==f)insert(rt,x,0);
            else{
                t1=t2=-1;
                ask_before(rt,x);ask_after(rt,x);
                if(t1==-1){ans+=num[t2]-x;ans%=1000000;del(t2);}
                else if(t2==-1){ans+=x-num[t1];ans%=1000000;del(t1);}
                else{
                    if(x-num[t1]>num[t2]-x){ans+=num[t2]-x;ans%=1000000;del(t2);}
                    else {ans+=x-num[t1];ans%=1000000;del(t1);}
                }
            }
        }
        printf("%d",ans);
        return 0;
    }
  • 相关阅读:
    pig实战 pig常用语法总结,教你快速入门——算法篇
    本科生码农应该会的6种基本排序算法(《数据结构与算法》)
    java 大块内存做数据缓存 大数据的高效收发
    一键安装zookeeper脚本制作篇 相关经验浅谈
    C语言第01次作业顺序、分支结构
    C语言 第三次作业函数
    hashmap笔记
    ArrayList排序与对象的序列化
    插入排序笔记(MIT算法导论课程)
    java解析四则运算表达式
  • 原文地址:https://www.cnblogs.com/thmyl/p/8067676.html
Copyright © 2020-2023  润新知