【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根据领养者的要求通过他自己发明的一个特 ...
随机推荐
- Step
php+MySQL html+css JQuery Mobile JavaScript weiPHP Sina Cloud 微信公众订阅号平台开发
- PHP表单验证
<!DOCTYPE html> <html> <head> <title>Test Code</title> </head> & ...
- Linux 的shell 字符串截取很有用。有八种方法。
一 Linux 的字符串截取很有用.有八种方法. 假设有变量 var=http://www.linuxidc.com/123.htm 1 # 号截取,删除左边字符,保留右边字符. echo ${va ...
- linux之eval用法(高级bash程序员的必修之技)
1. eval command-line 其中command-line是在终端上键入的一条普通命令行.然而当在它前面放上eval时,其结果是shell在执行命令行之前扫描它两次.如: pipe=&qu ...
- 【SpringMVC】SpringMVC系列12之数据类型转换、格式化、校验
12.数据类型转换.格式化.校验 12.1.数据绑定流程 Spring MVC 主框架将 ServletRequest 对象及目标方法的入参实例传递给 WebDataBinderFacto ...
- String to Integer
Implement function atoi to convert a string to an integer. If no valid conversion could be performed ...
- 【leetcode】Palindrome Partitioning
Palindrome Partitioning Given a string s, partition s such that every substring of the partition is ...
- 用cocos2dx实现模态对话框
ui部分使用了cocoStudio,注意这里没有实现怎么屏蔽其他的输入事件,其他的文档已经太多了,我这里使用的cocoStudio的控件自己的特性. 这里强烈推荐一下cocoStudio,虽然现在还有 ...
- iOS UIDatePicker frame改变问题
这种方法不行: pickerCtl = UIDatePicker(frame:pickerFrame) 但是这种却行 pickerCtl = UIDatePicker() pickerCtl!.fra ...
- iOS 中的字体预览
要预览iOS的各种字体的效果,可以访问http://iosfonts.com