刷题总结——宠物收养所(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+5; const int mod=1000000; int root,tot,size[N],son[N][2],father[N],key[N]; int n,cnt0=0,cnt1=0,a,b; long long ans=0; inline int R() {char c;int f=0;for(c=getchar();c<'0'||c>'9';c=getchar());for(;c<='9'&&c>='0';c=getchar())f=(f<<3)+(f<<1)+c-'0';return f; } long long Abs(long long x) {return x<0?-x:x; } inline void update(int now) {if(now){size[now]=1;if(son[now][0]) size[now]+=size[son[now][0]];if(son[now][1]) size[now]+=size[son[now][1]];} } inline void clear(int now) {size[now]=son[now][1]=son[now][0]=father[now]=key[now]=0; } inline int get(int now) {return son[father[now]][1]==now; } inline void rotate(int now) {int fa=father[now],ofa=father[fa],which=get(now);son[fa][which]=son[now][which^1],father[son[fa][which]]=fa; son[now][which^1]=fa,father[fa]=now,father[now]=ofa;if(ofa) son[ofa][son[ofa][1]==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][1];else now=son[now][0];} } inline void insert(int x) {int now=root,last=0;while(true){if(!now){now=++tot;size[now]=1;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][0];while(son[now][1]) now=son[now][1];return now; } inline int next() {int now=son[root][1];while(son[now][0]) now=son[now][0];return now; } inline long long prex() {int now=son[root][0];if(!now) return 1e+18;while(son[now][1]) now=son[now][1];return key[now]; } inline long long nextx() {int now=son[root][1];if(!now) return 1e+18;while(son[now][0]) now=son[now][0];return key[now]; } inline void Delete(int x) {if(x==1e+18) return;find(x);if(!son[root][0]&&!son[root][1]){clear(root);root=0;return;}else if(!son[root][0]){int oldroot=root;root=son[oldroot][1];father[root]=0;clear(oldroot);return;}else if(!son[root][1]){int oldroot=root;root=son[oldroot][0];father[root]=0;clear(oldroot);return;}else{int leftbig=pre(),oldroot=root;splay(leftbig);son[root][1]=son[oldroot][1];father[son[root][1]]=root;clear(oldroot);update(root);return;} } int main() {//freopen("a.in","r",stdin);n=R(); for(int i=1;i<=n;i++){a=R(),b=R();if(!cnt0&&!cnt1){if(a==0) cnt0++;else cnt1++;insert(b);}else if(!cnt0&&a==1){cnt1++;insert(b);}else if(!cnt1&&a==0){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 0; }
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+5; const int mod=1000000; const int inf=1e+9; set<int>st; int n,cnt0=0,cnt1=0,a,b; long long ans=0; inline int R() {char c;int f=0;for(c=getchar();c<'0'||c>'9';c=getchar());for(;c<='9'&&c>='0';c=getchar())f=(f<<3)+(f<<1)+c-'0';return f; } long long Abs(long long x) {return x<0?-x:x; } int main() {//freopen("a.in","r",stdin);n=R(); st.insert(-inf);st.insert(inf);for(int i=1;i<=n;i++){a=R(),b=R();if(!cnt0&&!cnt1){if(a==0) cnt0++;else cnt1++;st.insert(b);}else if(!cnt0&&a==1){cnt1++;st.insert(b);}else if(!cnt1&&a==0){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 0; }
转载于:https://www.cnblogs.com/AseanA/p/7489032.html
刷题总结——宠物收养所(bzoj1208)相关推荐
- BZOJ1208[HNOI2004]宠物收养场——treap
凡凡开了一间宠物收养场.收养场提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物. 每个领养者都希望领养到自己满意的宠物,凡凡根据领养者的要求通过他自己发明的一个特殊的公式,得出该领养者希望领 ...
- bzoj1208: [HNOI2004]宠物收养所
Time Limit: 10 Sec Memory Limit: 162 MB Submit: 6182 Solved: 2396 [Submit][Status][Discuss] Descri ...
- bzoj1208【HNOI2004】宠物收养所
1208: [HNOI2004]宠物收养所 Time Limit: 10 Sec Memory Limit: 162 MB Submit: 5923 Solved: 2296 [ Submit ...
- B1208 [HNOI2004]宠物收养所 平衡树||set (滑稽)
这个题是一道splay裸题,但是我不太会写,所以用set直接水过去!!!哈哈哈哈,美滋滋. set总结: set是一个集合,然后里面没用重复的元素.里面有一些函数: begin() ,返回se ...
- 洛谷P2286 [HNOI2004]宠物收养场
题目描述 凡凡开了一间宠物收养场.收养场提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物. 每个领养者都希望领养到自己满意的宠物,凡凡根据领养者的要求通过他自己发明的一个特殊的公式,得出该领 ...
- cogs62 [HNOI2004] 宠物收养所
cogs62 [HNOI2004] 宠物收养所 啦啦啦啦 不维护区间的平衡树题都是树状数组+二分练手题! 不会的参考我的普通平衡树的多种神奇解法之BIT+二分答案 // It is made by X ...
- BZOJ 1208: [HNOI2004]宠物收养所 (Treap)
BZOJ 1208: [HNOI2004]宠物收养所 题目概述: 有一家宠物收养所,提供两种服务:收养主人遗弃的宠物和让新主人领养宠物. 宠物收养所中总是会有两种情况发生:遗弃宠物过多和领养宠物人过多 ...
- java计算机毕业设计软考刷题系统源码+mysql数据库+系统+lw文档+部署
java计算机毕业设计软考刷题系统源码+mysql数据库+系统+lw文档+部署 java计算机毕业设计软考刷题系统源码+mysql数据库+系统+lw文档+部署 本源码技术栈: 项目架构:B/S架构 开 ...
- 「HNOI2004」 宠物收养所 - 平衡树Splay
题目描述 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物. 每个领养者都希望领养到自己满意的宠物,阿Q根据领养者的要求通过他自己发明的一个特殊的公式,得 ...
- OI 刷题记录——每周更新
每周日更新 2016.05.29 UVa中国麻将(Chinese Mahjong,Uva 11210) UVa新汉诺塔问题(A Different Task,Uva 10795) NOIP2012同余 ...
最新文章
- Visual Studio 2008 可扩展性开发(三):Add-In运行机制解析(下)
- OpenCV的HOG+SVM训练程序注意事项
- 拉格朗日插值法matlab上机,拉格朗日插值法使用MATLAB做的例题
- Mysql错误2003 -Can't connect toMySQL server on 'localhost'(10061)解决办法
- 关于原生AJAX和jQueryAJAX的编程
- 01_Android应用开发环境_01_android发展史及系统架构
- bfs (宽度搜素)
- eclipse设置Tomcat超级详细
- 首页大屏广告效果 jquery轮播图淡入淡出
- JavaScript:使用js脚本写入HTML代码
- 实测分析免费建站软件有哪些?哪个最好?
- PS怎么把人物扣的更干净_PS抠图技巧
- matplotlib柱状图给指定的柱换颜色_matplotlib绘图基本操作amp;美化教程
- matlab生成的fig文件名字修改
- 创建计划行确认数量为0的销售订单
- 关系图谱在风控体系的应用与实践
- 我的个人学习的小总结
- .Net C# Lambda表达式
- 连小白都能看懂的微信开发之测试账号申请
- 微软鼠标测试软件,微软Precision鼠标评测:Surface生产力工具最佳搭配