题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1208

题意:

  有一个宠物收养所,在接下来一段时间内会陆续有一些宠物进到店里,或是一些人来领养宠物。宠物和人的总数量为n。

  t = 0代表宠物,t = 1代表人。

  宠物和人分别有一个特点值a和b。有两种情况:

    (1)店里全是宠物,这时进来一个人来领养宠物。那么他会领养走abs(a-b)最小的那只宠物(b = a - x or a + x)。

      如果使得abs(a-b)最小的有不止一只宠物,那么b = a - x的那只会被领养走。

    (2)店里全是等待的人。这事有一只宠物被送进店里,那么它会被abs(a-b)最小的那个人领养走(a = b - x or b + x)。

      如果使得abs(a-b)最小的不只有一个人,那么宠物会被a = b - x的人领养走。

  按照依次来到店里的顺序,给出a,b。

  a = 0代表宠物,a = 1代表人。b为对应的特点值。

  定义每一次领养的不满意度 = abs(宠物特点值 - 人的特点值)

  问你所有不满意度之和 MOD 1000000。

题解:

  Splay。

  分析:

    (1)在每一个时刻(除了正在领养),店里要么全是宠物,要么全是人,要么为空。

    (2)在本题中,除了只能是人和宠物匹配这个特性,人和宠物是等价的,都可以在店里等待。

    (3)有可能先进来一大堆宠物,再进来一大堆人。这时搜索树会退化成链,所以要用splay。

  实现:

    每输入一对a,b:

    (1)如果店为空,那么将东西加入树中,并将树的类型改为a。

    (2)如果进来一个同类,那么将当前东西加入树中。

    (3)如果进来一个异类,那么找出对于这个异类的特点值的前驱pre与后继suc(类型均为pair<int,int>(dif,idx))。

      将被删除的东西为res = min(pre,suc)。

      所以erase(res.second),并更新答案ans = (ans + res.first) % MOD。

AC Code:

 #include <iostream>
#include <stdio.h>
#include <string.h>
#define MAX_N 80005
#define MOD 1000000
#define INF 100000000 using namespace std; int n,a,b;
int ans=;
int type;
int siz=;
int tot=;
int root=-;
int dat[MAX_N];
int par[MAX_N];
int lson[MAX_N];
int rson[MAX_N]; void rotate(int x,int type,int *lson,int *rson)
{
if(type==) swap(lson,rson);
int y=par[x];
int z=par[y];
lson[y]=rson[x];
if(rson[x]!=-) par[rson[x]]=y;
par[x]=z;
if(z!=-)
{
if(lson[z]==y) lson[z]=x;
else rson[z]=x;
}
rson[x]=y;
par[y]=x;
} void splay(int x,int act)
{
while(par[x]!=act)
{
int y=par[x];
int z=par[y];
if(z==act)
{
if(lson[y]==x) rotate(x,,lson,rson);
else rotate(x,,lson,rson);
}
else
{
if(lson[z]==y && lson[y]==x)
{
rotate(y,,lson,rson);
rotate(x,,lson,rson);
}
if(rson[z]==y && rson[y]==x)
{
rotate(y,,lson,rson);
rotate(x,,lson,rson);
}
if(rson[z]==y && lson[y]==x)
{
rotate(x,,lson,rson);
rotate(x,,lson,rson);
}
if(lson[z]==y && rson[y]==x)
{
rotate(x,,lson,rson);
rotate(x,,lson,rson);
}
}
}
if(act==-) root=x;
} int find(int val)
{
int now=root;
while(now!=-)
{
if(val==dat[now]) return now;
if(val<dat[now]) now=lson[now];
if(val>dat[now]) now=rson[now];
}
return -;
} void new_node(int val,int p,int &kid)
{
dat[tot]=val;
par[tot]=p;
lson[tot]=-;
rson[tot]=-;
kid=tot;
splay(tot,-);
tot++;
} void insert(int val)
{
siz++;
if(root==-)
{
int temp;
root=tot;
new_node(val,-,temp);
return;
}
int now=root;
while()
{
if(val==dat[now]) return;
if(val<dat[now])
{
if(lson[now]==-)
{
new_node(val,now,lson[now]);
return;
}
else now=lson[now];
}
if(val>dat[now])
{
if(rson[now]==-)
{
new_node(val,now,rson[now]);
return;
}
else now=rson[now];
}
}
} void erase(int x)
{
siz--;
splay(x,-);
if(lson[x]==- && rson[x]==-)
{
root=-;
return;
}
if(lson[x]==-)
{
root=rson[x];
par[rson[x]]=-;
return;
}
if(rson[x]==-)
{
root=lson[x];
par[lson[x]]=-;
return;
}
int pre=lson[root];
while(rson[pre]!=-) pre=rson[pre];
splay(pre,x);
par[pre]=-;
rson[pre]=rson[x];
par[rson[x]]=pre;
root=pre;
} pair<int,int> precursor(int val)
{
int now=root;
int pre=-;
int dif=INF;
while(now!=-)
{
if(dat[now]<val && val-dat[now]<dif)
{
pre=now;
dif=val-dat[now];
}
if(dat[now]>val) now=lson[now];
else now=rson[now];
}
return pair<int,int>(dif,pre);
} pair<int,int> succeed(int val)
{
int now=root;
int suc=-;
int dif=INF;
while(now!=-)
{
if(dat[now]>val && dat[now]-val<dif)
{
suc=now;
dif=dat[now]-val;
}
if(dat[now]>val) now=lson[now];
else now=rson[now];
}
return pair<int,int>(dif,suc);
} int main()
{
cin>>n;
for(int i=;i<n;i++)
{
cin>>a>>b;
if(siz==)
{
insert(b);
type=a;
}
else
{
if(a==type) insert(b);
else
{
pair<int,int> pre=precursor(b);
pair<int,int> suc=succeed(b);
pair<int,int> res=min(pre,suc);
erase(res.second);
ans=(ans+res.first)%MOD;
}
}
}
cout<<ans<<endl;
}

BZOJ 1208 [HNOI2004]宠物收养所:Splay(伸展树)的更多相关文章

  1. Bzoj 1208: [HNOI2004]宠物收养所(splay)

    1208: [HNOI2004]宠物收养所 Time Limit: 10 Sec Memory Limit: 162 MB Description 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收 ...

  2. BZOJ 1208 [HNOI2004]宠物收养所 | SPlay模板题

    题目: 洛谷也能评 题解: 记录一下当前树维护是宠物还是人,用Splay维护插入和删除. 对于任何一次询问操作都求一下value的前驱和后继(这里前驱和后继是可以和value相等的),比较哪个差值绝对 ...

  3. BZOJ 1208: [HNOI2004]宠物收养所

    1208: [HNOI2004]宠物收养所 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 7514  Solved: 2982[Submit][Sta ...

  4. BZOJ 1208: [HNOI2004]宠物收养所(BST)

    本来想先用set写一遍,再自己写个splay或treap,不过用set过了之后就懒得去写了....以后有空再来写吧..(不会有空的吧= = ------------------------------ ...

  5. BZOJ 1208: [HNOI2004]宠物收养所 SET的妙用

    1208: [HNOI2004]宠物收养所 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 4902  Solved: 1879 题目连接 http:/ ...

  6. bzoj 1208: [HNOI2004]宠物收养所 set

    1208: [HNOI2004]宠物收养所 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 7328  Solved: 2892[Submit][Sta ...

  7. bzoj 1208: [HNOI2004]宠物收养所 (Treap)

    链接:  https://www.lydsy.com/JudgeOnline/problem.php?id=1208 题面: 1208: [HNOI2004]宠物收养所 Time Limit: 10 ...

  8. bzoj 1208 HNOI2004宠物收养所 平衡树

    裸平衡树,恢复手感用的 //By BLADEVIL var n :longint; i :longint; x, y :longint; t, tot :longint; key, s, left, ...

  9. 1208: [HNOI2004]宠物收养所

    1208: [HNOI2004]宠物收养所 Time Limit: 10 Sec Memory Limit: 162 MB Submit: 12030 Solved: 4916 Description ...

随机推荐

  1. 用PROXYCHAINS实现SSH全局代理

    NUX下可以实现SSH全局代理的软件有tsocks和proxychains两种,但是个人感觉proxychains要更加稳定简单. $ yum install proxychains # vim /e ...

  2. jsp中jquery用法一步刷新 验证用户名是否存在

    <script type="text/javascript"> /* $(document).ready(function(){ var id="ha&quo ...

  3. Action window Flags

    Action window 主要字段使用 含义     target 值 作用 current 当前窗口 new 新窗口 inline 内联编辑 fullscreen 全屏 main 当前窗口的主动作 ...

  4. 【Excle数据透透视表】如何删除数据透视表

    选中区域A4:C17,在键盘上按DELETE键删除,结果提示: 那么如何删除呢? 解决方案 选中整个数透视表,再删除 具体操作: 选中整个数据透视表→DELETE 注意:删除之后,源数据不会受到影响

  5. 设计模式之MVC设计模式初阶

    MVC M:Model(数据) V:View(界面) C:Control(控制) 1⃣️Control可以直接访问View和Model 2⃣️View不可以拥有Control和Model属性,降低耦合 ...

  6. 朴素贝叶斯分类算法-----java

    1.贝叶斯分类的基础--贝叶斯定理 已知某条件概率.怎样得到两个事件交换后的概率,也就是在已知P(A|B)的情况下怎样求得P(B|A). 这里先解释什么是条件概率: 表示事件B已经发生的前提下,事件A ...

  7. HBase——完全分布

    实际上,在真实环境中你需要使用完全分布配置完整测试HBase.在一个分布式配置中,集群有多个节点,每个节点运行一个或多个HBase守护进程.其中包括主Master和备份Master实例,多个Zooke ...

  8. cenos7 修改hostname

    hostnamectl set-hostname Linuxidc 如何在CentOS 7上修改主机名 在CentOS中,有三种定义的主机名:静态的(static),瞬态的(transient),和灵 ...

  9. 网络通信数据处理 Xbytestring类

    PS_Xbytestring a byte string for store low level data type 文件夹[TOC] PS_Xbytestring 文件夹TOC base info ...

  10. Django--网页管理实例解析

    此篇为代码流程的注释以及自己写的小项目的思路: 首先是项目的路由配置: urlpatterns = [ # url(r'^admin/', admin.site.urls), url(r'^yemia ...