zoukankan      html  css  js  c++  java
  • P2286 [HNOI2004]宠物收养场

    P2286 [HNOI2004]宠物收养场

    题目描述

    凡凡开了一间宠物收养场。收养场提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物。

    每个领养者都希望领养到自己满意的宠物,凡凡根据领养者的要求通过他自己发明的一个特殊的公式,得出该领养者希望领养的宠物的特点值a(a是一个正整数,a<2^31),而他也给每个处在收养场的宠物一个特点值。这样他就能够很方便的处理整个领养宠物的过程了,宠物收养场总是会有两种情况发生:被遗弃的宠物过多或者是想要收养宠物的人太多,而宠物太少。

    被遗弃的宠物过多时,假若到来一个领养者,这个领养者希望领养的宠物的特点值为a,那么它将会领养一只目前未被领养的宠物中特点值最接近a的一只宠物。(任何两只宠物的特点值都不可能是相同的,任何两个领养者的希望领养宠物的特点值也不可能是一样的)如果有两只满足要求的宠物,即存在两只宠物他们的特点值分别为a-b和a+b,那么领养者将会领养特点值为a-b的那只宠物。

    收养宠物的人过多,假若到来一只被收养的宠物,那么哪个领养者能够领养它呢?能够领养它的领养者,是那个希望被领养宠物的特点值最接近该宠物特点值的领养者,如果该宠物的特点值为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以后的结果。

    输入输出样例

    输入样例#1:
    5                  
    0 2                      
    0 4                         
    1 3
    1 2
    1 5
    
    输出样例#1:
    3
    注:abs(3-2) + abs(2-4)=3,
    最后一个领养者没有宠物可以领养。

    /*
    维护splay,每次来flag判断是宠物还是人
    如果当前root为0就加入splay,否则查前驱后继
    注意是一个一个来的!! 
    */
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<cstdlib>
    
    #define N 800005
    #define mod 1000000
    
    using namespace std;
    int siz[N],f[N],key[N],a[N],b[N],ch[N][2],cnt[N],flag;
    int n,m,x,y,ans,cnt1,cnt2,mn,sz,root;
    
    inline int read()
    {
        int x=0,f=1;char c=getchar();
        while(c>'9'||c<'0'){if(c=='-')f=-1;c=getchar();}
        while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
        return x*f;
    }
    void update(int x)
    {
        siz[x]=cnt[x];
        if(ch[x][0]) siz[x]+=siz[ch[x][0]];
        if(ch[x][1]) siz[x]+=siz[ch[x][1]];
    }
    
    int pre()
    {
        int now=ch[root][0];
        while(ch[now][1]) now=ch[now][1];return now;
    }
    
    int nex()
    {
        int now=ch[root][1];
        while(ch[now][0]) now=ch[now][0];return now;
    }
    
    int getson(int x)
    {
        return ch[f[x]][1]==x;
    }
    
    void rorate(int x)
    {
        int fa=f[x],ffa=f[fa],k=getson(x);
        ch[fa][k]=ch[x][k^1];f[ch[fa][k]]=fa;
        ch[x][k^1]=fa;f[fa]=x;f[x]=ffa;
        if(ffa)ch[ffa][ch[ffa][1]==fa]=x;
        update(fa);update(x);
    }
    
    void splay(int x)
    {
        for(int fa;fa=f[x];rorate(x))
          if(f[fa]) rorate(getson(x)==getson(fa)?fa:x);
        root=x;
    }
    
    int findpos(int x)
    {
        int now=root,ans=0;
        while(1)
        {
            if(x<key[now]) now=ch[now][0];
            else 
            {
                ans+=ch[now][0]?siz[ch[now][0]]:0;
                if(x==key[now])
                {
                    splay(now);return ans+1;
                }
                ans+=cnt[now];now=ch[now][1];
            }
        }
    }
    
    void clear(int x)
    {
        siz[x]=cnt[x]=f[x]=key[x]=ch[x][0]=ch[x][1]=0;
    }
    
    void creat(int x)
    {
        sz=sz+1;ch[sz][0]=ch[sz][1]=f[sz]=0;
        key[sz]=x;cnt[sz]=siz[sz]=1;
    }
    
    void insert(int x)
    {
        if(!root) creat(x),root=sz;
        else
        {
            int now=root,fa=0;
            while(1)
            {
                if(key[now]==x)
                {
                    cnt[now]++;siz[now]++;splay(now);
                    break;
                }
                fa=now;now=ch[fa][x>key[fa]];
                if(!now)
                {
                    creat(x);f[sz]=fa;ch[fa][x>key[fa]]=sz;
                    splay(sz);break;
                }
            }
        }
    }
    
    void del(int x)
    {
        int t=findpos(x);
        if(cnt[root]>1)
        {
            cnt[root]--;siz[root]--;return;
        }
        if(!ch[root][0]&&!ch[root][1])
        {
            clear(root);root=0;return;
        }
        if(!ch[root][0])
        {
            int tmp=root;root=ch[root][1];f[root]=0;
            clear(tmp);return;
        }
        if(!ch[root][1])
        {
            int tmp=root;root=ch[root][0];f[root]=0;
            clear(tmp);return;
        }
        int pre1=pre(),tmp=root;splay(pre1);
        ch[root][1]=ch[tmp][1];f[ch[tmp][1]]=root;
        clear(tmp);update(root);
    }
    
    int main()
    {
        n=read();
        for(int i=1;i<=n;i++)
        {
            x=read();y=read();
            if(!root) flag=x;
            if(flag==x) insert(y);
            else
            {
                insert(y);int p1=pre(),p2=nex(),p;del(y);
                if(!p2||p1&&y-key[p1]<=key[p2]-y) p=p1;
                else p=p2;
                ans=(ans+abs(key[p]-y))%mod;del(key[p]);
            }
        }
        printf("%d
    ",ans);
        return 0;
    }


    折花枝,恨花枝,准拟花开人共卮,开时人去时。 怕相思,已相思,轮到相思没处辞,眉间露一丝。
  • 相关阅读:
    String的方法capitalize
    python基本运算符
    计算机中常用进制转换
    python中的print函数
    python转义字符
    3.python中的基本概念
    4.input()
    1.计算机基础知识
    Pyhton实用的format()格式化函数
    jieba(结巴)常用方法
  • 原文地址:https://www.cnblogs.com/L-Memory/p/6972229.html
Copyright © 2011-2022 走看看