【题目描述】
凡凡开了一间宠物收养场。收养场提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物。
每个领养者都希望领养到自己满意的宠物,凡凡根据领养者的要求通过他自己发明的一个特殊的公式,得出该领养者希望领养的宠物的特点值a(a是一个正整数,a<2^31),而他也给每个处在收养场的宠物一个特点值。这样他就能够很方便的处理整个领养宠物的过程了,宠物收养场总是会有两种情况发生:被遗弃的宠物过多或者是想要收养宠物的人太多,而宠物太少。
被遗弃的宠物过多时,假若到来一个领养者,这个领养者希望领养的宠物的特点值为a,那么它将会领养一只目前未被领养的宠物中特点值最接近a的一只宠物。(任何两只宠物的特点值都不可能是相同的,任何两个领养者的希望领养宠物的特点值也不可能是一样的)如果有两只满足要求的宠物,即存在两只宠物他们的特点值分别为a-b和a+b,那么领养者将会领养特点值为a-b的那只宠物。
收养宠物的人过多,假若到来一只被收养的宠物,那么哪个领养者能够领养它呢?能够领养它的领养者,是那个希望被领养宠物的特点值最接近该宠物特点值的领养者,如果该宠物的特点值为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以后的结果。
S a m p l e I n p u t Sample~~Input Sample Input
5 0 2 0 4 1 3 1 2 1 5
S a m p l e O u t p u t Sample~~Output Sample Output
3
【题意分析】
构建一棵splay,用一个type维护当前splay中的全部是顾客还是全部是宠物 (题目保证同一时间呆在收养所中的,要么全是宠物,要么全是领养者) 用一个变量cnt维护splay中有几个节点(当然你维护root的size也可以) 那么我们可以判断加入一个东西,如果加入的时候cnt为0了,那么就把type修改为加入东西的类型 (比如说splay空了,加了一只pet,就把type修改为pet) 如果和splay维护的相同就简单加入,cnt++。 如果类型不相同,搞一下前驱和后继,取绝对值之差小的计入ans,取% (不用管当前splay的是pet还是customer,操作都是取前驱后继再处理)
Code:
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <algorithm> #define qy 1000000 #define MAXN 300000 #define INF 1 << 30 using namespace std; int n, type, cnt; long long ans; struct spaly { int son[MAXN][3], val[MAXN], father[MAXN], sz, root; inline void rotate (int x) { int y = father[x], z = father[y]; int k = son[y][1] == x, kk = son[z][1] == y; son[z][kk] = x; father[x] = z; son[y][k] = son[x][k ^ 1]; father[son[x][k ^ 1]] = y; son[x][k ^ 1] = y; father[y] = x; } inline void splay (int x, int goal) { while (father[x] != goal) { int y = father[x], z = father[y]; if (z != goal) (son[y][1] == x) ^ (son[z][1] == y) ? rotate (x) : rotate (y); rotate (x); } if (! goal) root = x; } inline int prev (int x) { int now = root, pre; while (now) { (val[now] < x) ? pre = now, now = son[now][1] : now = son[now][0]; } return pre; } inline int succ (int x) { int now = root, nxt; while (now) { (val[now] > x) ? nxt = now, now = son[now][0] : now = son[now][1]; } return nxt; } inline void del (int x) { int pre = prev (x), nxt = succ (x); splay (pre, 0), splay (nxt, pre); son[nxt][0] = 0; } inline void insert (int x) { int now = root, fa = 0; while (now && x != val[now]) fa = now, now = son[now][x > val[now]]; now = ++sz; if (fa) son[fa][x > val[fa]] = now; son[now][0] = son[now][1] = 0; val[now] = x, father[now] = fa; splay (now, 0); } }tree; int main () { scanf ("%d", &n), tree.insert (INF), tree.insert (-INF); for (register int i = 1; i <= n; i++) { int opt, x; scanf ("%d%d", &opt, &x); if (cnt == 0) { type = opt, tree.insert (x), cnt++; } else if (type == opt) tree.insert (x), cnt++; else { int pre = tree.prev (x), nxt = tree.succ (x); int Pre = tree.val[pre], Nxt = tree.val[nxt]; if (abs (Pre - x) <= abs (Nxt - x)) tree.del (Pre), ans += abs (Pre - x); else tree.del (Nxt), ans += abs (Nxt - x); ans %= qy, cnt--; } } printf ("%lld\n", ans); return 0; }