Codevs 1285 宠物收养所

本文介绍了一道关于宠物收养所的算法题,通过维护一棵平衡二叉搜索树来高效匹配领养者与宠物,旨在最小化领养过程中的不满意程度。
1285 宠物收养所
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond
题目描述 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 Description
第一行为一个正整数n,n<=80000,表示一年当中来到收养所的宠物和领养者的总数。接下来的n行,按到来时间的先后顺序描述了一年当中来到收养所的宠物和领养者的情况。每行有两个正整数a, b,其中a=0表示宠物,a=1表示领养者,b表示宠物的特点值或是领养者希望领养宠物的特点值。(同一时间呆在收养所中的,要么全是宠物,要么全是领养者,这些宠物和领养者的个数不会超过10000个)
输出描述 Output Description
仅有一个正整数,表示一年当中所有收养了宠物的领养者的不满意程度的总和mod 1000000以后的结果。
样例输入 Sample Input
5
0 2
0 4
1 3
1 2
1 5
样例输出 Sample Output
3
数据范围及提示 Data Size & Hint

(abs(3-2) + abs(2-4)=3,最后一个领养者没有宠物可以领养)


#include<cstring>
#include<cstdio>
#include<cmath>
#include<ctime>
#include<iostream>
#include<cstdlib>
using namespace std;
typedef long long LL;
#define Mod 1000000
//long long fabs(long long x){
//	if(x<0)return -x;
//	return x;
//}
struct node{
	node *son[2];
	int fix;LL val;
	int cmp(int x){
		if(val==x)return -1;
		return (x<val?0:1);
	}
};
node* newnode(LL x){
	node *cur=new node;
	cur->val=x;cur->fix=rand();
	cur->son[1]=cur->son[0]=NULL;
	return cur;
}
void rotate(node* &cur,int p){
	node *pp=cur->son[p^1];
	cur->son[p^1]=pp->son[p];
	pp->son[p]=cur;
	cur=pp;
}
void Insert(node* &cur,LL x){
	if(!cur)cur=newnode(x);
	else {
		int bj=cur->val < x;
		Insert(cur->son[bj],x);
		if(cur->son[bj]->fix > cur->fix)
			rotate(cur,!bj);
	}
}

node* lower(node *cur,LL x){
	int d=cur->cmp(x);
	if(d<0)return cur;
	if(cur->son[d]==NULL){
		if(d==0) return NULL;
		if(d==1) return cur;
	}
	node *p=lower(cur->son[d],x);
	if(p==NULL&&cur->val < x) return cur;
	return p;
}
node* upper(node *cur,LL x){
	int d=cur->cmp(x);
	if(d<0)return cur;
	if(cur->son[d]==NULL){
		if(d==0) return cur;
		if(d==1) return NULL;
	}
	node *p=upper(cur->son[d],x);
	if(p==NULL&&cur->val > x) return cur;
	return p;
}
void t_erase(node* &o) {
	node *p=o;
	if(o->son[0]==NULL) o=o->son[1],delete p;
	else if(o->son[1]==NULL) o=o->son[0],delete p;
	else {
		int d=(o->son[0]->fix > o->son[0]->fix?1:0);
		rotate(o,d);
		t_erase(o->son[d]);
	}
}
void t_erase(node* &cur,LL v) { //先确认有v,用 t_find()
	int p=cur->cmp(v);
	if(p<0) t_erase(cur);
	else t_erase(cur->son[p],v);
}
int main(){
	srand(time(NULL));
	node *root=NULL,*p1,*p2;
	int n,a,sss;
	LL b,x,d,ans=0;
	scanf("%d",&n);
	while(n--){
		scanf("%d %lld",&a,&b);
		if(root==NULL){
			Insert(root,b);
			sss=a;
		}
		else if(sss==a)Insert(root,b);
		else{
			p1=lower(root,b);
			p2=upper(root,b);
			if(p1==NULL){
				ans+=fabs(b- p2->val);
				ans%=Mod;
				t_erase(root,p2->val);
			}
			else if(p2==NULL) {
				ans+=fabs(b- p1->val);
				ans%=1000000;
				t_erase(root,p1->val);
			}
			else {
				x=fabs(b- p1->val),d=fabs(b- p2->val);
				if(d<x) {
					ans+=d;
					ans%=1000000;
					t_erase(root,p2->val);
				}
				else {
					ans+=x;
					ans%=1000000;
					t_erase(root,p1->val);
				}
			}
		}
	}
	cout<<ans;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

七情六欲·

学生党不容易~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值