1208: [HNOI2004]寵物收養所
Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3620 Solved: 1364
[Submit][Status]
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
var n,ans,a,b,i,pre,suc,root,tot,min,typ:longint;
t:array[0..100001]of record
l,r,f,v:longint;
end;
procedure left(aa:longint);
var fa:longint;
begin
fa:=t[aa].f;
t[aa].f:=t[fa].f;
if t[t[fa].f].l=fa then t[t[fa].f].l:=aa;
if t[t[fa].f].r=fa then t[t[fa].f].r:=aa;
t[fa].f:=aa;
t[fa].r:=t[aa].l;
t[t[aa].l].f:=fa;
t[aa].l:=fa;
end;
procedure right(aa:longint);
var fa:longint;
begin
fa:=t[aa].f;
t[aa].f:=t[fa].f;
if t[t[fa].f].l=fa then t[t[fa].f].l:=aa;
if t[t[fa].f].r=fa then t[t[fa].f].r:=aa;
t[fa].f:=aa;
t[fa].l:=t[aa].r;
t[t[aa].r].f:=fa;
t[aa].r:=fa;
end;
procedure splay(now,goal:longint);
var fa:longint;
begin
while t[now].f<>goal do
begin
fa:=t[now].f;
if t[fa].f=goal then
begin
if t[fa].l=now then right(now) else left(now);
end
else
if t[t[fa].f].l=fa then
begin
if t[fa].l=now then
begin
right(fa);
right(now);
end
else
begin
left(now);
right(now);
end;
end
else
begin
if t[fa].r=now then
begin
left(fa);
left(now);
end
else
begin
right(now);
left(now);
end;
end;
end;
if goal=0 then root:=now;
end;
procedure find(x:longint);
var now:longint;
begin
now:=root;
repeat
if (t[now].v<=x) then pre:=now;
if (t[now].v>=x) then suc:=now;
if t[now].v=x then exit;
if x
until now=0;
end;
procedure insert(v:longint);
var now:longint;
begin
now:=root;
while true do
begin
if v<=t[now].v then
begin
if t[now].l<>0 then now:=t[now].l
else begin
inc(tot);
t[tot].f:=now;
t[tot].v:=v;
t[now].l:=tot;
splay(tot,0);
exit;
end;
end
else
if t[now].r<>0 then now:=t[now].r
else begin
inc(tot);
t[tot].f:=now;
t[tot].v:=v;
t[now].r:=tot;
splay(tot,0);
exit;
end;
end;
end;
procedure del(x:longint);
var l,r,new,rk:longint;
begin
splay(x,0);
l:=t[x].l;
r:=t[x].r;
if (l=0)and(r<>0) then
begin
root:=r;
t[r].f:=0;
exit;
end;
if (r=0)and(l<>0) then
begin
root:=l;
t[l].f:=0;
exit;
end;
if (r=0)and(l=0) then
begin
root:=0;
t[root].v:=0;
t[root].l:=0;
t[root].r:=0;
typ:=-1;
//tot:=0;不知道爲什麼加了這句就T了= =
exit;
end;
new:=0; rk:=l;
while (rk<>0) do
begin
new:=rk; rk:=t[rk].r;
end;
splay(new,root);
root:=t[root].l;
t[root].f:=0;
t[root].r:=t[x].r;
t[t[x].r].f:=root;
end;
begin
readln(n);
ans:=0;
tot:=0;
root:=0;
typ:=-1;
for i:=1 to n do
begin
readln(a,b);
if typ=-1 then typ:=a;
if a=typ then
begin
insert(b);
end
else
begin
pre:=0; suc:=0; min:=0;
find(b);
if suc=0 then min:=pre;
if pre=0 then min:=suc;
if (pre=0)and(suc=0) then
begin
typ:=a;
insert(b);
continue;
end; //這個地方沒注意。。wa了很久
if (pre<>0)and(suc<>0) then
if abs(t[pre].v-b)<=abs(t[suc].v-b) then min:=pre else min:=suc;
ans:=(ans+abs(t[min].v-b))mod 1000000;
del(min);
end;
end;
writeln(ans);
end.