【wikioi】1285 宠物收养所
题目链接:http://www.wikioi.com/problem/1285/
算法:Splay
刚开始看到这题,就注意到特征abs了,并且数据n<=80000显然不能暴力,只能用nlgn的做法,综合起来,似乎只有一个答案:Splay。
将每次插入的点splay到树根,然后找到它的前驱和后继,再取它的绝对值即可,我们记作_abs。那么答案就为sum{_absi},其中i为领养的人的数量,可以看为宠物的数量。
要注意的:
- 领养者将会领养特点值为a-b的那只宠物 和 特点值为a-b的那个领养者将成功领养该宠物 告诉我们要取前驱。
- 同一时间呆在收养所中的,要么全是宠物,要么全是领养者,这些宠物和领养者的个数不会超过10000个。 告诉我们要建两棵Splay树来存剩下的人。
- 操作人的和操作动物的几乎一样。
==========================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 宠物收养所的更多相关文章
- splay树 1285 宠物收养所
#include<cstdio> #include<iostream> using namespace std; int shu[80004][2],n,size,root,k ...
- C++之路进阶——codevs1285(宠物收养所)
1285 宠物收养所 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 最近,阿Q开了一间宠物收养所.收养所提供两种服 ...
- HNOI2004 宠物收养所 (Treap)
1285 宠物收养所 http://codevs.cn/problem/1285/ 时间限制: 1 s 空间限制: 128000 KB 题目描述 Description 最近,阿Q开了一间 ...
- Bzoj1208 [HNOI2004]宠物收养所
Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 7457 Solved: 2960 Description 最近,阿Q开了一间宠物收养所.收养所提供两 ...
- BZOJ 1208: [HNOI2004]宠物收养所
1208: [HNOI2004]宠物收养所 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 7514 Solved: 2982[Submit][Sta ...
- 宠物收养所(bzoj1208)
Description 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物.每个领养者都希望领养到自己满意的宠物,阿Q根据领养者的要求通过他自己发明的一个特 ...
- 【BZOJ1208】[HNOI2004]宠物收养所 Splay
还是模板题,两颗splay,找点删即可. #include <iostream> #include <cstdio> #include <cstdlib> #def ...
- 【BZOJ-1208】宠物收养所 Splay
1208: [HNOI2004]宠物收养所 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 6638 Solved: 2601[Submit][Sta ...
- BZOJ1208 宠物收养所
Description 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物.每个领养者都希望领养到自己满意的宠物,阿Q根据领养者的要求通过他自己发明的一个特 ...
随机推荐
- 【云计算】docker的小知识,帮你更深入理解容器技术
关于docker的15个小tip 1. 获取最近运行容器的id 这是我们经常会用到的一个操作,按照官方示例,你可以这样做(环境ubuntu): $ ID=$(docker run ubuntu e ...
- Balanced Binary Tree
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- iOS 推荐学习__bridge等ARC知识的好资料
请下载 iOS5 by Tutorials!写得很好的!
- zpf 视图
2014年8月19日 18:12:16 smarty使用了2年, 使用PHP本身做模版引擎也有4个多月了, 最终还是在我的这个框架中抛弃了smarty,转用原生的PHP代码做模版引擎,并简单写了一个视 ...
- (转)JAVA AJAX教程第一章-初始AJAX
既然是认识AJAX,理论和实践相结合,这样让自己学的更快,理解更深入,我分一下几点: 1. 认识传统的同步交互方式和AJAX解决方案 2. AJAX使用到的技术 3. 实例体验AJAX 一.同步 ...
- Linux下的ip命令,除了ifconfig还有很多
linux的ip命令和ifconfig类似,但前者功能更强大,并旨在取代后者.使用ip命令,只需一个命令,你就能很轻松地执行一些网络管理任务.ifconfig是net-tools中已被废弃使用的一个命 ...
- linux之间文件传输问题
如果linux服务器使用了秘钥登陆,可以先关闭秘钥登陆 http://blog.chinaunix.net/uid-23634108-id-2393471.html 然后:scp -P 端口号 no ...
- Maven使用笔记(二)Eclipse中maven项目添加依赖
1.在Eclipse中创建Eclipse项目后如何添加jar包? 点击pom.xml文件.我们可以看到下面有7个标签. 各个标签的含义如下: Overview:显示maven项目的一些基本信息Depe ...
- 有哪些关于 Android 开发的博客值得订阅?
链接:http://www.zhihu.com/question/19788650/answer/60771437来源:知乎 Google 官方[Android Developers Blog](An ...
- PHP5.3 goto操作符介绍
goto操作符是PHP5.+后新增功能,用来跳转到程序的另一位置:用法很简单:goto后面带上目标位置的标志,在目标位置上用目标名加冒号标记如下: <?php goto a; echo 'aaa ...