刷题总结——宠物收养所(bzoj1208)
题目:
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,最后一个领养者没有宠物可以领养)
题解:
splay模板题··当然用set做更简单···,按照题意往splay树种加入人或宠物的特殊值,按照题意加入后要么树种全是宠物或人的特殊值,此时直接继续加入操作;要么有一个人的特殊值剩下的全是宠物,或者有一个宠物的特殊值剩下的全是人,因此直接按照题意删除一个人和一个宠物(即一次领养)即可;注意用cnt1和cnt0来记录人和宠物在树中的数量
代码:
1.splay
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<cctype>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
const int N=8e4+;
const int mod=;
int root,tot,size[N],son[N][],father[N],key[N];
int n,cnt0=,cnt1=,a,b;
long long ans=;
inline int R()
{
char c;int f=;
for(c=getchar();c<''||c>'';c=getchar());
for(;c<=''&&c>='';c=getchar())
f=(f<<)+(f<<)+c-'';
return f;
}
long long Abs(long long x)
{
return x<?-x:x;
}
inline void update(int now)
{
if(now)
{
size[now]=;
if(son[now][]) size[now]+=size[son[now][]];
if(son[now][]) size[now]+=size[son[now][]];
}
}
inline void clear(int now)
{
size[now]=son[now][]=son[now][]=father[now]=key[now]=;
}
inline int get(int now)
{
return son[father[now]][]==now;
}
inline void rotate(int now)
{
int fa=father[now],ofa=father[fa],which=get(now);
son[fa][which]=son[now][which^],father[son[fa][which]]=fa;
son[now][which^]=fa,father[fa]=now,father[now]=ofa;
if(ofa) son[ofa][son[ofa][]==fa]=now;
update(fa),update(now);
}
inline void splay(int now)
{
while(father[now])
{
if(father[father[now]])
rotate(get(now)==get(father[now])?father[now]:now);
rotate(now);
}
root=now;
}
inline void find(int x)
{
int now=root;
while(true)
{
if(key[now]==x) {splay(now);break;}
else if(x>key[now]) now=son[now][];
else now=son[now][];
}
}
inline void insert(int x)
{
int now=root,last=;
while(true)
{
if(!now)
{
now=++tot;size[now]=;father[now]=last,key[now]=x;
son[last][x>key[last]]=now;update(last);
splay(now);
break;
}
last=now;
now=son[now][x>key[now]];
}
}
inline int pre()
{
int now=son[root][];
while(son[now][]) now=son[now][];
return now;
}
inline int next()
{
int now=son[root][];
while(son[now][]) now=son[now][];
return now;
}
inline long long prex()
{
int now=son[root][];
if(!now) return 1e+;
while(son[now][]) now=son[now][];
return key[now];
}
inline long long nextx()
{
int now=son[root][];
if(!now) return 1e+;
while(son[now][]) now=son[now][];
return key[now];
}
inline void Delete(int x)
{
if(x==1e+) return;
find(x);
if(!son[root][]&&!son[root][]){clear(root);root=;return;}
else if(!son[root][]){int oldroot=root;root=son[oldroot][];father[root]=;clear(oldroot);return;}
else if(!son[root][]){int oldroot=root;root=son[oldroot][];father[root]=;clear(oldroot);return;}
else
{
int leftbig=pre(),oldroot=root;
splay(leftbig);
son[root][]=son[oldroot][];father[son[root][]]=root;
clear(oldroot);update(root);
return;
}
}
int main()
{
//freopen("a.in","r",stdin);
n=R();
for(int i=;i<=n;i++)
{
a=R(),b=R();
if(!cnt0&&!cnt1)
{
if(a==) cnt0++;
else cnt1++;
insert(b);
}
else if(!cnt0&&a==)
{
cnt1++;insert(b);
}
else if(!cnt1&&a==)
{
cnt0++;insert(b);
}
else
{
if(!cnt1) cnt0--;
else if(!cnt0) cnt1--;
insert(b);
long long temp1=prex();
long long temp2=nextx();
long long ans1=Abs(temp1-b);
long long ans2=Abs(temp2-b);
if(ans1==ans2) Delete(temp1),Delete(b);
if(ans1>ans2) Delete(temp2),Delete(b);
if(ans1<ans2) Delete(temp1),Delete(b);
ans=(ans+min(ans1,ans2))%mod;
}
}
cout<<ans<<endl;
return ;
}
2.set
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<cctype>
#include<cstring>
#include<string>
#include<algorithm>
#include<set>
using namespace std;
const int N=8e4+;
const int mod=;
const int inf=1e+;
set<int>st;
int n,cnt0=,cnt1=,a,b;
long long ans=;
inline int R()
{
char c;int f=;
for(c=getchar();c<''||c>'';c=getchar());
for(;c<=''&&c>='';c=getchar())
f=(f<<)+(f<<)+c-'';
return f;
}
long long Abs(long long x)
{
return x<?-x:x;
}
int main()
{
//freopen("a.in","r",stdin);
n=R();
st.insert(-inf);
st.insert(inf);
for(int i=;i<=n;i++)
{
a=R(),b=R();
if(!cnt0&&!cnt1)
{
if(a==) cnt0++;
else cnt1++;
st.insert(b);
}
else if(!cnt0&&a==)
{
cnt1++;st.insert(b);
}
else if(!cnt1&&a==)
{
cnt0++;st.insert(b);
}
else
{
if(!cnt1) cnt0--;
else if(!cnt0) cnt1--;
set<int>::iterator l=--st.lower_bound(b),r=st.lower_bound(b);
if(b-*l<=*r-b&&*l!=-inf)
{
ans+=b-*l;
st.erase(l);
}
else
{
ans+=*r-b;
st.erase(r);
}
ans%=mod;
}
}
printf("%d",ans);
return ;
}
刷题总结——宠物收养所(bzoj1208)的更多相关文章
- [HNOI2004]宠物收养场 BZOJ1208 splay tree
题目描述 凡凡开了一间宠物收养场.收养场提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物. 每个领养者都希望领养到自己满意的宠物,凡凡根据领养者的要求通过他自己发明的一个特殊的公式,得出该领 ...
- Bzoj1208 [HNOI2004]宠物收养所
Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 7457 Solved: 2960 Description 最近,阿Q开了一间宠物收养所.收养所提供两 ...
- 【BZOJ1208】[HNOI2004]宠物收养所 Splay
还是模板题,两颗splay,找点删即可. #include <iostream> #include <cstdio> #include <cstdlib> #def ...
- 2018.07.06 BZOJ1208: HNOI2004宠物收养所(非旋treap)
1208: [HNOI2004]宠物收养所 Time Limit: 10 Sec Memory Limit: 162 MB Description 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收 ...
- BZOJ1208 HNOI2004 宠物收养所 【非旋转Treap】
BZOJ1208 HNOI2004 宠物收养所 Description 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物.每个领养者都希望领养到自己满意的 ...
- BZOJ1208:[HNOI2004]宠物收养所——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=1208 Description 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠物 ...
- HNOI2004宠物收养所(splay维护二叉搜索树模板题)
描述 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物.每个领养者都希望领养到自己满意的宠物,阿Q根据领养者的要求通过他自己发明的一个特殊的公式,得出该领 ...
- [BZOJ1208]宠物收养所(Splay)
Description 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物.每个领养者都希望领养到自己满意的宠物,阿Q根据领养者的要求通过他自己发明的一个特 ...
- 宠物收养所(bzoj1208)
Description 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物.每个领养者都希望领养到自己满意的宠物,阿Q根据领养者的要求通过他自己发明的一个特 ...
随机推荐
- COGS 2280. [HZOI 2015]树白黑
★★ 输入文件:B_Tree.in 输出文件:B_Tree.out 简单对比时间限制:2 s 内存限制:512 MB [题目描述] 给定一棵有根树,树根为1,一开始这棵树所有节点均为白 ...
- Install your Application into RaspberryPi2 and automatically start up
如何安装和卸载应用程序到RaspberryPi2? 如何配置应用程序在RaspberryPi2开机后自动启动? How to install your app into RaspberryPi2? H ...
- 使用ABAP编程实现对微软Office Word文档的操作
SAP ABAP里提供了一个标准的类CL_DOCX_DOCUMENT,提供了本地以".docx"结尾的微软Office word文档的读和写操作. 本文介绍了ABAP类CL_DOC ...
- Mybatis-Generator逆向生成Po,Mapper,XMLMAPPER(idea)
前文有一篇手工生成的说明,地址: http://www.cnblogs.com/xiaolive/p/4874605.html, 现在这个补充一下在idea里面的自动版本的数据库逆向生成工具: 一.g ...
- codeforce Gym 100203I I WIN (网络流)
把'I'拆成容量为1一条边,一个入点一个出点,入点和相邻的'W'连一条容量为1的边,出点和相邻的'N'连一条容量为1,所有的'W'和源点连一条容量为1边,所有的'N'和汇点连一条容量为1的边,表示只能 ...
- python学习(day1)
一.在这次实训之前,虽然听说过很多次python这种语言,但是从来没有真正去学习过,仅仅知道它是一种目前十分流行且功能非常强大的语言,可以方便快捷的实现很多功能.今天的课程带我了解了python,并且 ...
- gendiff - 致力于创建无错的 diff 文件的工具
SYNOPSIS gendiff <directory> <diff-extension> DESCRIPTION gendiff 是一个简单的脚本,目标是根据单一的目录生成一 ...
- winhex与磁盘格式与 数据恢复
第一阶段: 熟悉WinHex的使用. n 熟悉磁盘工具的使用. n 利用WinHex查看物理磁盘和逻辑磁盘. n 了解WinHex中相关工具的用法. 以管理员身份运行winhex(以便之后修改) 上方 ...
- 【单片机实验】6LED静态串行显示
实验三 6LED静态串行显示一.实验目的1.掌握数字.字符转换成由数码管显示的八段码的软件译码方法及译码过程:2.静态显示的原理和相关程序的编写. 二.实验电路静态显示 电路如图3-2所示.显示器由6 ...
- CPP-基础:单目运算符重载
关于++运算符前置和后置重载的实现实例: #include <iostream> using namespace std; //创建时钟类 class Clock { public: Cl ...