ack def ace register desc 先后 class 前驱
凡凡开了一间宠物收养场。收养场提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物。
每个领养者都希望领养到自己满意的宠物,凡凡根据领养者的要求通过他自己发明的一个特殊的公式,得出该领养者希望领养的宠物的特点值 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 以后的结果。
- 5
- 0 2
- 0 4
- 1 3
- 1 2
- 1 5
- 3
- 注:abs(3-2) + abs(2-4)=3, 最后一个领养者没有宠物可以领养。
基本的 $Treap$ 加上找前驱后继节点可以 $AC$。
首先,建一棵 $Treap$ 保存宠物或人,每次在里面找一个最近的抵消(通过找前驱后继来实现),其他的看题都知道怎么处理。
那么重点来了,前驱后继的实现。
前驱:
定义变量 $p1$,用来保存前驱最近的节点。
从根开始(显然),如果需要的编号小于当前编号,就往前走(走左儿子),否则 $p1=$ 当前编号,往右走。
后继同理。
做这道题的时候没想到直接去找前驱后继,就去二分找值了...
- #include<set>
- #include<map>
- #include<cmath>
- #include<ctime>
- #include<queue>
- #include<stack>
- #include<vector>
- #include<cstdio>
- #include<string>
- #include<cstdlib>
- #include<cstring>
- #include<iostream>
- #include<algorithm>
- #define LL long long
- #define RE register
- #define IL inline
- using namespace std;
- const int N=80000;
- const int MOD=1000000;
- IL int Abs(const int &a) {return a<0 ? -a:a;}
- IL int Min(const int &a,const int &b) {return a<b ? a:b;}
- struct node
- {
- int key,lev,low;
- node *child[2];
- }T[N+5],*root=NULL,*pos=T;
- int key[N+5];
- int n,a,b;
- int flag,ans,size;
- void NewNode(node* &r,int key);
- void Insert(node* &r,int key);
- void Delete(node* &r,int key);
- int Query(node* r,int rank,int cnt);
- void Rotate(node* &r,bool t);
- int Count(node* r);
- int main()
- {
- srand(time(0));
- scanf("%d",&n);
- while (n--)
- {
- scanf("%d%d",&a,&b);
- if (size==0)
- {
- flag=a;
- Insert(root,b);
- size++;
- }
- else if (flag==a)
- {
- Insert(root,b);
- size++;
- }
- else
- {
- int l=1,r=size,mid,ansn=Query(root,1,0),ansi=1;
- while (l<=r)
- {
- mid=(l+r)>>1;
- int tmp=Query(root,mid,0);
- if (tmp<=b) ansn=tmp,ansi=mid,l=mid+1;
- else r=mid-1;
- }
- if (ansi==size||size==1)
- {
- ans=(ans+Abs(b-ansn))%MOD;
- Delete(root,ansn);
- size--;
- }
- else
- {
- int tmp=Query(root,ansi+1,0);
- if (Abs(b-ansn)<=Abs(b-tmp))
- {
- ans=(ans+Abs(b-ansn))%MOD;
- Delete(root,ansn);
- }
- else
- {
- ans=(ans+Abs(b-tmp))%MOD;
- Delete(root,tmp);
- }
- size--;
- }
- }
- }
- printf("%d\n",ans);
- return 0;
- }
- void NewNode(node* &r,int key)
- {
- r=pos++;
- r->key=key;
- r->low=1;
- r->lev=rand();
- }
- void Insert(node* &r,int key)
- {
- if (!r)
- {
- NewNode(r,key);
- return;
- }
- bool t=key>r->key;
- if (!t) r->low++;
- Insert(r->child[t],key);
- if (r->child[t]->lev<r->lev)
- {
- Rotate(r,!t);
- r->low=1+Count(r->child[0]);
- }
- }
- void Delete(node* &r,int key)
- {
- if (r->key==key)
- {
- if (r->child[0]&&r->child[1])
- {
- bool t=r->child[0]->lev<r->child[1]->lev;
- Rotate(r,t);
- r->low=1+Count(r->child[0]);
- if (!t) r->low--;
- Delete(r->child[t],key);
- }
- else
- {
- if (r->child[0]) r=r->child[0];
- else r=r->child[1];
- }
- }
- else
- {
- bool t=key>r->key;
- if (!t) r->low--;
- Delete(r->child[t],key);
- }
- }
- void Rotate(node* &r,bool t)
- {
- node *y=r->child[!t],*R=r;
- r->child[!t]=y->child[t];
- y->child[t]=r;
- r=y;
- R->low=1+Count(R->child[0]);
- }
- int Query(node* r,int rank,int cnt)
- {
- if (r->low+cnt==rank) return r->key;
- if (r->low+cnt<rank) return Query(r->child[1],rank,r->low+cnt);
- if (r->low+cnt>rank) return Query(r->child[0],rank,cnt);
- }
- int Count(node* r)
- {
- if (!r) return 0;
- return r->low+Count(r->child[1]);
- }
[HNOI 2004] 宠物收养场
来源: http://www.bubuko.com/infodetail-2268596.html