1285 宠物收养所

http://codevs.cn/problem/1285/

 时间限制: 1 s
 空间限制: 128000 KB
 
 
题目描述 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 Description

第一行为一个正整数n,n<=80000,表示一年当中来到收养所的宠物和领养者的总数。接下来的n行,按到来时间的先后顺序描述了一年当中来到收养所的宠物和领养者的情况。每行有两个正整数a, b,其中a=0表示宠物,a=1表示领养者,b表示宠物的特点值或是领养者希望领养宠物的特点值。(同一时间呆在收养所中的,要么全是宠物,要么全是领养者,这些宠物和领养者的个数不会超过10000个)

输出描述 Output Description

仅有一个正整数,表示一年当中所有收养了宠物的领养者的不满意程度的总和mod 1000000以后的结果。

样例输入 Sample Input

5

0 2

0 4

1 3

1 2

1 5

样例输出 Sample Output

3

数据范围及提示 Data Size & Hint

(abs(3-2) + abs(2-4)=3,最后一个领养者没有宠物可以领养)

用这题练Treap

插入、删除、前驱、后继

#include<cstdio>
#include<algorithm>
#define mod 1000000
using namespace std;
struct node
{
int key,cnt,fix;
node *lc,*rc;
}*root,*null;
int sum[],ans;
struct TREAP
{
TREAP()
{
null= new node;
null->lc=NULL;
null->rc=NULL;
root=null;
root->fix=0x7fffffff;
}
void rotate_right(node *&now)
{
node *tmp = now->lc;
now->lc = tmp->rc;
tmp->rc = now;
now=tmp;
}
void rotate_left(node *&now)
{
node *tmp = now->rc;
now->rc = tmp->lc;
tmp->lc = now;
now=tmp;
}
void insert(node *&now,int x)
{
if(now==null)
{
now= new node;
now->cnt=;
now->key=x;
now->lc=null;
now->rc=null;
now->fix=rand();
}
else if(now->key > x)
{
insert(now->lc,x);
if( now->lc->fix < now->fix ) rotate_right(now);
}
else
{
insert(now->rc,x);
if(now->rc->fix < now->fix) rotate_left(now);
}
}
int find_pre(int k)
{
int tmp=-;
node *now=root;
while(now!=null)
{
if(now-> key <=k) tmp=now->key,now=now->rc;
else now=now->lc;
}
return tmp;
}
int find_suf(int k)
{
int tmp=-;
node *now = root;
while(now!=null)
{
if(now->key >=k) tmp=now->key,now=now->lc;
else now=now->rc;
}
return tmp;
}
void del(node *&now,int x)
{
if(now->key==x)
{
if(now->lc != null && now->rc != null)
{
if(now->lc->fix < now->rc->fix)
{
rotate_right(now);
del(now->rc,x);
}
else
{
rotate_left(now);
del(now->lc,x);
}
}
else
{
node *tmp=now;
if(now->lc == null) now = now->rc;
else now = now->lc;
delete(tmp);
}
}
else if(now->key < x) del(now->rc,x);
else del(now->lc,x);
}
}Treap;
int main()
{
int n,a,b,pre,suf;
scanf("%d",&n);
while(n--)
{
scanf("%d%d",&a,&b);
if(!sum[!a]) Treap.insert(root,b),sum[a]++;
else
{
sum[!a]--;
pre=Treap.find_pre(b);
suf=Treap.find_suf(b);
if(pre==-)
{
ans=(ans+abs(suf-b))%mod;
Treap.del(root,suf);
}
else if(suf==-)
{
ans=(ans+abs(pre-b))%mod;
Treap.del(root,pre);
}
else
{
if(abs(pre-b)<=abs(suf-b))
{
ans=(ans+abs(pre-b))%mod;
Treap.del(root,pre);
}
else
{
ans=(ans+abs(suf-b))%mod;
Treap.del(root,suf);
}
}
}
}
printf("%d\n",ans);
}

最新文章

  1. Java面试题系列 提高Java I/O 性能
  2. (Python)元祖、字典
  3. 分享一段视频关于SQL2014 Hekaton数据库的
  4. HDU-4455 Substrings(DP)
  5. Java7编程高手进阶读书笔记&mdash;集合框架
  6. nutch getOutLinks 外链的处理
  7. 通过demo搞懂encode_utf8和decode_utf8
  8. WPF换肤之四:界面设计和代码设计分离
  9. MySQL create table 语法
  10. 【Zookeeper】源码分析之请求处理链(三)
  11. caffe数据读取的双阻塞队列说明
  12. 解决 Win10 UWP 无法使用 ss 连接
  13. Java Spring Boot VS .NetCore (八) Java 注解 vs .NetCore Attribute
  14. js canvas图片压缩
  15. prototype:构造函数的真相、原型链
  16. 细说Request与Request.Params
  17. thinkcmf 角色授权支持分类
  18. 8 -- 深入使用Spring -- 3...1 Resource实现类ClassPathResource
  19. OAuth 2.0 学习
  20. android判断服务是否是运行状态

热门文章

  1. Scrum立会报告+燃尽图(Beta阶段第二周第四次)
  2. Android:有关菜单的学习(供自己参考)
  3. Codeforces Round #335 (Div. 2) D. Lazy Student 贪心+构造
  4. HDU 5159 Card
  5. HDU 5656 CA Loves GCD 01背包+gcd
  6. mysql的程序组成
  7. UVA11625_Lines of Containers
  8. luogu 1360 阵容均衡(前缀和+差分+hash)
  9. noip模拟题《戏》game
  10. MySQL复制 -- Binlog (1)