• [bzoj1208][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模板

    #include<cmath>
    #include<cstdio>
    #include<iostream>
    #define MOD 1000000
    using namespace std;
    
    template<typename integer>
    inline void readint(integer &x)
    {
    	integer num = 0, fix = 1;
    	char ch = getchar();
    
    	for(; !isdigit(ch); ch = getchar())
    	  if(ch == '-') fix = -1;
    	for(; isdigit(ch); ch = getchar())
    	  num = (num << 3) + (num << 1) + (ch ^ '0');
    
    	x = num * fix;
    	return;
    }
    
    template<typename Comparable>
    class SplayTree
    {
    	private:
    		struct BinaryNode
    		{
    			Comparable element;
    			BinaryNode * right;
    			BinaryNode * left;
    
    			BinaryNode(Comparable theElement, 
    				BinaryNode * lt = NULL, BinaryNode * rt = NULL)
    			: element(theElement), left(lt), right(rt) {}
    		};
    
    		BinaryNode * nullnode;
    
    		inline void zig(BinaryNode * & root)
    		{
    			BinaryNode * leftChild = root->left;
    
    			root->left = leftChild->right;
    			leftChild->right = root;
    			root = leftChild;
    		}
    
    		inline void zag(BinaryNode * & root)
    		{
    			BinaryNode * rightChild = root->right;
    
    			root->right = rightChild->left;
    			rightChild->left = root;
    			root = rightChild;
    		}
    
    		inline Comparable remove(BinaryNode * & root)
    		{
    			BinaryNode tmp = * root;
    			delete root;
    
    			if(tmp.left == nullnode)
    			{
    				root = tmp.right;
    			}
    			else
    			{
    				splay(tmp.element, tmp.left);
    				root = tmp.left;
    				root->right = tmp.right;
    			}
    
    			return tmp.element;
    		}
    
    		inline void splay(Comparable key, BinaryNode * & root)
    		{
    			static BinaryNode header(key, nullnode, nullnode);
    			header.left = header.right = nullnode;
    			BinaryNode * leftTreeMax = & header;
    			BinaryNode * rightTreeMin = & header;
    			nullnode->element = key;
    
    			while(true)
    			{
    				if(key < root->element)
    				{
    					BinaryNode * LeftChild = root->left;
    
    					if(key < LeftChild->element)
    					{
    						zig(root);
    					}
    
    					if(root->left == nullnode)
    					{
    						break;
    					}
    
    					rightTreeMin->left = root;
    					rightTreeMin = root;
    					root = root->left;
    				}
    				else if(key > root->element)
    				{
    					BinaryNode * rightChild = root->right;
    
    					if(key > rightChild->element)
    					{
    						zag(root);
    					}
    
    					if(root->right == nullnode)
    					{
    						break;
    					}
    
    					leftTreeMax->right = root;
    					leftTreeMax = root;
    					root = root->right;
    				}
    				else break;
    			}
    
    			leftTreeMax->right = root->left;
    			rightTreeMin->left = root->right;
    			root->left = header.right;
    			root->right = header.left;
    
    			return;
    		}
    
    	public:
    		BinaryNode * root;
    
    		SplayTree()
    		{
    			nullnode = new BinaryNode(0, NULL, NULL);
    			nullnode->right = nullnode->left = nullnode;
    			root = nullnode;
    		}
    
    		inline bool empty(void)
    		{
    			return root == nullnode;
    		}
    
    		inline void insert(Comparable key)
    		{
    			static BinaryNode * newNode = NULL;
    			if( newNode == NULL )
    			{
    				newNode = new BinaryNode(0, nullnode, nullnode);
    			}
    			newNode->element = key;
    
    			if( root == nullnode )
    			{
    				root = newNode;
    				root->left = root->right = nullnode;
    			}
    			else
    			{
    				splay(key, root);
    				if( key < root->element )
    				{
    					newNode->left = root->left;
    					newNode->right = root;
    
    					root->left = nullnode;
    					root = newNode;
    				}
    				else if( key > root->element )
    				{
    					newNode->left = root;
    					newNode->right = root->right;
    
    					root->right = nullnode;
    					root = newNode;
    				}
    				else return;
    			}
    
    			newNode = NULL;
    			return;
    		}
    
    		inline Comparable solve(Comparable key)
    		{
    			splay(key, root);
    
    			if(root->left != nullnode && key < root->element)
    			{
    				splay(key, root->left);
    				if(key - root->left->element <= root->element - key)
    				{
    					zig(root);
    				}
    			}
    			else if(root->right != nullnode && key > root->element)
    			{
    				splay(key, root->right);
    				if(key - root->element > root->right->element - key)
    				{
    					zag(root);
    				}
    			}
    
    			return remove(root);
    		}
    };
    
    struct data
    {
    	bool mark;
    	int a;
    
    	data(bool mark = false, int a = 0)
    	: mark(mark), a(a) {};
    
    	inline void init(void)
    	{
    		readint(mark);
    		readint(a);
    	}
    
    	inline bool operator < (const data & other) const
    	{
    		return a < other.a;
    	}
    
    	inline int operator - (const data & other)
    	{
    		return a - other.a;
    	}
    
    	inline bool operator > (const data & other) const
    	{
    		return a > other.a;
    	}
    };
    
    int main(int argc, char * argv[])
    {
    	register int i;
    	data tmp;
    	int ans, N;
    
    	while( ~scanf("%d", &N) )
    	{
    		ans = 0;
    		SplayTree<data> workHouse;
    		for(i = 1; i <= N; ++i)
    		{
    			tmp.init();
    			if( workHouse.empty() || 
    						workHouse.root->element.mark == tmp.mark)
    			{
    				workHouse.insert(tmp);
    			}
    			else
    			{
    				(ans += fabs(workHouse.solve(tmp) - tmp)) %= MOD;
    			}
    		}
    		printf("%d
    ", ans);
    	}
    
    	return 0;
    }
    
  • 相关阅读:
    UOJ299 游戏
    SPOJ-DivCnt2 Counting Divisors (square)
    Gym102331B Bitwise Xor
    POJ3495 Bitwise XOR of Arithmetic Progression
    LG5325 【模板】Min_25筛
    LOJ6229 这是一道简单的数学题
    BZOJ3601 一个人的数论
    LOJ138 类欧几里得算法
    Atcoder TypicalDPContest N~T
    莫队基础题
  • 原文地址:https://www.cnblogs.com/ljzalc1022/p/8758501.html
Copyright © 2020-2023  润新知