最近,阿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
#----------------------------------------------------------------------------------------------#
里面有AVL详解
不要以为此题要2棵树,只要1棵树:在同一时间里,只会有人或宠物之一在收养所。
所以,只需要看一下现在是狗在树(收养所)里还是人。
代码:
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
#define MAXN 100000
#define MOD 1000000
struct node
{
int num;
int l,r,h;
}tree[MAXN+5];
int N,R;
int cnt;
bool F;
void read(int &x)
{
int f=1; x=0; char s=getchar();
while(s<'0'||s>'9'){if(s=='-') f=-1;s=getchar();}
while(s>='0'&&s<='9'){x=x*10+s-'0';s=getchar();}
x*=f;
}
int zig(int x)
{
int t=tree[x].l;
tree[x].l=tree[t].r;
tree[t].r=x;
tree[x].h=max(tree[tree[x].r].h,tree[tree[x].l].h)+1;
tree[t].h=max(tree[tree[t].r].h,tree[tree[t].l].h)+1;
return t;
}
int zag(int x)
{
int t=tree[x].r;
tree[x].r=tree[t].l;
tree[t].l=x;
tree[x].h=max(tree[tree[x].r].h,tree[tree[x].l].h)+1;
tree[t].h=max(tree[tree[t].r].h,tree[tree[t].l].h)+1;
return t;
}
int zigzag(int x)
{
tree[x].r=zig(tree[x].r);
return zag(x);
}
int zagzig(int x)
{
tree[x].l=zag(tree[x].l);
return zig(x);
}
void maintain(int &root)
{
if(tree[tree[root].l].h==tree[tree[root].r].h+2)
{
int t=tree[root].l;
if(tree[tree[t].l].h==tree[tree[root].r].h+1)
root=zig(root);
else if(tree[tree[t].r].h==tree[tree[root].r].h+1)
{
tree[root].l=zag(tree[root].l);
root=zig(root);
}
}
else if(tree[tree[root].l].h+2==tree[tree[root].r].h)
{
int t=tree[root].r;
if(tree[tree[t].r].h==tree[tree[root].l].h+1)
root=zag(root);
else if(tree[tree[t].l].h==tree[tree[root].l].h+1)
{
tree[root].r=zig(tree[root].r);
root=zag(root);
}
}
tree[root].h=max(tree[tree[root].l].h,tree[tree[root].r].h)+1;
}
int insert(int x,int root)
{
if(!root)
{
tree[++cnt].num=x;
tree[cnt].h=1;
return cnt;
}
if(x<tree[root].num)
{
tree[root].l=insert(x,tree[root].l);
if(tree[tree[root].l].h==tree[tree[root].r].h+2)
{
if(tree[tree[root].l].num>x) root=zig(root);
else if(tree[tree[root].l].num<x) root=zagzig(root);
}
}
else if(x>tree[root].num)
{
tree[root].r=insert(x,tree[root].r);
if(tree[tree[root].r].h==tree[tree[root].l].h+2)
{
if(tree[tree[root].r].num<x) root=zag(root);
else if(tree[tree[root].r].num>x) root=zigzag(root);
}
}
tree[root].h=max(tree[tree[root].l].h,tree[tree[root].r].h)+1;
return root;
}
int dale(int &root,int x)
{
int t;
if(x==tree[root].num||(x<tree[root].num&&tree[root].l==0)||(x>tree[root].num&&tree[root].r==0))
{
if(tree[root].l==0||tree[root].r==0)
{
t=tree[root].num;
root=tree[root].l+tree[root].r;
return t;
}
else
tree[root].num=dale(tree[root].l,x);
}
else
{
if(x<tree[root].num) t=dale(tree[root].l,x);
else t=dale(tree[root].r,x);
}
maintain(root);
return t;
}
int D;
int find(int root,int x)
{
if(!root) return 0x7fffffff;
if(x==tree[root].num)
{
D=tree[root].num;
return 0;
}
if(x>tree[root].num)
{
int t=find(tree[root].r,x);
if(x-tree[root].num<=t)
{
D=tree[root].num;
return x-tree[root].num;
}
else return t;
}
if(x<tree[root].num)
{
int t=find(tree[root].l,x);
if(tree[root].num-x<t)
{
D=tree[root].num;
return tree[root].num-x;
}
else return t;
}
}
int main()
{
int a,x,ans=0;
int pn=0,dn=0;
read(N);
while(N--)
{
read(a);read(x);
if(!pn&&!dn)
{
if(a){R=insert(x,R);pn++;}
else{R=insert(x,R);dn++;}
}
else if(!a)
{
if(!pn)
{
R=insert(x,R);
dn++;
}
else if(!dn)
{
ans=(ans+abs(find(R,x)))%MOD;
dale(R,D);
pn--;
}
}
else
{
if(!dn)
{
R=insert(x,R);
pn++;
}
else if(!pn)
{
ans=(ans+abs(find(R,x)))%MOD;
dale(R,D);
dn--;
}
}
}
printf("%d",ans);
return 0;
}
By WZY
相关知识
算法:什么是宠物收养所问题?
springboot毕设基于协同过滤算法的宠物收养系统论文+程序+部署
【HNOI2004】宠物收养所
东莞宠物收养所
宠物收养所(宠物收养所在哪里)
宠物收养所题解
宠物收养所(c++)
1566:宠物收养所
宠物收养所 (SBT)
[BZOJ1208]宠物收养所(Splay)
网址: 宠物收养所算法解析 https://m.mcbbbk.com/newsview914691.html
上一篇: 「图」上海虹口宠物收养收容救助站 |
下一篇: 「图」宝山区淞南镇宠物收容中心基 |