HYSBZ 1208 宠物收养所 (Splay树)
题意:一家宠物收养所负责处理领养者与遗弃宠物业务,有人来领宠物,则领一只最理想的。若没有宠物了,领养者们就得等到宠物来,宠物一来立刻送给其中一个等待者。如果有两个理想的选择,则选择那个值较小的。收养所时刻只存在人or宠物or没有存在任何!
思路:
这题主要是树中可能存在人或者宠物,但两者不可能同时存在,因为一旦存在,必须有对象被配对并从树中删除。 所有函数独自写起来都是很简单的,但是合起来使用时就得小心了,得配合一点。
要时刻注意树中是否还有节点,若没有,则可能要树要换种类了。
#include <bits/stdc++.h>
#define pii pair<int,int>
#define INF 0x3f7f7f7f
#define LL long long
using namespace std;
const int N=;
const int mod=; struct node
{
int key, pre, ch[];
}nod[N];
int node_cnt, root, flag; int create_node(int v,int far)
{
nod[node_cnt].ch[]=nod[node_cnt].ch[]=;
nod[node_cnt].pre=far;
nod[node_cnt].key=v;
return node_cnt++;
} void Rotate(int t,int d)
{
int son=nod[t].ch[d];
int far=nod[t].pre;
int gra=nod[far].pre;
nod[t].pre=gra;
nod[far].pre=t;
nod[son].pre=far;
nod[t].ch[d]=far;
nod[far].ch[d^]=son;
nod[gra].ch[ nod[gra].ch[]==far ]=t;
} int Insert(int t,int v)
{
if(t==) return root=create_node(v, ); //树中还没有节点。
if( v < nod[t].key )
{
if(nod[t].ch[]) return Insert(nod[t].ch[], v);
else return nod[t].ch[]=create_node(v, t);
}
else
{
if(nod[t].ch[]) return Insert(nod[t].ch[], v);
else return nod[t].ch[]=create_node(v, t);
}
} void Splay(int t, int goal)
{
while(nod[t].pre!=goal)
{
int f=nod[t].pre, g=nod[f].pre;
if(g==goal) Rotate( t, nod[f].ch[]==t );
else
{
int d1=nod[f].ch[]==t, d2=nod[g].ch[]==f;
if(d1==d2) Rotate(f, d1),Rotate(t, d1);
else Rotate(t, d1),Rotate(t, d2);
}
}
if(goal==) root=t; //随时更新根!
} int Find(int t,int v) //在子树t中找到值为v的点,返回点号
{
while(t)
{
if(v==nod[t].key) return t;
if(v<nod[t].key) t=nod[t].ch[];
else t=nod[t].ch[];
}
return ; //找不到
} int Find_pre(int t,int v)
{
int val=-;
while(t)
{
if(nod[t].key<v) val=max(val, nod[t].key);
if(v<nod[t].key) t=nod[t].ch[];
else t=nod[t].ch[];
}
if(val<) return INF;
return val;
} int Find_bac(int t,int v)
{
int val=INF;
while(t)
{
if(nod[t].key>v) val=min(val, nod[t].key);
if(v<nod[t].key) t=nod[t].ch[];
else t=nod[t].ch[];
}
return val;
} void Delete(int v) //在树中删除值为v的任意一个点
{
Splay(Find(root, v), ); //将目标旋转到根
int L=nod[root].ch[];
int R=nod[root].ch[];
if(L== && R==) root=;
else if(R==) //没有右子树
{
nod[L].pre=;
root=L;
}
else if(L==) //没有左子树
{
nod[R].pre=;
root=R;
}
else //有前有后
{
while(nod[R].ch[]) R=nod[R].ch[]; //找到后继
Splay(R, root);
nod[R].ch[]=L;
nod[R].pre=;
nod[L].pre=R;
root=R;
}
} int main()
{
//freopen("input.txt", "r", stdin);
int n, ans, a, b, contain;
while(cin>>n)
{
flag=node_cnt=;
contain=root=ans=;
while(n--)
{
scanf("%d%d",&a,&b);
if(a==flag || contain==) // 树为空 or 树中是同种类 则 插入
{
flag=a;
contain++;
Splay(Insert(root, b), );
}
else
{
if(Find(root, b)) Delete(b); //刚好有值为b的。
else
{
int L=Find_pre(root, b);
int R=Find_bac(root, b);
if( abs(L-b)<=abs(R-b) ) //L和R不可能同时为INF。
{
ans=(ans+abs(L-b))%mod;
Delete(L);
}
else
{
ans=(ans+abs(R-b))%mod;
Delete(R);
}
}
contain--;
}
}
printf("%d\n", ans );
}
return ;
}
AC代码
HYSBZ 1208 宠物收养所 (Splay树)的更多相关文章
- bzoj 1208 宠物收养所--splay
这个题也是单点维护,不管来的是人还是狗,只要num=0就插入,否则就删除. // File Name: ACM/bzoj/1208.cpp // Author: Zlbing // Created T ...
- BZOJ 1208 宠物收养所 | 平衡树模板题
BZOJ 1208 宠物收养所 我犯过的错误:删除一个节点后没有update新的根节点,导致size错了! #include <cstdio> #include <cmath> ...
- Bzoj 1208: [HNOI2004]宠物收养所(splay)
1208: [HNOI2004]宠物收养所 Time Limit: 10 Sec Memory Limit: 162 MB Description 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收 ...
- 【BZOJ-1208】宠物收养所 Splay
1208: [HNOI2004]宠物收养所 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 6638 Solved: 2601[Submit][Sta ...
- 【BZOJ1208】[HNOI2004]宠物收养所 Splay
还是模板题,两颗splay,找点删即可. #include <iostream> #include <cstdio> #include <cstdlib> #def ...
- BZOJ 1208 宠物收养所
Description 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物.每个领养者都希望领养到自己满意的宠物,阿Q根据领养者的要求通过他自己发明的一个特 ...
- BZOJ 1208 宠物收养所 set+二分
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1208 题目大意: 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠 ...
- [bzoj1208][HNOI2004]宠物收养所——splay
题目大意 Description 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物.每个领养者都希望领养到自己满意的宠物,阿Q根据领养者的要求通过他自己发 ...
- HNOI2004宠物收养所(splay维护二叉搜索树模板题)
描述 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物.每个领养者都希望领养到自己满意的宠物,阿Q根据领养者的要求通过他自己发明的一个特殊的公式,得出该领 ...
随机推荐
- ubuntu下tesseract 4.0安装及参数使用
tesseract是一个开源的OCR引擎,最初是由惠普公司开发用来作为其平板扫描仪的OCR引擎,2005年惠普将其开源出来,之后google接手负责维护.目前稳定的版本是3.0.4.0版本加入了基 ...
- can't set android permissions - built without android support
/**************************************************************************** * can't set android pe ...
- ASP.NET WebServce项目下添加Http服务,支持Get,Post请求方式;传输格式json/xml
由于WEBServce老项目中需要增添新的接口,而且添加的接口不希望被其它项目以引用Servces方式使用. 那么得在现有Service项目中添加Http请求方式来实现系统间数据交互.只需要告知请求地 ...
- cocos2dx 游戏开发中常用场景切换方式以及特性
runWithScene(CCScene* scene):启动游戏,并运行scene 场景.这个方法在主程序启动时第一次启动主场景时调用. replaceScene(CCScene* scene) ...
- 出现ImportError: No module named win32api异常
ImportError: No module named win32api出现异常 实际是需要安装和自己python兼容的win32all 在http://starship.python.net/cr ...
- Collection View Programming Guide for iOS---(二)----Collection View Basics
Collection View Basics Collection View 基础 To present its content onscreen, a collection view coope ...
- eclipse+PyDev里面import win32api报错的问题解决
windows下面eclipse+PyDev的开发环境,安装了pywin32,写import win32api时老提示错误,在idle里正常执行. 原来是安装python库时,python安装路径下面 ...
- c语言sscanf总结
1函数原型 int scanf(const char *format,[,argument]...) extern int sscanf(_const char*_restrict_s,const c ...
- JAVA基础-面向对象06
一.封装 1. 封装概念和体现 封装:包装的意思,隐藏被封装的事物的信息:生活中的包装:快递:食品:电子产品……方便:好看易用:安全: Java程序中的包装: 类:包装了同一类事物的共性信息: 函数: ...
- 一个表的两个字段具有相同的类型。如何仅用SQL语句交换这两列的数据?
--假设为A B两个字段--查询Select A As B, B As A From TableName --更新Update TableName Set A = B, B = A