【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,最后一个领养者没有宠物可以领养
【Solution】
同【BZOJ1588】【HNOI2002】营业额统计均为Splay模板题,多一个删除操作。好久没打练练手。
代码如下:
/**************************************************************
Problem: 1208
User: llgyc
Language: Pascal
Result: Accepted
Time:384 ms
Memory:1476 kb
****************************************************************/
const mo = 1000000;
var ch : array[0..80080,0..1] of longint;
fa,num : array[0..80080] of longint;
n,rt,sz,color,tot : longint;
before,after,ans : longint;
function f_t(a:boolean):longint; begin if a then exit(1) else exit(0); end;
procedure rotate(x,k:longint);
var y:longint;
begin
y:=fa[x]; ch[y,1-k]:=ch[x,k];
if ch[x,k]<>0 then fa[ch[x,k]]:=y;
ch[fa[y],f_t(ch[fa[y],1]=y)]:=x;
fa[x]:=fa[y]; ch[x,k]:=y; fa[y]:=x;
end;
procedure splay(x,goal:longint);
var y,k:longint;
begin
while (fa[x]<>goal) do begin
if fa[fa[x]]=goal
then rotate(x,f_t(ch[fa[x],0]=x))
else begin
y:=fa[x]; k:=f_t(ch[fa[y],0]=y);
if ch[y,k]=x then begin
rotate(x,1-k); rotate(x,k);
end
else begin
rotate(y,k); rotate(x,k);
end;
end;
end;
if goal=0 then rt:=x;
end;
procedure insert(x,val:longint);
var y:longint;
begin
if tot=0 then begin
inc(sz); inc(tot); rt:=sz;
num[sz]:=val; ch[sz,0]:=0; ch[sz,1]:=0; ch[0,0]:=sz; fa[sz]:=0;
exit;
end;
while true do begin
y:=ch[x][f_t(num[x]<val)];
if y=0 then begin
inc(sz); inc(tot); y:=sz;
num[y]:=val; ch[y,0]:=0; ch[y,1]:=0; fa[y]:=x;
ch[x][f_t(num[x]<val)]:=y;
break;
end;
x:=y;
end;
splay(y,0);
end;
function pre(x:longint):longint;
var tmp:longint;
begin
tmp:=ch[x,0];
if tmp=0 then exit(-mo-1);
while (ch[tmp,1]<>0) do tmp:=ch[tmp,1];
before:=tmp;
exit(num[tmp]);
end;
function suc(x:longint):longint;
var tmp:longint;
begin
tmp:=ch[x,1];
if tmp=0 then exit(mo+1);
while (ch[tmp,0]<>0) do tmp:=ch[tmp,0];
after:=tmp;
exit(num[tmp]);
end;
procedure clear(x:longint);
begin ch[x,0]:=0; ch[x,1]:=0; num[x]:=0; fa[x]:=0; end;
procedure delete(x:longint);
var tmp:longint;
begin
splay(x,0); tmp:=pre(x);
if tmp=-mo-1 then begin
rt:=ch[x,1]; ch[0,0]:=rt; fa[rt]:=0; clear(x); dec(tot); exit;
end;
splay(before,0); ch[rt,1]:=ch[x,1]; if ch[x,1]<>0 then fa[ch[x,1]]:=rt;
clear(x); dec(tot);
end;
procedure main();
var a,b,x,y:longint;
begin
readln(n); sz:=0; tot:=0; fa[0]:=0;
while (n>0) do begin
dec(n); readln(a,b);
if tot=0 then color:=a;
if a=color
then insert(rt,b)
else begin
insert(rt,b); x:=pre(rt); y:=suc(rt); delete(rt);
if (x=-mo-1) or ((y<>mo+1) and (y-b<b-x))
then begin
delete(after); ans:=(ans+(y-b)) mod mo;
end
else begin
delete(before); ans:=(ans+(b-x)) mod mo;
end;
end;
end;
writeln(ans);
end;
begin
main();
end.