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,最后一个领养者没有宠物可以领养)
 
 
正解一:STL之set大法
解题报告:
      用STL中的set暴力维护题意的要求就可以了,没什么好说的。
  每次查找大于x的元素、小于等于x的元素,比较一下就加进贡献就可以了,注意取模。
     注:一开始加入一个无限大和无限小进入set,方便我们操作。
  set就是好,又短又快。
 
 
 //It is made by jump~
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <set>
#ifdef WIN32
#define OT "%I64d"
#else
#define OT "%lld"
#endif
using namespace std;
typedef long long LL;
const int inf = ( (<<) -);
const int MOD = ;
int n;
set<int>bst;
int kind;
int ans; inline int getint()
{
int w=,q=;
char c=getchar();
while((c<'' || c>'') && c!='-') c=getchar();
if (c=='-') q=, c=getchar();
while (c>='' && c<='') w=w*+c-'', c=getchar();
return q ? -w : w;
} int main()
{
n=getint();
bst.insert(inf); bst.insert(-inf);
int f,x;
int l,r;
for(int i=;i<=n;i++) {
f=getint(),x=getint();
if(bst.size()==){
kind=f; bst.insert(x);
}
else if(kind==f) bst.insert(x);
else{
l=*--bst.lower_bound(x);
r=*bst.lower_bound(x);
if(x-l<=r-x && l!=-inf){
ans+=x-l; ans%=MOD;
bst.erase(l);
}
else{
ans+=r-x; ans%=MOD;
bst.erase(r);
}
}
}
printf("%d",ans);
return ;
}

正解二:splay

解题报告:

显然手打splay才是勤奋的oier该做的事。

  

  %%%hzwer

 //It is made by jump~
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <set>
#ifdef WIN32
#define OT "%I64d"
#else
#define OT "%lld"
#endif
using namespace std;
typedef long long LL;
const int MOD = ;
const int MAXN = ;
int n;
int kind;
int rt;
int t1,t2;
int tr[MAXN][],num[MAXN],fa[MAXN];
int size;
int ans; inline int getint()
{
int w=,q=;
char c=getchar();
while((c<'' || c>'') && c!='-') c=getchar();
if (c=='-') q=, c=getchar();
while (c>='' && c<='') w=w*+c-'', c=getchar();
return q ? -w : w;
} inline void rotate(int x,int &k){
int y=fa[x],z=fa[y],l,r;
if(tr[y][]==x) l=; else l=;
r=l^;
if(y==k) k=x;
else {
if(tr[z][]==y) tr[z][]=x;
else tr[z][]=x;
}
fa[x]=z; fa[y]=x; fa[tr[x][r]]=y;
tr[y][l]=tr[x][r]; tr[x][r]=y;
} inline void splay(int x,int &k){
int y,z;
while(x!=k) {
y=fa[x],z=fa[y];
if(y!=k) {
if((tr[y][]==x)^(tr[z][]==y)) rotate(x,k);
else rotate(y,k);
}
rotate(x,k);
}
} inline void insert(int &k,int x,int last){
if(k==) {
size++; k=size; num[k]=x; fa[k]=last; splay(k,rt); return ;
}
if(x<num[k]) insert(tr[k][],x,k); else insert(tr[k][],x,k);
} inline void ask_before(int k,int x){
if(k==) return ;
if(num[k]<=x) { t1=k; ask_before(tr[k][],x); }
else ask_before(tr[k][],x);
} inline void ask_after(int k,int x){
if(k==) return ;
if(num[k]>=x){ t2=k; ask_after(tr[k][],x); }
else ask_after(tr[k][],x);
} inline void del(int x){
splay(x,rt);
if(tr[x][]*tr[x][]==) rt=tr[x][]+tr[x][];
else{
int k=tr[x][];
while(tr[k][]) k=tr[k][];
tr[k][]=tr[x][]; fa[tr[x][]]=k;
rt=tr[x][];
}
fa[rt]=;
} int main()
{
n=getint();
int f,x;
for(int i=;i<=n;i++) {
f=getint(); x=getint();
if(!rt) { kind=f; insert(rt,x,); }
else if(kind==f) { insert(rt,x,); }
else{
t1=t2=-;
ask_before(rt,x); ask_after(rt,x);
if(t1==-) { ans+=num[t2]-x; if(ans>=MOD) ans%=MOD; del(t2); }
else if(t2==-) { ans+=x-num[t1]; if(ans>=MOD) ans%=MOD; del(t1); }
else{
if(x-num[t1]>num[t2]-x){
ans+=num[t2]-x; if(ans>=MOD) ans%=MOD; del(t2);
}
else{
ans+=x-num[t1]; if(ans>=MOD) ans%=MOD; del(t1);
}
}
}
}
printf("%d",ans);
return ;
}

BZOJ1208 宠物收养所的更多相关文章

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

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

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

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

  3. Bzoj1208 [HNOI2004]宠物收养所

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

  4. 宠物收养所(bzoj1208)

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

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

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

  6. 【BZOJ-1208】宠物收养所 Splay

    1208: [HNOI2004]宠物收养所 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 6638  Solved: 2601[Submit][Sta ...

  7. bzoj1208 [HNOI2004]宠物收养所(STL,Treap)

    1208: [HNOI2004]宠物收养所 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 5956  Solved: 2317[Submit][Sta ...

  8. BZOJ1208[HNOI2004]宠物收养场——treap

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

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

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

随机推荐

  1. 第12章 纤程(Fiber)

    12.1 纤程对象的介绍 (1)纤程与线程的比较 比较 线程(Thread) 纤程(Fiber) 实现方式 是个内核对象 在用户模式中实现的一种轻量级的线程,是比线程更小的调度单位. 调度方式 由Mi ...

  2. Auto Clear Unity Console Log

    功能 可以在Editor模式下执行,当然也可以Runtime模式下执行,自动清除 Console的log信息 功能需求 当在制作Editor的一些功能时,常常需要手动的点击Console窗口的Clea ...

  3. 定制你的Unity编辑器

    Unity的编辑器可以通过写脚本进行界面定制,包括添加功能菜单,今天写游戏Demo用到了记录一下. 为Unity添加子菜单 示例程序 [AddComponentMenu("Defend Ho ...

  4. Windows系统安装Oracle 11g客户端

    一.下载 http://www.oracle.com/technetwork/database/enterprise-edition/downloads/index.html以下网址来源此官方下载页网 ...

  5. 【转】【C#】SendMessage

    SendMessage是一个在user32.dll中声明的API函数,在C#中导入如下: using System.Runtime.InteropServices; [DllImport(" ...

  6. C语言 百炼成钢13

    //题目37:将一个数组逆序输出.用第一个与最后一个交换. #include<stdio.h> #include<stdlib.h> #include<math.h> ...

  7. [转]SRTM、ASTER GDEM等全球数字高程数据(DEM)下载方式简介

    之前写过一篇短文对比过几种数字高程数据的区别:5种全球高程数据对比,这篇文章简要介绍下如何下载这些数据.       1.DLR的数字高程数据.该数据也是SRTM(shuttle radar topo ...

  8. ARM基础知识

    ARM处理器模式: 模式可以理解为 工作环境. 异常模式:SVC管理模式. FIQ 快速中断模式 . IRQ中断模式.Abort中止.Undef 未定义模式: 正常模式:System系统模式. Use ...

  9. Linux查看系统资源命令

    转载于:http://lxbins.blog.51cto.com/1089997/283663 top:======================================主要参数d:指定更新 ...

  10. iOS后台定位实现

    iOS后台定位实现 (2013-01-24 16:43:12)     工作中碰到一个定位的应用场景:app需要在后台运行,实时上传用户地理位置.   苹果对iOS的规范性在提升了app的品质的同时也 ...