luogu2286宠物收养场

来源:互联网 发布:苹果手机qq软件 编辑:程序博客网 时间:2024/04/28 08:34

题目描述

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

每个领养者都希望领养到自己满意的宠物,凡凡根据领养者的要求通过他自己发明的一个特殊的公式,得出该领养者希望领养的宠物的特点值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表示splay中当前记录的是人还是宠物的信息
如果类别相同,就insert,否则就进行领养
领养的过程还是比较好实现的:
假设当前值是w,我们insert(w),之后寻找他的前驱后继,
选择差值小的(若差值相同,就选编号小的)进行领养,
完成操作后,记得把w从splay中删除
这里有一个很需要注意的问题:
在找前驱和后继时,如果ta是第一个(最后一个),ta就没有前驱(后继),
这时候直接返回0,在计算差值时,赋成INF,消除影响
ps.因为这个玄学的操作,我搞了一下午,不加这个特判的话,
就会T(???),竟然不是WA
大神给的解释是:如果不特判,程序也许会默认的进行logN的查找甚至是死循环,所以就T掉了。。。。

这里写代码片#include<cstdio>#include<cstring>#include<iostream>#include<cmath>using namespace std;const int mod=1000000;const int N=80100;const int INF=100000010;int ch[N][2],pre[N],size[N],v[N];int root=0,top=0;int n,ans=0,flag;int get(int x){    return (ch[pre[x]][0]==x?0:1);}int abs(int x){    return (x>=0 ? x:-x);}void clean(int bh){    v[bh]=size[bh]=ch[bh][0]=ch[bh][1]=pre[bh]=0;}void update(int bh){    size[bh]=1;    if (ch[bh][0]) size[bh]+=size[ch[bh][0]];    if (ch[bh][1]) size[bh]+=size[ch[bh][1]];}void rotate(int bh){    int fa=pre[bh];    int grand=pre[fa];    int wh=get(bh);    ch[fa][wh]=ch[bh][wh^1];    pre[ch[fa][wh]]=fa;    ch[bh][wh^1]=fa;    pre[fa]=bh;    pre[bh]=grand;    if (grand)       ch[grand][ch[grand][0]==fa ? 0:1]=bh;    update(fa);    update(bh);    return;}void splay(int bh){    for (int fa;fa=pre[bh];rotate(bh))        if (pre[fa])           rotate(get(bh)==get(fa) ? fa:bh);    root=bh;    return;}void insert(int x){    int fa=0,now=root;    if (!root)    {        top++;        v[top]=x;        size[top]=1;        ch[top][0]=ch[top][1]=0;        pre[top]=0;        root=top;        return;    }    while (1){        if (v[now]==x)        {            update(now);            update(fa);            splay(now);            return;        }        fa=now;        now=ch[now][v[now]<x];        if (!now)        {            top++;            v[top]=x;            size[top]=1;            ch[top][0]=ch[top][1]=0;            pre[top]=fa;            ch[fa][v[fa]<x]=top;            update(fa);            splay(top);  //            return;        }    }}void find(int x)  //查找值 {    int now=root;    while (1){        if (v[now]>x) now=ch[now][0];        else{            if (v[now]==x)            {                splay(now);                return;            }            now=ch[now][1];        }    }}int qian(int x){    int now=ch[root][0];  //now=ch[root][0]; 避免x是最小的值     while (ch[now][1]) now=ch[now][1];    return v[now];}int hou(int x){    int now=ch[root][1];    while (ch[now][0]) now=ch[now][0];    return v[now];}int fro(){    int now=root;    if (ch[now][0]) now=ch[now][0];    while (ch[now][1]) now=ch[now][1];    return now;}void del(int x){    find(x);    if (!ch[root][1]&&!ch[root][0])    {        clean(root);        root=0;        return;    }    if (!ch[root][1])    {        int k=root;        root=ch[root][0];        pre[root]=0;        clean(k);        return;    }    if (!ch[root][0])    {        int k=root;        root=ch[root][1];        pre[root]=0;        clean(k);        return;    }    int k=root;    int wh=fro();    splay(wh);    ch[wh][1]=ch[k][1];    pre[ch[k][1]]=root;    update(root);    clean(k);    return;}int main(){    scanf("%d",&n);    int tot=0;    for (int i=1;i<=n;i++)    {        int u,w;        scanf("%d%d",&u,&w);        if (i==1) flag=u;        if (flag==u) insert(w),tot++;        else{            if (tot==0) {                flag=u;                insert(w);                tot++;                continue;            }            insert(w);            int r1=qian(w);            int r2=hou(w);            int p1,p2;            if (r1) p1=abs(r1-w);  //如果w是最小值,r1=0             else p1=INF;            if (r2) p2=abs(r2-w);  //如果w是最大值,r1=0             else p2=INF;                                                                                                                                                                                                                                                                                                                                                                             if (p1<=p2)//存在两个领养者他们希望领养宠物的特点值分别为a-b和a+b,            //那么特点值为a-b的那个领养者将成功领养该宠物            {  //p1代表的就是编号较小的                 del(r1);                ans+=p1; ans%=mod;            }            else{                del(r2);                ans+=p2; ans%=mod;            }            del(w);            tot--;        }    }    printf("%d",ans);    return 0;}
原创粉丝点击