AVL练习题——宠物收养所

题目描述

最近,阿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)。【任务描述】你得到了一年当中,领养者和被收养宠物到来收养所的情况,希望你计算所有收养了宠物的领养者的不满意程度的总和。这一年初始时,收养所里面既没有宠物,也没有领养者。

输入

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

输出

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

样例输入

5
0 2
0 4
1 3
1 2
1 5

样例输出

3

几乎全裸的平衡树题,由题意可得要么收养所里全是狗等人来领,要么全是人等狗来领……(好像哪里不对……)
有童鞋想建两棵树,一棵装人,一棵装狗,完全没有必要,新建一个bool变量p,如果p=0,指树里边全是狗,p=1,指树里边全是人,如果输入的a不等于p,则删除树中与b最接近的节点,反之,则插入b到树中

代码如下:
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int abs(int x){return x>0?x:-x;}
int max(int x,int y){return x>y?x:y;}
int min(int x,int y){return x<y?x:y;}
struct ill{
    int t;
    int lc,rc;
    int h;
}a[80001];
int p=-1,g;
int s,mi;
int zig(int r)
{
    int t=a[r].lc;
    a[r].lc=a[t].rc;
    a[t].rc=r;
    a[r].h=max(a[a[r].rc].h,a[a[r].lc].h)+1;
    a[t].h=max(a[a[t].rc].h,a[a[t].lc].h)+1;
    return t;
}
int zag(int r)
{
    int t=a[r].rc;
    a[r].rc=a[t].lc;
    a[t].lc=r;
    a[r].h=max(a[a[r].rc].h,a[a[r].lc].h)+1;
    a[t].h=max(a[a[t].rc].h,a[a[t].lc].h)+1;
    return t;
}
int zigzag(int r)
{
    a[r].rc=zig(a[r].rc);
    return zag(r);
}
int zagzig(int r)
{
   a[r].lc=zag(a[r].lc);
   return zig(r);
}
int getint()
{
    char c;int flag=1,num=0;
    while((c=getchar())<'0'||c>'9')if(c=='-')flag=-1;
    while(c>='0'&&c<='9'){num=num*10+c-48;c=getchar();}
    return num*=flag;
}
void move(int &root)
{
    if(a[a[root].lc].h==a[a[root].rc].h+2)
    {
        int t=a[root].lc;
        if(a[a[t].lc].h>a[a[t].rc].h)root=zig(root);
        else root=zagzig(root);
    }
    if(a[a[root].lc].h+2==a[a[root].rc].h)
    {
        int t=a[root].rc;
        if(a[a[t].lc].h<a[a[t].rc].h)root=zag(root);
        else root=zigzag(root);
    }
    a[root].h=max(a[a[root].lc].h,a[a[root].rc].h)+1;
}
int insert(int root,int x)
{
    if(root==0)
    {
        a[++g].t=x;
        root=g;
        return root;
    }
    if(x<a[root].t)a[root].lc=insert(a[root].lc,x);
    else a[root].rc=insert(a[root].rc,x);
    move(root);
    return root;
}
void find(int root,int x)
{
    if(root==0)return;
    if(abs(a[root].t-x)<mi)
    {
        mi=abs(a[root].t-x);
        s=a[root].t;
    }
    if(x<a[root].t)find(a[root].lc,x);
    else find(a[root].rc,x);
}
int dele(int &root,int x)
{
    int tx;
    if(x==a[root].t||(x<a[root].t&&a[root].lc==0)||(x>a[root].t&&a[root].rc==0))
    {
        if(a[root].lc==0||a[root].rc==0)
        {
            tx=a[root].t;
            root=a[root].lc+a[root].rc;
            return tx;
        }
        else a[root].t=dele(a[root].lc,x);
    }
    else
    {
        if(x<a[root].t)tx=dele(a[root].lc,x);
        else tx=dele(a[root].rc,x);
    }
    move(root);
    return tx;
}
int n,k;
int main()
{
    int i,x,z,root=0;
    n=getint();
    for(i=1;i<=n;i++)
    {
        z=getint(),x=getint();
        if(!root||z==p)
        {
            root=insert(root,x);
            p=z;
            continue;
        }
        else
        {
            mi=1<<30;
            find(root,x);
            k+=abs(x-s);
            k%=1000000;
            dele(root,s);
        }
    }
    printf("%d",k);
}


原文地址:https://www.cnblogs.com/Darknesses/p/12002555.html