题目:

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. C++和ASM文件的互相调用

    1. C++调用ASM中的函数,需要在ASM源文件中指定.model flat, c 就是指定以C的形式编译,然后在头文件中用EXTERN_C声明这个头文件就可以了 2. ASM中调用C++函数,需要 ...

  2. labview密码忘记怎么办,如何破解labview密码,vi密码md5码破解重置

    labview密码忘记了或者需要破解labview密码,可以找到vi文件的md5码,把里面的md5码拿到网站http://cmd5.la解密就可以了. 把vi文件的32位md5码放到网站cmd5.la ...

  3. A*算法研究

    许多工业与科学计算问题都可以转化为在图中寻路问题.启发式的寻路方法将问题表示为一个图,然后利用问题本身的信息,来加速解的搜索过程.一个典型的例子是有一些通路连接若干城市,找出从指定起点城市到指定终点城 ...

  4. Tensorflow_入门学习_2_一个神经网络栗子

    3.0 A Neural Network Example 载入数据: from tensorflow.examples.tutorials.mnist import input_data mnist ...

  5. 开源项目: circular-progress-button

    带进度条显示的按钮, 其效果如下所示: 其由三部分动画组成: 初始状态->圆环状态->完成状态. 0. 实现从初始到圆环的简单实现: 继承自button 类, 设置其背景 public c ...

  6. (三)VMware harbor使用http访问

    参考:https://www.cnblogs.com/biglittleant/p/7283738.html harbor使用http访问 如果使用http启动harbor需要在docker中配置-- ...

  7. sql server 处理分母为空

    SP 前面加下面设置,会忽略错误结果 直接返回null 不会导致SP 失败 SET ANSI_WARNINGS OFFSET ARITHABORT OFFSET ARITHIGNORE ON

  8. Linux命令基础操作--vim 归档 压缩 分区 格式化 挂载 Innode

    1 将用户信息数据库文件和组信息数据库文件纵向合并为一个文件/1.txt(覆盖) 使用 cat命令将查看的文件合并输出到/1.txt 这里的关键:定位到文件,如果后面加上/后被认为是目录 分为两步,先 ...

  9. mac 上node.js环境的安装与测试【转】

    http://blog.csdn.net/baihuaxiu123/article/details/51868142 一 摘要 如何大家之前做过web服务器的人都知道,nginx+lua与现在流行的n ...

  10. 歌乐第二弹:C++九九八十一

    第一弹传送门:极乐净土 二话不说,上代码(注意事项在第一弹里): #include <windows.h> //q前缀为低音,g为高音,s前缀为半音阶 const int q1 = 131 ...