zoukankan      html  css  js  c++  java
  • 【BZOJ-1208】宠物收养所 Splay

    1208: [HNOI2004]宠物收养所

    Time Limit: 10 Sec  Memory Limit: 162 MB
    Submit: 6638  Solved: 2601
    [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,最后一个领养者没有宠物可以领养)

    HINT

    Source

    Solution

    从数据范围及描述,很容易就想到是要用数据结构维护,支持前驱后继,插入和删除操作,很显然用Splay可以轻松解决

    需要维护 宠物 和 收养者,第一眼想到要建两棵Splay,一棵维护宠物一棵维护收养者,并对两个同时判断,完成删除

    但实际并不需要如此,只需要打个标记记录当前Splay所维护的东西,并分类讨论即可:

    如果当前读进来的和现在所维护的一致,加入Splay;否则判断,删除前驱或后继,记录答案;若当前Splay为空,加入,并更改标记

    Code

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    using namespace std;
    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*10+ch-'0'; ch=getchar();}
        return x*f;
    }
    #define maxn 100010
    int n,m;
        int fa[maxn],son[maxn][2],key[maxn],cnt[maxn],size[maxn];
        int sz,root;
        void clear(int now)
            {
                son[now][1]=son[now][0]=fa[now]=cnt[now]=key[now]=size[now];
            }
        int get(int now)
            {
                return son[fa[now]][1]==now;
            }
        void update(int now)
            {
                if (!now) return;
                size[now]=cnt[now];
                if (son[now][0]) size[now]+=size[son[now][0]];
                if (son[now][1]) size[now]+=size[son[now][1]];
            }
        void rotate(int now)
            {
                int old=fa[now],oldf=fa[old],which=get(now);
                son[old][which]=son[now][which^1]; fa[son[old][which]]=old;
                fa[old]=now; son[now][which^1]=old; fa[now]=oldf;
                if (oldf) son[oldf][son[oldf][1]==old]=now;
                update(old); update(now);
            }
        void splay(int now)
            {
                for (int f; (f=fa[now]); rotate(now))
                    if (fa[f]) 
                        if (get(now)==get(f)) 
                            rotate(f); else rotate(now);
                root=now;
            }
        void insert(int v)
            {
                if (!root) 
                    {sz++;son[sz][0]=son[sz][1]=fa[sz]=0;key[sz]=v;size[sz]=1;cnt[sz]=1;root=sz;return;}
                int now=root,f=0;
                while (true)
                    {
                        if (key[now]==v)
                            {cnt[now]++;update(now);update(f);splay(now);break;}
                        f=now; now=son[now][key[now]<v];
                        if (now==0)
                            {
                               sz++;son[sz][0]=son[sz][1]=0;key[sz]=v;size[sz]=1;
                               cnt[sz]=1;fa[sz]=f;son[f][key[f]<v]=sz;
                               update(f);splay(sz);break;
                            }
                    }
            }
        int find(int v)
            {
                int ans=0,now=root;
                while (true)
                    {
                        if (v<key[now]) now=son[now][0]; else
                            {
                                if (son[now][0]) ans+=size[son[now][0]];
                                if (v==key[now]) {splay(now); return ans+1;}
                                ans+=cnt[now]; now=son[now][1];
                            }
                    }
            }
        int prev()
            {
                int now=son[root][0];
                while (son[now][1]) now=son[now][1];
                return now;
            }
        int succ()
            {
                int now=son[root][1];
                while (son[now][0]) now=son[now][0];
                return now;
            }
        bool remove(int now)
            {
                if (find(now)==-1) return false;
                if (cnt[root]>1) {cnt[root]--; return true;};
                if (!son[root][0] && !son[root][1]) {clear(root); root=0; return true;}
                if (!son[root][0])
                    {
                        int oldroot=root; root=son[root][1]; fa[root]=0; clear(oldroot); return true;
                    }
                else if (!son[root][1])
                    {
                        int oldroot=root; root=son[root][0]; fa[root]=0; clear(oldroot); return true;
                    }
                int leftbig=prev(),oldroot=root;
                splay(leftbig);
                fa[son[oldroot][1]]=root; son[root][1]=son[oldroot][1];
                clear(oldroot); update(root); return true;
            }
    #define mod 1000000
    int main()
    {
        n=read(); int pd=0; int ans=0;
        for (int i=1; i<=n; i++)
            {
                int opt=read(),x=read();
                if (root==0) {insert(x);pd=opt;continue;}
                if (pd==opt) {insert(x);continue;}
                if (pd!=opt)
                    {
                        insert(x); int pre=key[prev()],suc=key[succ()];
                        if (pre==0) {ans=(ans+abs(suc-x))%mod;remove(x);remove(suc);continue;}
                        if (suc==0) {ans=(ans+abs(pre-x))%mod;remove(x);remove(pre);continue;}
                        if (abs(x-pre)<=abs(x-suc)) {ans=(ans+abs(pre-x));remove(x);remove(pre);continue;}
                        if (abs(x-pre)>abs(x-suc)) {ans=(ans+abs(suc-x));remove(x);remove(suc);continue;}
                    }
            }
        while (ans<0) ans+=mod;
        printf("%d
    ",ans);
        return 0;
    }
  • 相关阅读:
    ajax遍历数组(实现百度搜索提示的效果)
    角色管理的增删改
    String与Date、Timestamp互转
    jquery
    认识拦截器
    验证表单介绍
    什么是struts2?
    如何理解OOP?
    请描述Java中的时间监听机制?
    hibernate3中session.get()与session.load()两个方法的区别?
  • 原文地址:https://www.cnblogs.com/DaD3zZ-Beyonder/p/5335651.html
Copyright © 2011-2022 走看看