【BZOJ-1208】宠物收养所 Splay
1208: [HNOI2004]宠物收养所
Time Limit: 10 Sec Memory Limit: 162 MB
Submit: 6638 Solved: 2601
[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
0 2
0 4
1 3
1 2
1 5
Sample Output
(abs(3-2) + abs(2-4)=3,最后一个领养者没有宠物可以领养)
HINT
Source
Solution
从数据范围及描述,很容易就想到是要用数据结构维护,支持前驱后继,插入和删除操作,很显然用Splay可以轻松解决
需要维护 宠物 和 收养者,第一眼想到要建两棵Splay,一棵维护宠物一棵维护收养者,并对两个同时判断,完成删除
但实际并不需要如此,只需要打个标记记录当前Splay所维护的东西,并分类讨论即可:
如果当前读进来的和现在所维护的一致,加入Splay;否则判断,删除前驱或后继,记录答案;若当前Splay为空,加入,并更改标记
Code
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
int read()
{
int x=,f=; char ch=getchar();
while (ch<'' || ch>'') {if (ch=='-') f=-; ch=getchar();}
while (ch>='' && ch<='') {x=x*+ch-''; ch=getchar();}
return x*f;
}
#define maxn 100010
int n,m;
int fa[maxn],son[maxn][],key[maxn],cnt[maxn],size[maxn];
int sz,root;
void clear(int now)
{
son[now][]=son[now][]=fa[now]=cnt[now]=key[now]=size[now];
}
int get(int now)
{
return son[fa[now]][]==now;
}
void update(int now)
{
if (!now) return;
size[now]=cnt[now];
if (son[now][]) size[now]+=size[son[now][]];
if (son[now][]) size[now]+=size[son[now][]];
}
void rotate(int now)
{
int old=fa[now],oldf=fa[old],which=get(now);
son[old][which]=son[now][which^]; fa[son[old][which]]=old;
fa[old]=now; son[now][which^]=old; fa[now]=oldf;
if (oldf) son[oldf][son[oldf][]==old]=now;
update(old); update(now);
}
void splay(int now)
{
for (int f; (f=fa[now]); rotate(now))
if (fa[f])
if (get(now)==get(f))
rotate(f); else rotate(now);
root=now;
}
void insert(int v)
{
if (!root)
{sz++;son[sz][]=son[sz][]=fa[sz]=;key[sz]=v;size[sz]=;cnt[sz]=;root=sz;return;}
int now=root,f=;
while (true)
{
if (key[now]==v)
{cnt[now]++;update(now);update(f);splay(now);break;}
f=now; now=son[now][key[now]<v];
if (now==)
{
sz++;son[sz][]=son[sz][]=;key[sz]=v;size[sz]=;
cnt[sz]=;fa[sz]=f;son[f][key[f]<v]=sz;
update(f);splay(sz);break;
}
}
}
int find(int v)
{
int ans=,now=root;
while (true)
{
if (v<key[now]) now=son[now][]; else
{
if (son[now][]) ans+=size[son[now][]];
if (v==key[now]) {splay(now); return ans+;}
ans+=cnt[now]; now=son[now][];
}
}
}
int prev()
{
int now=son[root][];
while (son[now][]) now=son[now][];
return now;
}
int succ()
{
int now=son[root][];
while (son[now][]) now=son[now][];
return now;
}
bool remove(int now)
{
if (find(now)==-) return false;
if (cnt[root]>) {cnt[root]--; return true;};
if (!son[root][] && !son[root][]) {clear(root); root=; return true;}
if (!son[root][])
{
int oldroot=root; root=son[root][]; fa[root]=; clear(oldroot); return true;
}
else if (!son[root][])
{
int oldroot=root; root=son[root][]; fa[root]=; clear(oldroot); return true;
}
int leftbig=prev(),oldroot=root;
splay(leftbig);
fa[son[oldroot][]]=root; son[root][]=son[oldroot][];
clear(oldroot); update(root); return true;
}
#define mod 1000000
int main()
{
n=read(); int pd=; int ans=;
for (int i=; i<=n; i++)
{
int opt=read(),x=read();
if (root==) {insert(x);pd=opt;continue;}
if (pd==opt) {insert(x);continue;}
if (pd!=opt)
{
insert(x); int pre=key[prev()],suc=key[succ()];
if (pre==) {ans=(ans+abs(suc-x))%mod;remove(x);remove(suc);continue;}
if (suc==) {ans=(ans+abs(pre-x))%mod;remove(x);remove(pre);continue;}
if (abs(x-pre)<=abs(x-suc)) {ans=(ans+abs(pre-x));remove(x);remove(pre);continue;}
if (abs(x-pre)>abs(x-suc)) {ans=(ans+abs(suc-x));remove(x);remove(suc);continue;}
}
}
while (ans<) ans+=mod;
printf("%d\n",ans);
return ;
}
【BZOJ-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 宠物收养所
Description 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物.每个领养者都希望领养到自己满意的宠物,阿Q根据领养者的要求通过他自己发明的一个特 ...
- BZOJ 1208 宠物收养所 set+二分
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1208 题目大意: 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠 ...
- Bzoj 1208: [HNOI2004]宠物收养所(splay)
1208: [HNOI2004]宠物收养所 Time Limit: 10 Sec Memory Limit: 162 MB Description 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收 ...
- HYSBZ 1208 宠物收养所 (Splay树)
题意:一家宠物收养所负责处理领养者与遗弃宠物业务,有人来领宠物,则领一只最理想的.若没有宠物了,领养者们就得等到宠物来,宠物一来立刻送给其中一个等待者.如果有两个理想的选择,则选择那个值较小的.收养所 ...
- 【BZOJ1208】[HNOI2004]宠物收养所 Splay
还是模板题,两颗splay,找点删即可. #include <iostream> #include <cstdio> #include <cstdlib> #def ...
- [bzoj1208][HNOI2004]宠物收养所——splay
题目大意 Description 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物.每个领养者都希望领养到自己满意的宠物,阿Q根据领养者的要求通过他自己发 ...
- HNOI2004宠物收养所(splay维护二叉搜索树模板题)
描述 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物.每个领养者都希望领养到自己满意的宠物,阿Q根据领养者的要求通过他自己发明的一个特殊的公式,得出该领 ...
随机推荐
- MySQL遇到check the manual that corresponds to your MySQL server version for the right syntax错误
用MySQL新建了一个Order表,插入了一条数据.总是显示 You have an error in your SQL syntax; check the manual thatcorrespond ...
- uGUI练习 开篇
一.准备阶段 1.Unity 4.6 Beta b18或更高版本(注:目前泄露版的Unity5.0Beta 对UI的支持并没有4.6 Beta那么好) 2.了解 Unity 2D Sprite的基础知 ...
- Lua笔记(1)
今天开始学习Lua,下面把一些重点记下来: 单行注释-- ,多行注释 --[[ ....... --]] Lua中false和nil表示条件判断的假,其余的,包括空字符串,0,都表示真. Lua没 ...
- 实例化Model的三种方式
- Jsp c标签数值格式化
整数带千分符显示:<fmt:formatNumber value="${num}" type="number"/> 整数显示:<fmt:for ...
- 21Spring_JdbcTemplatem模板工具类的使用——配置文件(连接三种数据库连接池)
上一篇文章提到过DriverManagerDataSource只是Spring内置的数据库连接池,我们可选的方案还有c3p0数据库连接池以及DBCP数据库连接池. 所以这篇文章讲一下上面三种数据库连接 ...
- 【Mysql】 my.ini配置一例
# For advice on how to change settings please see # http://dev.mysql.com/doc/refman/5.6/en/server-co ...
- [转]SQLServer跨服务器访问数据库(openrowset/opendatasource/openquery)
正 文: 1.启用Ad Hoc Distributed Queries 在使用openrowset/opendatasource前搜先要启用Ad Hoc Distributed Queries服务,因 ...
- WorldWind源码剖析系列:WorldWind实时确定、更新、初始化和渲染地形和纹理数据
WorldWind实时确定.更新.初始化和渲染地形和纹理数据 当用户点击WorldWind中的地球时,首先响应的是WorldWindow.OnPaint()函数,后续程序的调用流程如下图所示. 零散知 ...
- Android -- 创建数据库到SD卡
SQLite 系统自带的SQLite是通过SQLiteOpenHelper实现的,而SQLiteOpenHelper是将数据库存储到/data/data/包名/databasas,这样做的话在没有ro ...