2018.07.06 BZOJ1208: HNOI2004宠物收养所(非旋treap)
1208: [HNOI2004]宠物收养所
Time Limit: 10 Sec Memory Limit: 162 MB
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,最后一个领养者没有宠物可以领养)
一眼题,用两颗treap" role="presentation" style="position: relative;">treaptreap分别维护pet" role="presentation" style="position: relative;">petpet和people" role="presentation" style="position: relative;">peoplepeople的情况,维护前驱后继即可。不过用set" role="presentation" style="position: relative;">setset也行。
代码如下:
#include<bits/stdc++.h>
#define mod 1000000
#define N 80005
using namespace std;
typedef pair<int,int> res;
int n;
inline int read(){
int ans=0;
char ch=getchar();
while(!isdigit(ch))ch=getchar();
while(isdigit(ch))ans=(ans<<3)+(ans<<1)+ch-'0',ch=getchar();
return ans;
}
struct Treap{
int rt,son[N][2],siz[N],val[N],rd[N],cnt;
inline int build(int v){rd[++cnt]=rand(),val[cnt]=v,siz[cnt]=1,son[cnt][0]=son[cnt][1]=0;return cnt;}
inline void pushup(int p){siz[p]=siz[son[p][0]]+siz[son[p][1]]+1;}
inline int merge(int a,int b){
if(!a||!b)return a+b;
if(rd[a]<rd[b]){
son[a][1]=merge(son[a][1],b);
pushup(a);
return a;
}
son[b][0]=merge(a,son[b][0]);
pushup(b);
return b;
}
inline res split(int p,int k){
if(!p)return res(0,0);
res ans,tmp;
if(siz[son[p][0]]>=k){
tmp=split(son[p][0],k);
son[p][0]=tmp.second,pushup(p);
ans.first=tmp.first;
ans.second=p;
return ans;
}
tmp=split(son[p][1],k-siz[son[p][0]]-1);
son[p][1]=tmp.first,pushup(p);
ans.first=p;
ans.second=tmp.second;
return ans;
}
inline int rank(int p,int v){
if(!p)return 0;
if(val[p]>v)return rank(son[p][0],v);
return rank(son[p][1],v)+siz[son[p][0]]+1;
}
inline void ins(int v){
int k=rank(rt,v);
res x=split(rt,k);
int p=build(v);
rt=merge(merge(x.first,p),x.second);
}
inline void del(int v){
int k=rank(rt,v);
res x=split(rt,k);
res y=split(x.first,k-1);
rt=merge(y.first,x.second);
}
inline int pre(int p,int v){
if(!p)return -0x3f3f3f3f;
if(val[p]<v)return max(val[p],pre(son[p][1],v));
return pre(son[p][0],v);
}
inline int suf(int p,int v){
if(!p)return 0x3f3f3f3f;
if(val[p]>v)return min(val[p],suf(son[p][0],v));
return suf(son[p][1],v);
}
}people,pet;
int main(){
srand(20030112);
n=read();
int ans=0;
while(n--){
int op=read(),v=read();
if(op==0){
if(!people.siz[people.rt])pet.ins(v);
else{
int a=people.pre(people.rt,v),b=people.suf(people.rt,v);
if(v-a<=b-v)ans+=(v-a),people.del(a);
else ans+=(b-v),people.del(b);
}
}
else{
if(!pet.siz[pet.rt])people.ins(v);
else{
int a=pet.pre(pet.rt,v),b=pet.suf(pet.rt,v);
if(v-a<=b-v)ans+=(v-a),pet.del(a);
else ans+=(b-v),pet.del(b);
}
}
ans%=mod;
}
printf("%d",ans);
return 0;
}
2018.07.06 BZOJ1208: HNOI2004宠物收养所(非旋treap)的更多相关文章
- 2018.08.06 bzoj1500: [NOI2005]维修数列(非旋treap)
传送门 平衡树好题. 我仍然是用的fhqtreap,感觉速度还行. 维护也比线段树splay什么的写起来简单. %%%非旋treap大法好. 代码: #include<bits/stdc++.h ...
- 2018.07.24 loj#107. 维护全序集(非旋treap)
传送门 就是普通平衡树,可以拿来练非旋treap" role="presentation" style="position: relative;"&g ...
- BZOJ1208 HNOI2004 宠物收养所 【非旋转Treap】
BZOJ1208 HNOI2004 宠物收养所 Description 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物.每个领养者都希望领养到自己满意的 ...
- bzoj1208 [HNOI2004]宠物收养所(STL,Treap)
1208: [HNOI2004]宠物收养所 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 5956 Solved: 2317[Submit][Sta ...
- Bzoj1208 [HNOI2004]宠物收养所
Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 7457 Solved: 2960 Description 最近,阿Q开了一间宠物收养所.收养所提供两 ...
- BZOJ1208[HNOI2004]宠物收养场——treap
凡凡开了一间宠物收养场.收养场提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物. 每个领养者都希望领养到自己满意的宠物,凡凡根据领养者的要求通过他自己发明的一个特殊的公式,得出该领养者希望领 ...
- [bzoj1208][HNOI2004]宠物收养所——splay
题目大意 Description 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物.每个领养者都希望领养到自己满意的宠物,阿Q根据领养者的要求通过他自己发 ...
- [bzoj1208][HNOI2004][宠物收养所] (平衡树)
Description 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物.每个领养者都希望领养到自己满意的宠物,阿Q根据领养者的要求通过他自己发明的一个特 ...
- BZOJ1208 [HNOI2004]宠物收养所 splay
原文链接http://www.cnblogs.com/zhouzhendong/p/8085803.html 题目传送门 - BZOJ1208 题意概括 有两种数,依次加入. 规则为下: 如果当前剩余 ...
随机推荐
- LaiFeng-code
https://github.com/LaiFeng-Android/SopCastComponent https://github.com/LaiFengiOS/
- Python内存管理机制及优化简析(转载)
from:http://kkpattern.github.io/2015/06/20/python-memory-optimization-zh.html 准备工作 为了方便解释Python的内存管理 ...
- c语言使用librdkafka库实现kafka的生产和消费实例(转)
关于librdkafka库的介绍,可以参考kafka的c/c++高性能客户端librdkafka简介,本文使用librdkafka库来进行kafka的简单的生产.消费 一.producer librd ...
- mongodb基础学习3-查询的复杂用法
昨天看了一下查询,今天来说下查询的复杂用法,可以类比mysql的查询 $ne:不等于 $gt, $gte, $lt, $lte:大于,大于等于,小于,小于等于 $in $and $nor:相当于上面的 ...
- ResponseUtil数据传递至前端
package com.java1234.util; import java.io.OutputStream; import java.io.PrintWriter; import javax.ser ...
- c# 7 vs2017 tuple
var unnamed = (42, "The meaning of life"); var anonymous = (16, "a perfect square& ...
- mysql 常用option
[mysql 常用option] --host=host_name, -h host_name Connect to the MySQL server on the given host. --por ...
- HTML5 historyState pushState、replaceState
DOM中的window对象通过window.history方法提供了对浏览器历史记录的读取,让你可以在用户的访问记录中前进和后退. 从HTML5开始,我们可以开始操作这个历史记录堆栈. 1.Histo ...
- [leetcode]283. Move Zeroes移零
Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...
- Redis阅读目录
一.Redis简介 点击链接查看:https://www.cnblogs.com/hwlong/p/9325986.html 二.Redis安装及基本配置 点击链接查看:https://www.cnb ...