zoukankan      html  css  js  c++  java
  • 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,
    最后一个领养者没有宠物可以领养。

    代码

    维护两颗平衡树,或是模拟即可。

    在宠物多的时候查询人,删除宠物

    人多的时候查询宠物,删除人

    #include<bits/stdc++.h>
    #define mod 1000000
    using namespace std;
    const int maxn=100000+100;
    const long long inf=1ll<<60;
    int ch[maxn][2],fa[maxn],size[maxn],cnt[maxn];
    long long val[maxn];
    int ncnt,root;
    long long ans=0;
    int tot;
    inline int read()
    {
        int x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
        return x*f; 
    }
    int chk(int u)
    {
        return ch[fa[u]][1]==u;
    }
    void pushup(int u)
    {
        size[u]=size[ch[u][0]]+size[ch[u][1]]+cnt[u];
    }
    void rotate(int u)
    {
        int f=fa[u],ff=fa[f],k=chk(u),s=ch[u][k^1];
        ch[f][k]=s,fa[s]=f;
        ch[ff][chk(f)]=u,fa[u]=ff;
        ch[u][k^1]=f,fa[f]=u;
        pushup(u),pushup(f);
    } 
    void splay(int u,int goal=0)
    {
        while(fa[u]!=goal)
        {
            int f=fa[u],ff=fa[f];
            if(ff!=goal)
            {
                if(chk(u)==chk(f))rotate(f);
                else rotate(u);
            }
            rotate(u);
        }
        if(!goal)root=u;
    }
    void insert(long long x)
    {
        int u=root,f=0;
        while(u&&val[u]!=x)
        f=u,u=ch[u][x>val[u]];
        if(u)cnt[u]++;
        else
        {
            u=++ncnt;
            if(f)ch[f][x>val[f]]=u;
            fa[u]=f,val[u]=x;
            size[u]=cnt[u]=1;
            ch[u][0]=ch[u][1]=0;
        }
        splay(u);
    }
    void find(int x)
    {
        int u=root;
        while(ch[u][x>val[u]]&&val[u]!=x)
        u=ch[u][x>val[u]];
        splay(u);
    } 
    int pre(long long x)
    {
        find(x);
        if(val[root]<x)return root;
        int u=ch[root][0];
        while(ch[u][1])u=ch[u][1];
        return u;
    }
    int succ(long long x)
    {
        find(x);
        if(val[root]>x)return root;
        int u=ch[root][1];
        while(ch[u][0])u=ch[u][0];
        return u;
    }
    void remove(int x)
    {
        int last=pre(x),next=succ(x);
        splay(last),splay(next,last);
        int u=ch[next][0];
        if(cnt[u]>1)cnt[u]--,splay(u);
        else ch[next][0]=0;
    }
    int main()
    { 
        insert(inf),insert(-inf);
        int n=read();
        for(int i=1;i<=n;i++)
        {
            int a=read(),b=read();
            if(tot>0)
            {
                if(a==0)
                insert(b);
                if(a==1)
                {
                    long long p=val[pre(b)],s=val[succ(b)];
                    ans+=min(abs(p-b),abs(s-b));
                    ans%=mod;
                    remove(abs(p-b)<=abs(s-b)?p:s);
                }    
            }
            else if(tot<0)
            {
                 if(a==1)
                insert(b);
                if(a==0)
                {
                    long long p=val[pre(b)],s=val[succ(b)];
                    ans+=min(abs(p-b),abs(s-b));
                    ans%=mod;
                    remove(abs(p-b)<=abs(s-b)?p:s);
                }    
            }
            else
            insert(b);    
            tot+=a==0?1:-1;
        }
        printf("%lld",ans);
        return 0;
    }
    View Code
  • 相关阅读:
    Golang :索引值对的数组
    MySql-BlackHole:黑洞引擎
    golang fmt 中的 Sprintf、Fprintf和 Printf函数
    golang 中的 rune 和 byte
    mysql 全文索引
    Python 原始字符串
    如何给博客园(或者CSDN)设置域名访问
    CPU、内存、磁盘三者的关系
    018.redis 阶段性总结:1T 以上海量数据+10 万以上 QPS 高并发+ 99.99% 高可用
    017.redis 在实践中的一些常见问题以及优化思路(包含 linux 内核参数优化)
  • 原文地址:https://www.cnblogs.com/DriverBen/p/10884586.html
Copyright © 2011-2022 走看看