zoukankan      html  css  js  c++  java
  • HNOI2004 宠物收养所 (Treap)

    1285 宠物收养所

    http://codevs.cn/problem/1285/

     时间限制: 1 s
     空间限制: 128000 KB
     
     
    题目描述 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 Description

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

    输出描述 Output Description

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

    样例输入 Sample Input

    5

    0 2

    0 4

    1 3

    1 2

    1 5

    样例输出 Sample Output

    3

    数据范围及提示 Data Size & Hint

    (abs(3-2) + abs(2-4)=3,最后一个领养者没有宠物可以领养)

    用这题练Treap

    插入、删除、前驱、后继 

    #include<cstdio>
    #include<algorithm>
    #define mod  1000000
    using namespace std;
    struct node
    {
        int key,cnt,fix;
        node *lc,*rc;
    }*root,*null;
    int sum[2],ans;
    struct TREAP
    {
        TREAP()
        {
            null= new node;
            null->lc=NULL;
            null->rc=NULL;
            root=null;
            root->fix=0x7fffffff;
        }
        void rotate_right(node *&now)
        {
            node *tmp = now->lc;
            now->lc = tmp->rc;
            tmp->rc = now;
            now=tmp;
        }
        void rotate_left(node *&now)
        {
            node *tmp = now->rc;
            now->rc = tmp->lc;
            tmp->lc = now;
            now=tmp;
        }
        void insert(node *&now,int x)
        {
            if(now==null)
            {
                now= new node;
                now->cnt=1;
                now->key=x;
                now->lc=null;
                now->rc=null;
                now->fix=rand();
            }
            else if(now->key > x)
            {
                insert(now->lc,x);
                if( now->lc->fix < now->fix ) rotate_right(now); 
            }
            else
            {
                insert(now->rc,x);
                if(now->rc->fix < now->fix) rotate_left(now);
            }
        }
        int find_pre(int k)
        {
            int tmp=-1;
            node *now=root;
            while(now!=null)
            {
                if(now-> key <=k) tmp=now->key,now=now->rc;
                else now=now->lc;
            }
            return tmp;
        }
        int find_suf(int k)
        {
            int tmp=-1;
            node *now = root;
            while(now!=null)
            {
                if(now->key >=k) tmp=now->key,now=now->lc;
                else now=now->rc;
            }
            return tmp;
        }
        void del(node *&now,int x)
        {
            if(now->key==x)
            {
                if(now->lc != null && now->rc != null)
                {
                    if(now->lc->fix < now->rc->fix) 
                    {
                        rotate_right(now);
                        del(now->rc,x);
                    } 
                    else
                    {
                        rotate_left(now);
                        del(now->lc,x);
                    } 
                }
                else
                {
                    node *tmp=now;
                    if(now->lc == null) now = now->rc;
                    else now = now->lc;
                    delete(tmp);
                }
            }
            else if(now->key < x) del(now->rc,x);
            else del(now->lc,x);
        }
    }Treap;
    int main()
    {
        int n,a,b,pre,suf;
        scanf("%d",&n);
        while(n--)
        {
            scanf("%d%d",&a,&b);
            if(!sum[!a])  Treap.insert(root,b),sum[a]++;
            else
            {
                sum[!a]--;
                pre=Treap.find_pre(b);
                suf=Treap.find_suf(b);
                if(pre==-1) 
                {
                    ans=(ans+abs(suf-b))%mod;
                    Treap.del(root,suf);
                }
                else if(suf==-1) 
                {
                    ans=(ans+abs(pre-b))%mod;
                    Treap.del(root,pre);
                }
                else 
                {
                    if(abs(pre-b)<=abs(suf-b))
                    {
                        ans=(ans+abs(pre-b))%mod;
                        Treap.del(root,pre);
                    }
                    else
                    {
                        ans=(ans+abs(suf-b))%mod;
                        Treap.del(root,suf);
                    }
                }
            }
        }
        printf("%d
    ",ans);
    }
  • 相关阅读:
    C#事件和委托的区别
    已知有个rand7()的函数,返回1到7随机自然数,让利用这个rand7()构造rand10()随机1~10
    如何搭建github+hexo博客-转
    ngRouter和ui-router区别
    JS数组追加数组采用push.apply的坑(转)
    vue中关于computed的一点理解
    simplify the life ECMAScript 5(ES5)中bind方法简介
    github使用-知乎的某小姐的一篇文章
    Jade 模板引擎使用
    玩转Nodejs日志管理log4js(转)
  • 原文地址:https://www.cnblogs.com/TheRoadToTheGold/p/6866648.html
Copyright © 2011-2022 走看看