zoukankan      html  css  js  c++  java
  • 【AVL】宠物收养所

    【HNOI2004】宠物收养所

    时间限制: 5 Sec  内存限制: 128 MB

    题目描述

    最近,阿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)。【任务描述】你得到了一年当中,领养者和被收养宠物到来收养所的情况,希望你计算所有收养了宠物的领养者的不满意程度的总和。这一年初始时,收养所里面既没有宠物,也没有领养者。

    输入

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

    输出

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

    样例输入

     (如果复制到控制台无换行,可以先粘贴到文本编辑器,再复制)

    5
    0 2
    0 4
    1 3
    1 2
    1 5
    

    样例输出

    3

    提示


    #----------------------------------------------------------------------------------------------#
    至于AVL,请看:Splay树
    里面有AVL详解

    不要以为此题要2棵树,只要1棵树:在同一时间里,只会有人宠物之一在收养所。
    所以,只需要看一下现在是狗在树(收养所)里还是人。

    代码:
    #include<cstdio>
    #include<cmath>
    #include<algorithm>
    using namespace std;
    #define MAXN 100000
    #define MOD 1000000
    struct node
    {
        int num;
        int l,r,h;
    }tree[MAXN+5];
    int N,R;
    int cnt;
    bool F;
    void read(int &x)
    {
        int f=1; x=0; char s=getchar();
        while(s<'0'||s>'9'){if(s=='-') f=-1;s=getchar();}
        while(s>='0'&&s<='9'){x=x*10+s-'0';s=getchar();}
        x*=f;
    }
    int zig(int x)
    {
        int t=tree[x].l;
        tree[x].l=tree[t].r;
        tree[t].r=x;
        tree[x].h=max(tree[tree[x].r].h,tree[tree[x].l].h)+1;
        tree[t].h=max(tree[tree[t].r].h,tree[tree[t].l].h)+1;
        return t;
    }
    int zag(int x)
    {
        int t=tree[x].r;
        tree[x].r=tree[t].l;
        tree[t].l=x;
        tree[x].h=max(tree[tree[x].r].h,tree[tree[x].l].h)+1;
        tree[t].h=max(tree[tree[t].r].h,tree[tree[t].l].h)+1;
        return t;
    }
    int zigzag(int x)
    {
        tree[x].r=zig(tree[x].r);
        return zag(x);
    }
    int zagzig(int x)
    {
        tree[x].l=zag(tree[x].l);
        return zig(x);
    }
    void maintain(int &root)
    {
        if(tree[tree[root].l].h==tree[tree[root].r].h+2)
        {
            int t=tree[root].l;
            if(tree[tree[t].l].h==tree[tree[root].r].h+1)
                root=zig(root);
            else if(tree[tree[t].r].h==tree[tree[root].r].h+1)
            {
                tree[root].l=zag(tree[root].l);
                root=zig(root);
            }
        }
        else if(tree[tree[root].l].h+2==tree[tree[root].r].h)
        {
            int t=tree[root].r;
            if(tree[tree[t].r].h==tree[tree[root].l].h+1)
                root=zag(root);
            else if(tree[tree[t].l].h==tree[tree[root].l].h+1)
            {
                tree[root].r=zig(tree[root].r);
                root=zag(root);
            }
        }
        tree[root].h=max(tree[tree[root].l].h,tree[tree[root].r].h)+1;
    }
     
    int insert(int x,int root)
    {
        if(!root)
        {
            tree[++cnt].num=x;
            tree[cnt].h=1;
            return cnt;
        }
        if(x<tree[root].num)
        {
            tree[root].l=insert(x,tree[root].l);
            if(tree[tree[root].l].h==tree[tree[root].r].h+2)
            {
                if(tree[tree[root].l].num>x) root=zig(root);
                else if(tree[tree[root].l].num<x) root=zagzig(root);
            }
        }
        else if(x>tree[root].num)
        {
            tree[root].r=insert(x,tree[root].r);
            if(tree[tree[root].r].h==tree[tree[root].l].h+2)
    		{
                if(tree[tree[root].r].num<x) root=zag(root);
                else if(tree[tree[root].r].num>x) root=zigzag(root);
            }
        }
        tree[root].h=max(tree[tree[root].l].h,tree[tree[root].r].h)+1;
        return root;
    }
    /*
    5//这是一组数据
    1 4
    1 6
    0 5
    0 4
    1 2
    */
    int dale(int &root,int x)
    {
        int t;
        if(x==tree[root].num||(x<tree[root].num&&tree[root].l==0)||(x>tree[root].num&&tree[root].r==0))
        {
            if(tree[root].l==0||tree[root].r==0)
            {
                t=tree[root].num;
                root=tree[root].l+tree[root].r;
                return t;
            }
            else
                tree[root].num=dale(tree[root].l,x);
        }
        else
        {
            if(x<tree[root].num) t=dale(tree[root].l,x);
            else t=dale(tree[root].r,x);
        }
        maintain(root);
        return t;
    }//以上都是模板
    int D;
    int find(int root,int x)//找到与x相差最小的值(返回它们的差)
    {
    	if(!root) return 0x7fffffff;//找到了根(不能选),返回极大值
    	if(x==tree[root].num)//找到了与x完全一样的值
    	{
    		D=tree[root].num;//D保存要删除结点的值
    		return 0;//差自然为0
    	}
    	if(x>tree[root].num)
    	{
    		int t=find(tree[root].r,x);//找右边(如果找左边差会越来越大,找右边会有可能变小)
    		if(x-tree[root].num<=t)//如果还是当前差值小(注意'<=',因为左儿子和右儿子如果一样会选左儿子(题目有说))
    		{
    			D=tree[root].num;
    			return x-tree[root].num;
    		}
    		else return t;
    	}
    	if(x<tree[root].num)
    	{
    		int t=find(tree[root].l,x);
    		if(tree[root].num-x<t)
    		{
    			D=tree[root].num;
    			return tree[root].num-x;
    		}
    		else return t;
    	}//与上面类似
    }
    int main()
    {
        int a,x,ans=0;
        int pn=0,dn=0;//pn是现在收养所中人的数量,dn是现在收养所中宠物的数量
        read(N);
        while(N--)
        {
            read(a);read(x);
    		if(!pn&&!dn)
    		{
    			if(a){R=insert(x,R);pn++;}
    			else{R=insert(x,R);dn++;}
    		}
    		else if(!a)//要有else!否则有可能会插入多次
    		{
    			if(!pn)//没有人
    			{
    				R=insert(x,R);
    				dn++;//直接插入,宠物数量+1
    			}
    			else if(!dn)//有人(没有狗),代表是“狗选人”
    			{
    				ans=(ans+abs(find(R,x)))%MOD;
    				dale(R,D);//删掉值为D的结点(这里既然找到了,肯定有此结点),虽然dale有返回值,但这里用不上
    				pn--;//人少了一个
    			}
    		}
    		else
    		{
    			if(!dn)
    			{
    				R=insert(x,R);
    				pn++;
    			}
    			else if(!pn)
    			{
    				ans=(ans+abs(find(R,x)))%MOD;
    				dale(R,D);
    				dn--;
    			}
    		}//与上面类似
    		//printf("----------------
    R=%d,D=%d,ans=%d
    ----------------
    ",R,D,ans);
    	}
        printf("%d",ans);
        return 0;
    }
    

                                                                                                                                                                  By WZY

  • 相关阅读:
    java.lang.IllegalStateException: Failed to load property source from location 'classpath:/application.yml'
    generate failed: Cannot resolve classpath entry: mysql-connector-java-5.1.38.jar
    Spring boot 零配置开发微服务
    【ABAP系列】SAP ABAP BAPI_REQUISITION_CREATE创建采购申请
    【ABAP系列】SAP ABAP 字符编码与解码、Unicode
    【ABAP系列】ABAP CL_ABAP_CONV_IN_CE
    【Fiori系列】浅谈SAP Fiori的设计美感与发展历程
    【Fiori系列】为什么SAP Fiori活的如此精致
    【ABAP系列】SAP ABAP下载带密码的Excel文件
    【ABAP系列】SAP ABAP 高级业务应用程序编程(ABAP)
  • 原文地址:https://www.cnblogs.com/LinqiongTaoist/p/7203734.html
Copyright © 2011-2022 走看看