BZOJ_1208_&_Codevs_1258_[HNOI2004]_宠物收养所_(平衡树/set)

描述


http://www.lydsy.com/JudgeOnline/problem.php?id=1208

(据说codevs要更新?就不放codevs的地址了吧...)

有宠物和人,每个单位都有一个与其他单位不同的值(正),如果现在有宠物,来了一个人,人取走和他的值最接近的宠物(如果有两个选值小的),如果现在有人,来了一个宠物,和它值最接近的人(如果有两个选值小的)取走它.求每次带宠物走两个单位的值的差的绝对值的总和.

 

 

1208: [HNOI2004]宠物收养所

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 6798  Solved: 2677
[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

 

5
0 2
0 4
1 3
1 2
1 5

 

Sample Output

 

3
(abs(3-2) + abs(2-4)=3,最后一个领养者没有宠物可以领养)

 

HINT

 

Source

 

 

分析


用平衡树查找前驱和后驱即可.依照题意,要么全是人要么全是宠物,所以建一棵树即可.模板题.

注意:

1.判断是否有前驱或后驱时要小心.

 

p.s.第一道用Splay写的题,调了好久...指针理解的不是很好啊.

Treap:

  1 #include <cstdio>
  2 #include <cstdlib>
  3 #include <algorithm>
  4 using namespace std;
  5 
  6 const int oo=~0u>>1;
  7 
  8 struct Treap{
  9     struct node{
 10         node* ch[2];
 11         int v,r,s;
 12         node(int v,node* t):v(v){ ch[0]=ch[1]=t; r=rand(); s=1; }
 13         void push_up(){ s=ch[0]->s+ch[1]->s+1; }
 14     }*root,*null;
 15     Treap(){
 16         null=new node(0,NULL); null->s=0; null->r=oo;
 17         root=null;
 18     }
 19     void rot(node* &o,bool d){
 20         node* k=o->ch[!d]; o->ch[!d]=k->ch[d]; k->ch[d]=o;
 21         o->push_up(); k->push_up(); o=k;
 22     }
 23     void insert(node* &o,int x){
 24         if(o==null) o=new node(x,null);
 25         else{
 26             bool d=x>o->v;
 27             insert(o->ch[d],x);
 28             if(o->ch[d]->r<o->r) rot(o,!d);
 29             o->push_up();
 30         }
 31     }
 32     void remove(node* &o,int x){
 33         if(o->v==x){
 34             if(o->ch[0]!=null&&o->ch[1]!=null){
 35                 bool d=o->ch[0]->r<o->ch[1]->r;
 36                 rot(o,d); remove(o->ch[d],x);
 37                 o->push_up();
 38             }
 39             else{
 40                 node* u=o;
 41                 if(o->ch[0]==null) o=o->ch[1]; else o=o->ch[0];
 42                 delete u;
 43             }
 44         }
 45         else{
 46             bool d=x>o->v;
 47             remove(o->ch[d],x);
 48             o->push_up();
 49         }
 50     }
 51     int pre(int x){
 52         node* t=root;
 53         int ret=-1;
 54         while(t!=null){
 55             if(t->v<x) ret=t->v,t=t->ch[1];
 56             else t=t->ch[0];
 57         }
 58         return ret;
 59     }
 60     int suc(int x){
 61         node* t=root;
 62         int ret=-1;
 63         while(t!=null){
 64             if(t->v>x) ret=t->v,t=t->ch[0];
 65             else t=t->ch[1];
 66         }
 67         return ret;
 68     }
 69 }tree;
 70 
 71 const int maxn=80000+5,mod=1e6;
 72 int n;
 73 int main(){
 74     scanf("%d",&n);
 75     int cnt=0,now,ans=0;
 76     while(n--){
 77         int a,b;
 78         scanf("%d%d",&a,&b);
 79         if(cnt==0) now=a;
 80         if(a==now){
 81             tree.insert(tree.root,b);
 82             cnt++;
 83         }
 84         else{
 85             int pre=tree.pre(b);
 86             int suc=tree.suc(b);
 87             if(pre==-1){
 88                 int    t=suc-b;
 89                 ans=(ans+t)%mod;
 90                 tree.remove(tree.root,suc);
 91             }
 92             else if(suc==-1){
 93                 int t=b-pre;
 94                 ans=(ans+t)%mod;
 95                 tree.remove(tree.root,pre);
 96             }
 97             else{
 98                 int t1=b-pre,t2=suc-b;
 99                 if(t1<=t2){
100                     ans=(ans+t1)%mod;
101                     tree.remove(tree.root,pre);
102                 }
103                 else{
104                     ans=(ans+t2)%mod;
105                     tree.remove(tree.root,suc);
106                 }
107             }
108             cnt--;
109         }
110     }
111     printf("%d\n",ans);
112     return 0;
113 }
114 
115 Treap
View Code

 

set:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 const int mod=1000000;
 5 int n,ans,a,b,now,cnt;
 6 set <int> st;
 7 int main(){
 8     scanf("%d",&n);
 9     set <int> :: iterator it;
10     while(n--){
11         scanf("%d%d",&a,&b);
12         if(!cnt) now=a;
13         if(a==now) st.insert(b), cnt++;
14         else{
15             it=st.lower_bound(b);
16             if(it!=st.end()&&it!=st.begin()){
17                 int suc=*it,pre=*(--it);
18                 if(b-pre<=suc-b){
19                     st.erase(pre);
20                     ans=(ans+b-pre)%mod;
21                 }
22                 else{
23                     st.erase(suc);
24                     ans=(ans+suc-b)%mod;
25                 }
26             }
27             else if(it!=st.end()){
28                 int suc=*it;
29                 st.erase(suc);
30                 ans=(ans+suc-b)%mod;
31             }
32             else{
33                 int pre=*(--it);
34                 st.erase(pre);
35                 ans=(ans+b-pre)%mod;
36             }
37             cnt--;
38         }
39     }
40     printf("%d\n",ans);
41     return 0;
42 }
View Code

 


 

posted @ 2016-05-13 18:12  晴歌。  阅读(159)  评论(0编辑  收藏  举报