题目:

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

5
0 2
0 4
1 3
1 2
1 5

Sample Output

3
(abs(3-2) + abs(2-4)=3,最后一个领养者没有宠物可以领养)
数据有一定梯度
对于80%的数据,收养者按照到来的顺序收养宠物

题解:

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)的更多相关文章

  1. [HNOI2004]宠物收养场 BZOJ1208 splay tree

    题目描述 凡凡开了一间宠物收养场.收养场提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物. 每个领养者都希望领养到自己满意的宠物,凡凡根据领养者的要求通过他自己发明的一个特殊的公式,得出该领 ...

  2. Bzoj1208 [HNOI2004]宠物收养所

    Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 7457  Solved: 2960 Description 最近,阿Q开了一间宠物收养所.收养所提供两 ...

  3. 【BZOJ1208】[HNOI2004]宠物收养所 Splay

    还是模板题,两颗splay,找点删即可. #include <iostream> #include <cstdio> #include <cstdlib> #def ...

  4. 2018.07.06 BZOJ1208: HNOI2004宠物收养所(非旋treap)

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

  5. BZOJ1208 HNOI2004 宠物收养所 【非旋转Treap】

    BZOJ1208 HNOI2004 宠物收养所 Description 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物.每个领养者都希望领养到自己满意的 ...

  6. BZOJ1208:[HNOI2004]宠物收养所——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=1208 Description 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠物 ...

  7. HNOI2004宠物收养所(splay维护二叉搜索树模板题)

    描述 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物.每个领养者都希望领养到自己满意的宠物,阿Q根据领养者的要求通过他自己发明的一个特殊的公式,得出该领 ...

  8. [BZOJ1208]宠物收养所(Splay)

    Description 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物.每个领养者都希望领养到自己满意的宠物,阿Q根据领养者的要求通过他自己发明的一个特 ...

  9. 宠物收养所(bzoj1208)

    Description 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物.每个领养者都希望领养到自己满意的宠物,阿Q根据领养者的要求通过他自己发明的一个特 ...

随机推荐

  1. Lucene全文检索技术学习

    ---------------------------------------------------------------------------------------------------- ...

  2. 限制UITextField输入长度

    如果要限制UITextField输入长度最长不超过kMaxLength,那么需要实现做以下操作: 1.实现UITextFieldDelegate协议: 2.实现textField:shouldChan ...

  3. 从汇编看c++中的多态

    http://www.cnblogs.com/chaoguo1234/archive/2013/05/19/3079078.html 在c++中,当一个类含有虚函数的时候,类就具有了多态性.构造函数的 ...

  4. atom 自定义快捷键

    'atom-text-editor': 'shift-alt-i':'core:move-up' 'shift-alt-space':'core:move-down' 'shift-alt-l':'c ...

  5. react中的setState的使用和深入理解

    前端框架从MVC过渡到MVVM.从DOM操作到数据驱动,一直在不断的进步着,提升着, angular中用的是watcher对象,vue是观察者模式,react就是state了,他们各有各的特点,没有好 ...

  6. 数据库-SQL语法:GROUP BY与HAVING

    注意:select 后的字段,必须要么包含在group by中,要么包含在having 后的聚合函数里. 1. GROUP BY 是分组查询, 一般 GROUP BY 是和聚合函数配合使用. grou ...

  7. Day5 集合的深浅copy

    集合是无序的,不重复的数据集合,它里面的元素是可哈希的(不可变类型),但是集合本身是不可哈希(所以集合做不了字典的键)的.以下是集合最重要的两点: 去重,把一个列表变成集合,就自动去重了. 关系测试, ...

  8. php微信开发自动回复一直提示“该公众号提供的服务出现故障,请稍后再试”

    坑:服务器可以接受到发到公众号的信息,但是公众号不能回复,直接echo " ";exit();也会提示“该公众号提供的服务出现故障,请稍后再试”: 可能原因:用的php,是把数组转 ...

  9. Core Animation演示

    相关代码展示: - (IBAction)toggleRoundCorners:(id)sender { [CATransaction setDisableActions:![_enableAnimat ...

  10. 牛客练习赛40 C-小A与欧拉路

    求图中最短的欧拉路.题解:因为是一棵树,因此当从某一个节点遍历其子树的时候,如果还没有遍历完整个树,一定还需要再回到这个节点再去遍历其它子树,因此除了从起点到终点之间的路,其它路都被走了两次,而我们要 ...