码迷,mamicode.com
首页 > 其他好文 > 详细

BZOJ 1208: [HNOI2004]宠物收养所

时间:2016-12-10 00:55:43      阅读:302      评论:0      收藏:0      [点我收藏+]

标签:else   stat   先后   desc   cat   inline   discuss   and   name   

1208: [HNOI2004]宠物收养所

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 7514  Solved: 2982
[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,最后一个领养者没有宠物可以领养)

分析:

Splay模板题...其实可以用set水过去...然而第一次写Splay就拿来练手了...(QAQ为什么我的Splay这么长...)

代码:

技术分享
  1 #include<algorithm>
  2 #include<iostream>
  3 #include<cstring>
  4 #include<cstdio>
  5 #include<cmath>
  6 //by NeighThorn
  7 #define inf 0x3f3f3f3f
  8 using namespace std;
  9 
 10 const int maxn=80000+5,MOD=1000000;
 11 
 12 int n,a,b,ans,cnt;
 13 int root,tot,w[maxn],fa[maxn],ls[maxn],rs[maxn];
 14 
 15 inline void zig(int x){
 16     int y=fa[x];
 17     if(rs[x])
 18         fa[rs[x]]=y,ls[y]=rs[x];
 19     else
 20         ls[y]=0;
 21     fa[x]=fa[y];
 22     if(fa[x]){
 23         if(ls[fa[x]]==y)
 24             ls[fa[x]]=x;
 25         else
 26             rs[fa[x]]=x;
 27     }
 28     fa[y]=x;rs[x]=y;
 29 }
 30 
 31 inline void zag(int x){
 32     int y=fa[x];
 33     if(ls[x])
 34         fa[ls[x]]=y,rs[y]=ls[x];
 35     else
 36         rs[y]=0;
 37     fa[x]=fa[y];
 38     if(fa[x]){
 39         if(ls[fa[x]]==y)
 40             ls[fa[x]]=x;
 41         else
 42             rs[fa[x]]=x;
 43     }
 44     fa[y]=x,ls[x]=y;
 45 }
 46 
 47 inline void splay(int x,int z){
 48     while(fa[x]!=z){
 49         int y=fa[x];
 50         if(fa[y]==z){
 51             if(ls[y]==x)
 52                 zig(x);
 53             else
 54                 zag(x);
 55         }
 56         else{
 57             if(ls[fa[y]]==y){
 58                 if(ls[y]==x)
 59                     zig(y),zig(x);
 60                 else
 61                     zag(x),zig(x);
 62             }
 63             else{
 64                 if(rs[y]==x)
 65                     zag(y),zag(x);
 66                 else
 67                     zig(x),zag(x);
 68             }
 69         }
 70     }
 71     if(!z)
 72         root=x;
 73 }
 74 
 75 inline void ins(int rt,int x){
 76     if(rt==0){
 77         w[++tot]=x;root=tot;
 78         return;
 79     }
 80     if(w[rt]==x)
 81         return;
 82     else if(x<w[rt]){
 83         if(!ls[rt])
 84             w[++tot]=x,ls[rt]=tot,fa[tot]=rt,splay(tot,0);
 85         else
 86             ins(ls[rt],x);
 87     }
 88     else{
 89         if(!rs[rt])
 90             w[++tot]=x,rs[rt]=tot,fa[tot]=rt,splay(tot,0);
 91         else
 92             ins(rs[rt],x);
 93     }
 94 }
 95 
 96 inline void del(int rt,int x){
 97     if(w[rt]==x){
 98         splay(rt,0);
 99         int l=ls[rt],r=rs[rt];
100         if(!l)
101             root=r,fa[r]=0;
102         else{
103             while(rs[l])
104                 l=rs[l];
105             splay(l,rt);
106             root=l,fa[l]=0,rs[l]=rs[rt],fa[rs[rt]]=l;
107         }
108     }
109     else if(x<w[rt])
110         del(ls[rt],x);
111     else
112         del(rs[rt],x);
113 }
114 
115 inline int pre(int rt,int x){
116     int res=-inf;
117     while(rt){
118         if(x==w[rt])
119             return x;
120         else{
121             if(w[rt]<x)
122                 res=max(res,w[rt]),rt=rs[rt];
123             else
124                 rt=ls[rt];
125         }
126     }
127     return res;
128 }
129 
130 inline int suf(int rt,int x){
131     int res=inf;
132     while(rt){
133         if(x==w[rt])
134             return x;
135         else{
136             if(w[rt]>x)
137                 res=min(res,w[rt]),rt=ls[rt];
138             else
139                 rt=rs[rt];
140         }
141     }
142     return res;
143 }
144 
145 signed main(void){
146     memset(ls,0,sizeof(ls));
147     memset(rs,0,sizeof(rs));
148     memset(fa,0,sizeof(fa));
149     scanf("%d",&n);tot=root=ans=cnt=0;
150     for(int i=1;i<=n;i++){
151         scanf("%d%d",&a,&b);
152         if(a==0){
153             if(cnt>=0)
154                 ins(root,b);
155             else{
156                 int lala1=pre(root,b),lala2=suf(root,b);
157                 if(abs(lala1-b)<=abs(lala2-b))
158                     (ans+=abs(lala1-b))%=MOD,del(root,lala1);
159                 else
160                     (ans+=abs(lala2-b))%=MOD,del(root,lala2);
161             }
162             cnt++;
163         }
164         else{
165             if(cnt<=0)
166                 ins(root,b);
167             else{
168                 int lala1=pre(root,b),lala2=suf(root,b);
169                 if(abs(lala1-b)<=abs(lala2-b))
170                     (ans+=abs(lala1-b))%=MOD,del(root,lala1);
171                 else
172                     (ans+=abs(lala2-b))%=MOD,del(root,lala2);
173             }
174             cnt--;
175         }
176     }
177     printf("%d\n",ans);
178     return 0;
179 }
View Code

by NeighThorn

BZOJ 1208: [HNOI2004]宠物收养所

标签:else   stat   先后   desc   cat   inline   discuss   and   name   

原文地址:http://www.cnblogs.com/neighthorn/p/6147169.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!