题目链接:http://www.wikioi.com/problem/1285/

算法:Splay

刚开始看到这题,就注意到特征abs了,并且数据n<=80000显然不能暴力,只能用nlgn的做法,综合起来,似乎只有一个答案:Splay。

将每次插入的点splay到树根,然后找到它的前驱和后继,再取它的绝对值即可,我们记作_abs。那么答案就为sum{_absi},其中i为领养的人的数量,可以看为宠物的数量。

要注意的:

  1. 领养者将会领养特点值为a-b的那只宠物 和 特点值为a-b的那个领养者将成功领养该宠物 告诉我们要取前驱。
  2. 同一时间呆在收养所中的,要么全是宠物,要么全是领养者,这些宠物和领养者的个数不会超过10000个。 告诉我们要建两棵Splay树来存剩下的人。
  3. 操作人的和操作动物的几乎一样。

==========================14.06.13==========================

原来写的splay的bug太多,已换成数组= = ps:14.07.26又换成指针。。。。>_<

详细看另一篇splay文章,http://www.cnblogs.com/iwtwiioi/p/3537061.html

=================================很久以前==============================

下面放上代码

#include <cstdio>
using namespace std;
#define F(rt) rt-> pa
#define K(rt) rt-> key
#define CH(rt, d) rt-> ch[d]
#define C(rt, d) (K(rt) > d ? 0 : 1)
#define NEW(d) new Splay(d)
#define PRE(rt) F(rt) = CH(rt, 0) = CH(rt, 1) = null int n, ans, who; struct Splay {
Splay* ch[2], *pa;
int key;
Splay(int d = 0) : key(d) { ch[0] = ch[1] = pa = NULL; }
}; typedef Splay* tree;
tree null = new Splay, root[2] = {null, null}; void rot(tree& rt, int d) {
tree k = CH(rt, d^1), u = F(rt); int flag = CH(u, 1) == rt;
CH(rt, d^1) = CH(k, d); if(CH(k, d) != null) F(CH(k, d)) = rt;
CH(k, d) = rt; F(rt) = k; rt = k; F(rt) = u;
if(u != null) CH(u, flag) = k;
} void splay(tree nod, tree& rt) {
if(nod == null) return;
tree pa = F(rt);
while(F(nod) != pa) {
if(F(nod) == rt)
rot(rt, CH(rt, 0) == nod);
else {
int d = CH(F(F(nod)), 0) == F(nod);
int d2 = CH(F(nod), 0) == nod;
if(d == d2) { rot(F(F(nod)), d); rot(F(nod), d2); }
else { rot(F(nod), d2); rot(F(nod), d); }
}
}
rt = nod;
} tree maxmin(tree rt, int d) {
if(rt == null) return null;
while(CH(rt, d) != null) rt = CH(rt, d);
return rt;
} tree ps(tree rt, int d) {
if(rt == null) return null;
rt = CH(rt, d);
return maxmin(rt, d^1);
} tree search(tree& rt, int d) {
if(rt == null) return null;
tree t = rt;
while(t != null && K(t) != d) t = CH(t, C(t, d));
splay(t, rt);
return t;
} void insert(tree& rt, int d) {
tree q = NULL, t = rt;
while(t != null) q = t, t = CH(t, C(t, d));
t = new Splay(d);
PRE(t);
if(q) F(t) = q, CH(q, C(q, d)) = t;
else rt = t;
splay(t, rt);
} void del(tree& rt) {
if(rt == null) return;
tree t = rt;
if(CH(t, 0) == null) t = CH(rt, 1);
else {
t = CH(rt, 0);
splay(ps(rt, 0), t);
CH(t, 1) = CH(rt, 1);
if(CH(rt, 1) != null) F(CH(rt, 1)) = t;
}
delete rt;
F(t) = null;
rt = t;
} void init(int key, int d) {
if(root[d^1] == null) { who = d; insert(root[who], key); return; }
who = d^1;
insert(root[who], key);
tree succ = ps(root[who], 0), pred = ps(root[who], 1);
int l = 0, r = 0;
if(succ != null) l = K(root[who]) - K(succ);
if(pred != null) r = K(pred) - K(root[who]);
del(root[who]);
if(succ != null && (pred == null || l <= r)) {
ans = (ans + l) % 1000000;
splay(succ, root[who]);
del(root[who]);
}
else if(pred != null && (succ == null || r < l)) {
ans = (ans + r) % 1000000;
splay(pred, root[who]);
del(root[who]);
}
} int main() {
PRE(null);
scanf("%d", &n);
int c, b;
while(~scanf("%d%d", &c, &b)) init(b, c);
printf("%d", ans);
return 0;
}

  

【wikioi】1285 宠物收养所的更多相关文章

  1. splay树 1285 宠物收养所

    #include<cstdio> #include<iostream> using namespace std; int shu[80004][2],n,size,root,k ...

  2. C++之路进阶——codevs1285(宠物收养所)

    1285 宠物收养所  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond   题目描述 Description 最近,阿Q开了一间宠物收养所.收养所提供两种服 ...

  3. HNOI2004 宠物收养所 (Treap)

    1285 宠物收养所 http://codevs.cn/problem/1285/  时间限制: 1 s  空间限制: 128000 KB     题目描述 Description 最近,阿Q开了一间 ...

  4. Bzoj1208 [HNOI2004]宠物收养所

    Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 7457  Solved: 2960 Description 最近,阿Q开了一间宠物收养所.收养所提供两 ...

  5. BZOJ 1208: [HNOI2004]宠物收养所

    1208: [HNOI2004]宠物收养所 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 7514  Solved: 2982[Submit][Sta ...

  6. 宠物收养所(bzoj1208)

    Description 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物.每个领养者都希望领养到自己满意的宠物,阿Q根据领养者的要求通过他自己发明的一个特 ...

  7. 【BZOJ1208】[HNOI2004]宠物收养所 Splay

    还是模板题,两颗splay,找点删即可. #include <iostream> #include <cstdio> #include <cstdlib> #def ...

  8. 【BZOJ-1208】宠物收养所 Splay

    1208: [HNOI2004]宠物收养所 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 6638  Solved: 2601[Submit][Sta ...

  9. BZOJ1208 宠物收养所

    Description 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物.每个领养者都希望领养到自己满意的宠物,阿Q根据领养者的要求通过他自己发明的一个特 ...

随机推荐

  1. 《ASP.NET1200例》嵌套在DataLisT控件中的其他服务器控件---DropDownList控件的数据绑定

    aspx <script type="text/javascript"> function CheckAll(Obj) { var AllObj = document. ...

  2. HDU 4310 Hero (贪心算法)

    A - Hero Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Sta ...

  3. Ubuntu 12.10 安装 jdk-7u10-linux-x64.tar.gz(转载)

    在Ubuntu 12.10下安装 jdk-7u10-linux-x64.tar.gz 总的原则:将jdk-7u10-linux-x64.tar.gz压缩包解压至/usr/lib/jdk,设置jdk环境 ...

  4. 基于gitosis的Git云端服务器配置

    (本文需要自己实践,由于时间关系,我仅仅是做了整理和快速的练习,至于笔记中的账号和ip域名都是我参考文章中的.如果读者有任何问题欢迎留言和发邮件到luoquantao@126.com) 硬件:云端阿里 ...

  5. 【读书笔记】读《JavaScript高级程序设计-第2版》 - 非函数部分

    章节列表: 第08章:BOM 第09章:客户端检测 第10章:DOM 第11章:DOM2和DOM3 第12章:事件 第13章:表单脚本 第14章:错误处理与调试 第17章:Ajax和JSON第20章: ...

  6. 配置redis外网可访问

    redis采用的安全策略,默认会只准许本地访问 通过简单配置,完成允许外网访问 [root@cache01 conf]# egrep "(^bind|#bind|# bind)" ...

  7. sql:除非另外还指定了 TOP 或 FOR XML,否则,ORDER BY 子句在视图、内联函数、派生表、子查询

    执行sql语句: select * from ( select * from tab where ID>20 order by userID desc ) as a order by date ...

  8. Java Hour 55 Spring Framework 2

    上一章节估计被官方的说明文档扯晕了,其实说白了不就是个IOC 注入的容器么,用过了微软Enterprise Library 的Unity 的我还会怕这个.自己随便写个demo, 将知识的主题框架先构建 ...

  9. matlab练习程序(最小包围矩形)

    又是计算几何,我感觉最近对计算几何上瘾了. 当然,工作上也会用一些,不过工作上一般直接调用boost的geometry库. 上次写过最小包围圆,这次是最小包围矩形,要比最小包围圆复杂些. 最小包围矩形 ...

  10. cocos2dx游戏开发——微信打飞机学习笔记(九)——BulletLayer的搭建

    一.创建文件~ BulletLayer.h BulletLayer.cpp 二.How to do? (1)实例化BulletLayer方法的实现~ Bullet(PlayerLayer* temp) ...