题目链接: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. 出现“Windows资源管理器已停止工作”错误

    出现"Windows资源管理器已停止工作"错误 什么是资源管理器呢,explorer.exe进程的作用就是让我们管理计算机中的资源! 今天开电脑的时候就一直提示windows资源管 ...

  2. Vue DOM事件

    本文参考自:https://mp.weixin.qq.com/s?src=3&timestamp=1527154113&ver=1&signature=tWGeTa86gyK* ...

  3. UNP学习笔记(第七章 套接字选项)

    有多种方法获取和设置影响套接字的选项: 1.getsockopt和setsockopt函数 2.fcntl函数 3.ioctl函数 getsockopt和setsockopt函数 这两个函数仅用于套接 ...

  4. UNP学习笔记(第五章 TCP客户/服务程序实例)

    我们将在本章使用前一章中介绍的基本函数编写一个完整的TCP客户/服务器程序实例 这个简单得例子是执行如下步骤的一个回射服务器: TCP回射服务器程序 #include "unp.h" ...

  5. SharePoint 的PowerShell命令之获取所有网站模版

    Get-SPWebTemplate | select Name, Title

  6. Linux内存段的分析

        Linux 应用程序的内存分配中,是用 segment(段)进行区别的,使用 size 命令进行查看: size a.out text data bss dec hex filename a. ...

  7. 转:office 2016最新安装及激活教程(KMS)

    office 2016最新安装及激活教程(KMS)[亲测有效]!!   win7激活教程 博主的一个朋友,咳咳……你们懂得,想装office,于是我就上网找了一下激活的方法,亲测有效,而且也没有什么广 ...

  8. Lua学习七----------Lua函数

    © 版权声明:本文为博主原创文章,转载请注明出处 1.Lua函数 - 完成指定的任务,这种情况下函数作为调用语句使用 - 计算并返回值,这种情况下函数作为赋值语句的表达式使用 - Lua函数可以返回多 ...

  9. JVM完全指南

    JVM完全指南     一:虚拟机内存图解   JAVA程序运行与虚拟机之上,运行时需要内存空间.虚拟机执行JAVA程序的过程中会把它管理的内存划分为不同的数据区域方便管理.     虚拟机管理内存数 ...

  10. HTTP状态码介绍详细

    HTTP协议中几个状态码的含义:1xx(临时响应) 表示临时响应并需要请求者继续执行操作的状态代码. 代码 说明 100 (继续) 请求者应当继续提出请求. 服务器返回此代码表示已收到请求的第一部分, ...