Luogu P2286 [HNOI2004]宠物收养场
一道比较简单的直接Treap运用题目,思维难度和代码难度都不是很高。
题意有点长,我们仔细剖析一下题意发现以下几个关键:
- 任何时候收养站里只可能有人和宠物中的其中一种,或者都没有
- 如果只有宠物并有人来时,那个人会立刻挑选一只宠物并离开(只有人的时候同理)。
- 如果没有宠物并有人来时,那个人会一直等着直到有宠物到来并且那个宠物被它领养了(没有人的时候同理)。
然后我们思考一下那个选宠物(或者是宠物选人)的过程,其实就是在一个集合找一个数和它最接近的过程。
然后这道题已经很良心地提示你了:
存在两个宠物a-b,a+b......
其实仔细想一下就是一个找前驱和找后缀的问题,我们找出前驱和后继就可以直接统计并删除了。
具体实现的时候开两个Treap,一棵存宠物,一棵存人(这里由于宠物和人只会同时存在一种,所以其实开一棵也可以),然后按题意搞一下就可以了。
然后我写了两个namespace,所以主程序看上去有点乱。
最后注意开long long
CODE
#include<cstdio>
#include<cctype>
using namespace std;
typedef long long LL;
const LL N=80005,INF=1e18,mod=1000000;
LL n,opt,x,ans;
inline char tc(void)
{
static char fl[100000],*A=fl,*B=fl;
return A==B&&(B=(A=fl)+fread(fl,1,100000,stdin),A==B)?EOF:*A++;
}
inline void read(LL &x)
{
x=0; char ch; while (!isdigit(ch=tc()));
while (x=(x<<3)+(x<<1)+ch-'0',isdigit(ch=tc()));
}
inline LL rand(void)
{
static LL seed=233;
return seed=(LL)seed*482711LL%2147483647;
}
inline LL min(LL a,LL b)
{
return a<b?a:b;
}
namespace pet
{
struct Treap
{
LL ch[2],val,dat;
}node[N];
LL tot,rt,size;
inline LL build(LL v)
{
node[++tot].val=v; node[tot].dat=rand(); return tot;
}
inline void init(void)
{
rt=build(-INF); node[rt].ch[1]=build(INF);
}
inline void rotate(LL &rt,LL d)
{
LL temp=node[rt].ch[d^1]; node[rt].ch[d^1]=node[temp].ch[d];
node[temp].ch[d]=rt; rt=temp;
}
inline void insert(LL &rt,LL v)
{
if (!rt) { rt=build(v); return; }
LL d=v<node[rt].val?0:1; insert(node[rt].ch[d],v);
if (node[rt].dat<node[node[rt].ch[d]].dat) rotate(rt,d^1);
}
inline void remove(LL &rt,LL v)
{
if (node[rt].val==v)
{
if (node[rt].ch[0]||node[rt].ch[1])
{
if (!node[rt].ch[1]||node[node[rt].ch[1]].dat<node[node[rt].ch[0]].dat) rotate(rt,1),remove(node[rt].ch[1],v);
else rotate(rt,0),remove(node[rt].ch[0],v);
} else rt=0; return;
}
if (v<node[rt].val) remove(node[rt].ch[0],v); else remove(node[rt].ch[1],v);
}
inline LL get_pre(LL rt,LL v)
{
LL pre;
while (rt)
{
if (node[rt].val<=v) pre=node[rt].val,rt=node[rt].ch[1];
else rt=node[rt].ch[0];
}
return pre;
}
inline LL get_next(LL rt,LL v)
{
LL next;
while (rt)
{
if (node[rt].val>=v) next=node[rt].val,rt=node[rt].ch[0];
else rt=node[rt].ch[1];
}
return next;
}
};
namespace hum
{
struct Treap
{
LL ch[2],val,dat;
}node[N];
LL tot,rt,size;
inline LL build(LL v)
{
node[++tot].val=v; node[tot].dat=rand(); return tot;
}
inline void init(void)
{
rt=build(-INF); node[rt].ch[1]=build(INF);
}
inline void rotate(LL &rt,LL d)
{
LL temp=node[rt].ch[d^1]; node[rt].ch[d^1]=node[temp].ch[d];
node[temp].ch[d]=rt; rt=temp;
}
inline void insert(LL &rt,LL v)
{
if (!rt) { rt=build(v); return; }
LL d=v<node[rt].val?0:1; insert(node[rt].ch[d],v);
if (node[rt].dat<node[node[rt].ch[d]].dat) rotate(rt,d^1);
}
inline void remove(LL &rt,LL v)
{
if (node[rt].val==v)
{
if (node[rt].ch[0]||node[rt].ch[1])
{
if (!node[rt].ch[1]||node[node[rt].ch[1]].dat<node[node[rt].ch[0]].dat) rotate(rt,1),remove(node[rt].ch[1],v);
else rotate(rt,0),remove(node[rt].ch[0],v);
} else rt=0; return;
}
if (v<node[rt].val) remove(node[rt].ch[0],v); else remove(node[rt].ch[1],v);
}
inline LL get_pre(LL rt,LL v)
{
LL pre;
while (rt)
{
if (node[rt].val<=v) pre=node[rt].val,rt=node[rt].ch[1];
else rt=node[rt].ch[0];
}
return pre;
}
inline LL get_next(LL rt,LL v)
{
LL next;
while (rt)
{
if (node[rt].val>=v) next=node[rt].val,rt=node[rt].ch[0];
else rt=node[rt].ch[1];
}
return next;
}
};
int main()
{
//freopen("CODE.in","r",stdin); freopen("CODE.out","w",stdout);
pet::init(); hum::init(); read(n);
while (n--)
{
read(opt); read(x);
if (opt)
{
if (pet::size)
{
LL pre=pet::get_pre(pet::rt,x),next=pet::get_next(pet::rt,x); --pet::size;
if (pre==-INF||x-pre>next-x) ans=(ans+next-x)%mod,pet::remove(pet::rt,next);
else ans=(ans+x-pre)%mod,pet::remove(pet::rt,pre);
} else ++hum::size,hum::insert(hum::rt,x);
} else
{
if (hum::size)
{
LL pre=hum::get_pre(hum::rt,x),next=hum::get_next(hum::rt,x); --hum::size;
if (pre==-INF||x-pre>next-x) ans=(ans+next-x)%mod,hum::remove(hum::rt,next);
else ans=(ans+x-pre)%mod,hum::remove(hum::rt,pre);
} else ++pet::size,pet::insert(pet::rt,x);
}
}
return printf("%lld",ans),0;
}
Luogu P2286 [HNOI2004]宠物收养场的更多相关文章
- P2286 [HNOI2004]宠物收养场
题目描述 凡凡开了一间宠物收养场.收养场提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物. 每个领养者都希望领养到自己满意的宠物,凡凡根据领养者的要求通过他自己发明的一个特殊的公式,得出该领 ...
- 洛谷P2286 [HNOI2004]宠物收养场【Treap】题解+AC代码
题目传送门啦~啦~啦~ 题目描述 凡凡开了一间宠物收养场.收养场提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物. 每个领养者都希望领养到自己满意的宠物,凡凡根据领养者的要求通过他自己发明的 ...
- 洛谷P2286 [HNOI2004]宠物收养场
题目描述 凡凡开了一间宠物收养场.收养场提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物. 每个领养者都希望领养到自己满意的宠物,凡凡根据领养者的要求通过他自己发明的一个特殊的公式,得出该领 ...
- 洛谷 P2286 [HNOI2004]宠物收养场
题目描述 凡凡开了一间宠物收养场.收养场提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物. 每个领养者都希望领养到自己满意的宠物,凡凡根据领养者的要求通过他自己发明的一个特殊的公式,得出该领 ...
- 洛谷P2286 [HNOI2004]宠物收养所 [STL,平衡树]
题目传送门 宠物收养所 题目描述 凡凡开了一间宠物收养场.收养场提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物. 每个领养者都希望领养到自己满意的宠物,凡凡根据领养者的要求通过他自己发明的 ...
- [HNOI2004]宠物收养场 Treap前驱后继
凡凡开了一间宠物收养场.收养场提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物. 每个领养者都希望领养到自己满意的宠物,凡凡根据领养者的要求通过他自己发明的一个特殊的公式,得出该领养者希望领 ...
- BZOJ1208[HNOI2004]宠物收养场——treap
凡凡开了一间宠物收养场.收养场提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物. 每个领养者都希望领养到自己满意的宠物,凡凡根据领养者的要求通过他自己发明的一个特殊的公式,得出该领养者希望领 ...
- [HNOI2004]宠物收养场 BZOJ1208 splay tree
题目描述 凡凡开了一间宠物收养场.收养场提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物. 每个领养者都希望领养到自己满意的宠物,凡凡根据领养者的要求通过他自己发明的一个特殊的公式,得出该领 ...
- LG_2286_[HNOI2004]宠物收养场
题目描述 凡凡开了一间宠物收养场.收养场提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物. 每个领养者都希望领养到自己满意的宠物,凡凡根据领养者的要求通过他自己发明的一个特殊的公式,得出该领 ...
随机推荐
- loadrunner 运行脚本-Run-time Settings-Browser Enmulation设置详解
运行脚本-Run-time Settings-Browser Enmulation设置详解 by:授客 QQ:1033553122 浏览器模拟 所有Internet Vuser Header包含一个标 ...
- NoHttp封装--02 自定义请求
bean实体类请求: 1.bean import java.io.Serializable; import com.alibaba.fastjson.annotation.JSONField; pub ...
- JVM vs DVM
- springboot 升级到2.0后 context-path 配置 不起作用,不生效 不管用 皆是因为版本改动导致的在这里记录一下
不知不觉,新的项目已经将springboot升级为2.0版本了.刚开始没有配置server.contextpath,默认的“/”,然后今天放到自己的服务器上,所以就要规范名称. 结果,失败了,无论我 ...
- (网页)jQuery UI 实例 - 日期选择器(Datepicker)
默认功能 日期选择器(Datepicker)绑定到一个标准的表单 input 字段上.把焦点移到 input 上(点击或者使用 tab 键),在一个小的覆盖层上打开一个交互日历.选择一个日期,点击页面 ...
- [20171221]利用rman实现2台机器文件拷贝.txt
[20171221]利用rman实现2台机器文件拷贝.txt --//昨天使用rman duplicate建立dg,我看到执行如下代码: RMAN> duplicate target datab ...
- linux kernel 源码安装
有时我们在安装系统后,发现没有安装当前系统的内核源码在/usr/src/kernels目录下,其实我们是少安装了一个rpm包: 当你配置好yum源后: yum install kernel-devel ...
- Ubuntu18.04 更换源
在虚拟机新建一个Ubuntu18.04.1-live-server-amd64当做服务器 在安装软件时报错: slave@slave:~$ sudo -s[sudo] password for sla ...
- Fragment分解使用
Fragment碎片:作为Activity的一部分,不能单独使用: 1. Fragment特点: (1)一个Fragment可以在多个Activity中重用: (2)一个Activity内部可以嵌入多 ...
- iOS-单选cell的实现
一.思路 先设置一个chooseCelltag标记(类型为NSIndexPath),然后在点击cell触发的时候,如果tag设置有值,就设置 UITableViewCell *selectedCell ...