1208: [HNOI2004]宠物收养所
Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 9732 Solved: 3892
[ Submit][ Status][ Discuss]
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
0 2
0 4
1 3
1 2
1 5
Sample Output
(abs(3-2) + abs(2-4)=3,最后一个领养者没有宠物可以领养)
HINT
Source
这道题卡了两天,原来是我xjb改板子的锅,哎。其实这道题跟上一个基本是一样的,就是讨论了一下情况,但是上一个题目我是把查找过程中的splay注释掉了
但是也过了,然后就感觉自己的想法没问题,但是还是跪了两天。
#include <bits/stdc++.h>
using namespace std;
#define MAXN 100010
const int mod=1000000;
struct Node
{
int key, sz, cnt;
Node *ch[2], *pnt;//左右儿子和父亲
Node() {}
Node(int x, int y, int z)
{
key = x, sz = y, cnt = z;
}
void rs()
{
sz = ch[0]->sz + ch[1]->sz + cnt;
}
} nil(0, 0, 0), *NIL = &nil;
struct Splay //伸展树结构体类型
{
Node *root;
int ncnt;//计算key值不同的结点数,注意已经去重了
Node nod[MAXN];
void init() // 首先要初始化
{
root = NIL;
ncnt = 0;
}
void rotate(Node *x, bool d) //旋转操作,d为true表示右旋
{
Node *y = x->pnt;
y->ch[!d] = x->ch[d];
if (x->ch[d] != NIL)
x->ch[d]->pnt = y;
x->pnt = y->pnt;
if (y->pnt != NIL)
{
if (y == y->pnt->ch[d])
y->pnt->ch[d] = x;
else
y->pnt->ch[!d] = x;
}
x->ch[d] = y;
y->pnt = x;
y->rs();
x->rs();
}
void splay(Node *x, Node *target) //将x伸展到target的儿子位置处
{
Node *y;
while (x->pnt != target)
{
y = x->pnt;
if (x == y->ch[0])
{
if (y->pnt != target && y == y->pnt->ch[0])
rotate(y, true);
rotate(x, true);
}
else
{
if (y->pnt != target && y == y->pnt->ch[1])
rotate(y, false);
rotate(x, false);
}
}
if (target == NIL)
root = x;
}
/************************以上一般不用修改************************/
void insert(int key) //插入一个值
{
if (root == NIL)
{
ncnt = 0;
root = &nod[++ncnt];
root->ch[0] = root->ch[1] = root->pnt = NIL;
root->key = key;
root->sz = root->cnt = 1;
return;
}
Node *x = root, *y;
while (1)
{
x->sz++;
if (key == x->key)
{
x->cnt++;
x->rs();
y = x;
break;
}
else if (key < x->key)
{
if (x->ch[0] != NIL)
x = x->ch[0];
else
{
x->ch[0] = &nod[++ncnt];
y = x->ch[0];
y->key = key;
y->sz = y->cnt = 1;
y->ch[0] = y->ch[1] = NIL;
y->pnt = x;
break;
}
}
else
{
if (x->ch[1] != NIL)
x = x->ch[1];
else
{
x->ch[1] = &nod[++ncnt];
y = x->ch[1];
y->key = key;
y->sz = y->cnt = 1;
y->ch[0] = y->ch[1] = NIL;
y->pnt = x;
break;
}
}
}
splay(y, NIL);
}
Node* search(int key) //查找一个值,返回指针
{
if (root == NIL)
return NIL;
Node *x = root, *y = NIL;
while (1)
{
if (key == x->key)
{
y = x;
break;
}
else if (key > x->key)
{
if (x->ch[1] != NIL)
x = x->ch[1];
else
break;
}
else
{
if (x->ch[0] != NIL)
x = x->ch[0];
else
break;
}
}
splay(x, NIL);
return y;
}
Node* searchm(Node *x,bool d) //查找最小值,返回指针
{
Node *y = x->pnt;
while (x->ch[d] != NIL) //遍历到最左的儿子就是最小值
{
x = x->ch[d];
}
splay(x, y);
return x;
}
void del(int key) //删除一个值
{
if (root == NIL)
return;
Node *x = search(key), *y;
if (x == NIL)
return;
if (x->cnt > 1)
{
x->cnt--;
x->rs();
return;
}
else if (x->ch[0] == NIL && x->ch[1] == NIL)
{
init();
return;
}
else if (x->ch[0] == NIL)
{
root = x->ch[1];
x->ch[1]->pnt = NIL;
return;
}
else if (x->ch[1] == NIL)
{
root = x->ch[0];
x->ch[0]->pnt = NIL;
return;
}
y = searchm(x->ch[1],0);
y->pnt = NIL;
y->ch[0] = x->ch[0];
x->ch[0]->pnt = y;
y->rs();
root = y;
}
int rank(int key) //求结点高度
{
Node *x = search(key);
if (x == NIL)
return 0;
return x->ch[0]->sz + 1/* or x->cnt*/;
}
Node* findk(int kth) //查找第k小的值
{
if (root == NIL || kth > root->sz)
return NIL;
Node *x = root;
while (1)
{
if (x->ch[0]->sz +1 <= kth && kth <= x->ch[0]->sz + x->cnt)
break;
else if (kth <= x->ch[0]->sz)
x = x->ch[0];
else
{
kth -= x->ch[0]->sz + x->cnt;
x = x->ch[1];
}
}
splay(x, NIL);
return x;
}
} sp;
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
int sz[2],ans=0;
memset(sz,0,sizeof(sz));
sp.init();
for(int i=0; i<n; i++)
{
int team,val;
scanf("%d%d",&team,&val);
sp.insert(val);
if(sz[!team])
{
sz[!team]--;
if(sp.root->cnt>1)///说明之前有相同元素
{
sp.del(val);
continue;
}
else
{
if(sp.root->ch[0]==NIL)
{
int t=sp.searchm(sp.root->ch[1],0)->key;
ans+=(t-val)%mod;
sp.del(t);
}
else if(sp.root->ch[1]==NIL)
{
int t=sp.searchm(sp.root->ch[0],1)->key;
ans+=(val-t)%mod;
sp.del(t);
}
else
{
int t=sp.searchm(sp.root->ch[1],0)->key;
int t1=sp.searchm(sp.root->ch[0],1)->key;
if(val-t1<=t-val)
{
ans+=(val-t1)%mod;
sp.del(t1);
}
else
{
ans+=(t-val)%mod;
sp.del(t);
}
}
}
sp.del(val);
}
else
{
sz[team]++;
}
ans%=mod;
}
printf("%d\n",ans);
}
}