BZOJ1208 宠物收养所

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,最后一个领养者没有宠物可以领养)
 
 
正解一:STL之set大法
解题报告:
      用STL中的set暴力维护题意的要求就可以了,没什么好说的。
  每次查找大于x的元素、小于等于x的元素,比较一下就加进贡献就可以了,注意取模。
     注:一开始加入一个无限大和无限小进入set,方便我们操作。
  set就是好,又短又快。
 
 
 1 //It is made by jump~
 2 #include <iostream>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <cstdio>
 6 #include <cmath>
 7 #include <algorithm>
 8 #include <ctime>
 9 #include <vector>
10 #include <queue>
11 #include <map>
12 #include <set>
13 #ifdef WIN32   
14 #define OT "%I64d"
15 #else
16 #define OT "%lld"
17 #endif
18 using namespace std;
19 typedef long long LL;
20 const int inf = ( (1<<31) -1);
21 const int MOD = 1000000;
22 int n;
23 set<int>bst;
24 int kind;
25 int ans;
26 
27 inline int getint()
28 {
29     int w=0,q=0;
30     char c=getchar();
31     while((c<'0' || c>'9') && c!='-') c=getchar();
32     if (c=='-')  q=1, c=getchar();
33     while (c>='0' && c<='9') w=w*10+c-'0', c=getchar();
34     return q ? -w : w;
35 }
36 
37 int main()
38 {
39     n=getint();
40     bst.insert(inf); bst.insert(-inf);
41     int f,x;
42     int l,r;
43     for(int i=1;i<=n;i++) {
44     f=getint(),x=getint();
45     if(bst.size()==2){
46         kind=f; bst.insert(x);
47     }
48     else if(kind==f) bst.insert(x);
49     else{
50         l=*--bst.lower_bound(x);
51         r=*bst.lower_bound(x);
52         if(x-l<=r-x && l!=-inf){
53         ans+=x-l; ans%=MOD;
54         bst.erase(l);
55         }
56         else{
57         ans+=r-x; ans%=MOD;
58         bst.erase(r);
59         }
60     }
61     }
62     printf("%d",ans);
63     return 0;
64 }

正解二:splay

解题报告:

      显然手打splay才是勤奋的oier该做的事。

  

  %%%hzwer

  1 //It is made by jump~
  2 #include <iostream>
  3 #include <cstdlib>
  4 #include <cstring>
  5 #include <cstdio>
  6 #include <cmath>
  7 #include <algorithm>
  8 #include <ctime>
  9 #include <vector>
 10 #include <queue>
 11 #include <map>
 12 #include <set>
 13 #ifdef WIN32   
 14 #define OT "%I64d"
 15 #else
 16 #define OT "%lld"
 17 #endif
 18 using namespace std;
 19 typedef long long LL;
 20 const int MOD = 1000000;
 21 const int MAXN = 80011;
 22 int n;
 23 int kind;
 24 int rt;
 25 int t1,t2;
 26 int tr[MAXN][2],num[MAXN],fa[MAXN];
 27 int size;
 28 int ans;
 29 
 30 inline int getint()
 31 {
 32        int w=0,q=0;
 33        char c=getchar();
 34        while((c<'0' || c>'9') && c!='-') c=getchar();
 35        if (c=='-')  q=1, c=getchar();
 36        while (c>='0' && c<='9') w=w*10+c-'0', c=getchar();
 37        return q ? -w : w;
 38 }
 39 
 40 inline void rotate(int x,int &k){
 41     int y=fa[x],z=fa[y],l,r;
 42     if(tr[y][0]==x) l=0;  else l=1;
 43     r=l^1;
 44     if(y==k) k=x;
 45     else {
 46     if(tr[z][0]==y) tr[z][0]=x; 
 47     else tr[z][1]=x;
 48     }
 49     fa[x]=z; fa[y]=x; fa[tr[x][r]]=y;
 50     tr[y][l]=tr[x][r]; tr[x][r]=y;
 51 }
 52 
 53 inline void splay(int x,int &k){
 54     int y,z;
 55     while(x!=k) {
 56     y=fa[x],z=fa[y];
 57     if(y!=k) {
 58         if((tr[y][0]==x)^(tr[z][0]==y)) rotate(x,k);
 59         else rotate(y,k);
 60     }
 61     rotate(x,k);
 62     }
 63 }
 64 
 65 inline void insert(int &k,int x,int last){
 66     if(k==0) {
 67     size++; k=size; num[k]=x; fa[k]=last; splay(k,rt); return ;
 68     }
 69     if(x<num[k]) insert(tr[k][0],x,k); else insert(tr[k][1],x,k);
 70 }
 71 
 72 inline void ask_before(int k,int x){
 73     if(k==0) return ;
 74     if(num[k]<=x) { t1=k; ask_before(tr[k][1],x); }
 75     else ask_before(tr[k][0],x);
 76 }
 77 
 78 inline void ask_after(int k,int x){
 79     if(k==0) return ;
 80     if(num[k]>=x){ t2=k; ask_after(tr[k][0],x); }
 81     else ask_after(tr[k][1],x);
 82 }
 83 
 84 inline void del(int x){
 85     splay(x,rt);
 86     if(tr[x][0]*tr[x][1]==0) rt=tr[x][0]+tr[x][1];
 87     else{
 88     int k=tr[x][1];
 89     while(tr[k][0]) k=tr[k][0];
 90     tr[k][0]=tr[x][0]; fa[tr[x][0]]=k;
 91     rt=tr[x][1];
 92     }
 93     fa[rt]=0;
 94 }
 95 
 96 int main()
 97 {
 98   n=getint();
 99   int f,x;
100   for(int i=1;i<=n;i++) {
101       f=getint(); x=getint();
102       if(!rt) { kind=f; insert(rt,x,0); }
103       else if(kind==f) { insert(rt,x,0); }
104       else{
105       t1=t2=-1;
106       ask_before(rt,x); ask_after(rt,x);
107       if(t1==-1) { ans+=num[t2]-x; if(ans>=MOD) ans%=MOD; del(t2); }
108       else if(t2==-1) { ans+=x-num[t1]; if(ans>=MOD) ans%=MOD; del(t1); }
109       else{
110           if(x-num[t1]>num[t2]-x){
111           ans+=num[t2]-x; if(ans>=MOD) ans%=MOD; del(t2);
112           }
113           else{
114           ans+=x-num[t1]; if(ans>=MOD) ans%=MOD; del(t1);
115           }
116       } 
117       }
118   }
119   printf("%d",ans);
120   return 0;
121 }
原文地址:https://www.cnblogs.com/ljh2000-jump/p/5528687.html