BZOJ 1208: [HNOI2004]宠物收养所

1208: [HNOI2004]宠物收养所

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 5688  Solved: 2187

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

Splay

解题:Splay

  1 #include <bits/stdc++.h>
  2 using namespace std;
  3 const int INF = 0x3f3f3f3f;
  4 const int maxn = 100010;
  5 const int mod = 1000000;
  6 struct SplayTree {
  7     int val[maxn],fa[maxn],ch[maxn][2];
  8     int root,tot;
  9     void init() {
 10         root = tot = fa[0] = 0;
 11         ch[0][0] = ch[0][1] = 0;
 12     }
 13     void newnode(int &x,int key,int f) {
 14         val[x = ++tot] = key;
 15         fa[x] = f;
 16         ch[x][0] = ch[x][1] = 0;
 17     }
 18     void rotate(int x,int kd) {
 19         int y = fa[x];
 20         ch[y][kd^1] = ch[x][kd];
 21         fa[ch[x][kd]] = y;
 22         fa[x] = fa[y];
 23         fa[y] = x;
 24         ch[x][kd] = y;
 25         if(fa[x]) ch[fa[x]][y == ch[fa[x]][1]] = x;
 26     }
 27     void Splay(int x,int goal) {
 28         while(fa[x] != goal) {
 29             if(fa[fa[x]] == goal) rotate(x,x == ch[fa[x]][0]);
 30             else {
 31                 int y = fa[x],z = fa[y],s = (ch[z][0] == y);
 32                 if(x == ch[y][s]) {
 33                     rotate(x,!s);
 34                     rotate(x,s);
 35                 } else {
 36                     rotate(y,s);
 37                     rotate(x,s);
 38                 }
 39             }
 40         }
 41         if(!goal) root = x;
 42     }
 43     void insert(int key) {
 44         if(!root) {
 45             newnode(root,key,0);
 46             return;
 47         }
 48         int x = root;
 49         while(ch[x][val[x] < key]) x = ch[x][val[x] < key];
 50         newnode(ch[x][val[x] < key],key,x);
 51         Splay(tot,0);
 52     }
 53     int precursor(int key) {
 54         int ret = -INF,x = root;
 55         while(x) {
 56             if(val[x] <= key) {
 57                 ret = x;
 58                 x = ch[x][1];
 59             } else x = ch[x][0];
 60         }
 61         return ret;
 62     }
 63     int sucessor(int key) {
 64         int x = root,ret = INF;
 65         while(x) {
 66             if(val[x] >= key) {
 67                 ret = x;
 68                 x = ch[x][0];
 69             } else x = ch[x][1];
 70         }
 71         return ret;
 72     }
 73     void delNode(int x) {
 74         Splay(x,0);
 75         if(ch[x][0] || ch[x][1]) {
 76             if(!ch[x][0]) fa[root = ch[x][1]] = 0;
 77             if(!ch[x][1]) fa[root = ch[x][0]] = 0;
 78             if(ch[x][0] && ch[x][1]) {
 79                 int y = ch[x][0];
 80                 while(ch[y][1]) y = ch[y][1];
 81                 Splay(y,x);
 82                 ch[y][1] = ch[x][1];
 83                 fa[root = y] = 0;
 84                 fa[ch[x][1]] = y;
 85             }
 86         } else root = 0;
 87     }
 88     int Find(int key) {
 89         int x = root;
 90         while(x) {
 91             if(val[x] == key) return x;
 92             x = ch[x][val[x] < key];
 93         }
 94         return 0;
 95     }
 96 } spt;
 97 int main() {
 98     spt.init();
 99     int n,kind,k,tkind = -1,ret = 0;
100     scanf("%d",&n);
101     for(int i = 1; i <= n; ++i) {
102         scanf("%d%d",&kind,&k);
103         if(!spt.root || kind == tkind) {
104             tkind = kind;
105             spt.insert(k);
106         } else {
107             int root = spt.Find(k);
108             if(root) {
109                 spt.delNode(root);
110                 continue;
111             }
112             int a = spt.precursor(k),aa;
113             int b = spt.sucessor(k),bb;
114             if(a != -INF) aa = spt.val[a];
115             else aa = INF;
116             if(b != INF) bb = spt.val[b];
117             else bb = INF;
118             if(abs(aa - k) <= abs(bb - k)){
119                 ret = (ret + abs(aa - k))%mod;
120                 spt.delNode(a);
121             }else{
122                 ret = (ret + abs(bb - k))%mod;
123                 spt.delNode(b);
124             }
125         }
126     }
127     printf("%d\n",ret);
128     return 0;
129 }
View Code

 

posted @ 2015-10-09 19:01  狂徒归来  阅读(210)  评论(0编辑  收藏  举报