zoukankan      html  css  js  c++  java
  • [HNOI2004]宠物收养所

    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

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


    我们发现,如果有堆积,那么要么全是宠物,要么全是领养者。那么我们可以记录一下当前splay中是宠物还是领养者,每次往splay中插入一个不同的东西后,就弹出两个节点(领养者和宠物)

    关于本人splay代码操作详解请参考浅谈算法——splay

    #include<cmath>
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #define inf 0x7f7f7f7f
    using namespace std;
    typedef long long ll;
    typedef unsigned int ui;
    typedef unsigned long long ull;
    inline int read(){
    	int x=0,f=1;char ch=getchar();
    	for (;ch<'0'||ch>'9';ch=getchar())	if (ch=='-')    f=-1;
    	for (;ch>='0'&&ch<='9';ch=getchar())	x=(x<<1)+(x<<3)+ch-'0';
    	return x*f;
    }
    inline void print(int x){
    	if (x>=10)     print(x/10);
    	putchar(x%10+'0');
    }
    const int N=8e4,p=1e6;
    int ans;
    struct Splay{
    	#define T(x) (tree[f[x]][1]==x)
    	int tree[N+10][2],f[N+10],val[N+10];
    	int root,len,Tree;
    	void move(int x){
    		int fa=f[x],son=tree[x][T(x)^1];
    		tree[x][T(x)^1]=fa;
    		tree[fa][T(x)]=son;
    		if (son)	f[son]=fa;
    		f[x]=f[fa];
    		if (f[x])	tree[f[x]][T(fa)]=x;
    		f[fa]=x;
    	}
    	void splay(int x){
    		while (f[x]){
    			if (f[f[x]])	T(x)==T(f[x])?move(f[x]):move(x);
    			move(x);
    		}
    		root=x;
    	}
    	int get_pre(){
    		int x=tree[root][0];
    		while (tree[x][1])	x=tree[x][1];
    		return x;
    	}
    	int get_suc(){
    		int x=tree[root][1];
    		while (tree[x][0])	x=tree[x][0];
    		return x;
    	}
    	void Del(int x){
    		splay(x);
    		if (!(tree[x][0]&&tree[x][1])){
    			root=tree[x][0]+tree[x][1];
    			f[root]=0;
    			return;
    		}
    		int i=get_pre();
    		splay(i);
    		f[tree[x][1]]=i;
    		tree[i][1]=tree[x][1];
    	}
    	void get(){//用前驱或后继找到另一个匹配
    		int ans1=get_pre(),ans2=get_suc();
    		if (ans1&&ans2){
    			int x=val[root]-val[ans1],y=val[ans2]-val[root];
    			if (x<=y)	ans=(ans+x)%p,Del(root),Del(ans1);
    			else	ans=(ans+y)%p,Del(root),Del(ans2);
    			return;
    		}
    		ans=(ans+abs(val[root]-val[ans1]-val[ans2]))%p;
    		Del(root),Del(ans1+ans2);
    	}
    	void insert(int x,int flag){//flag记录是宠物还是领养者
    		val[++len]=x;
    		if (!root){
    			root=len,Tree=flag;
    			return;
    		}
    		int i=root;
    		while (1){
    			if (x<=val[i]){
    				if (!tree[i][0]){
    					tree[i][0]=len;
    					f[len]=i;
    					break;
    				}i=tree[i][0];
    			}else{
    				if (!tree[i][1]){
    					tree[i][1]=len;
    					f[len]=i;
    					break;
    				}i=tree[i][1];
    			}
    		}
    		splay(len);
    		if (flag!=Tree)	get();//弹出一对节点
    	}
    }T;
    int main(){
    	int n=read();
    	for (int i=1,x,y;i<=n;i++)	x=read(),y=read(),T.insert(y,x);
    	printf("%d
    ",ans);
    	return 0;
    }
    
  • 相关阅读:
    <Android 应用 之路> 天气预报(五)
    Java图形界面开发—列出指定目录
    解决The Network Adapter could not establish the connection
    <Android 应用 之路> 天气预报(四)
    Java集合框架—List
    Java集合框架—Map
    C#工程缺少IIS组件无法打开的解决办法
    关于com工程依赖的一些总结
    C:移位运算符
    void类型及void指针
  • 原文地址:https://www.cnblogs.com/Wolfycz/p/9475645.html
Copyright © 2011-2022 走看看