一道比较简单的直接Treap运用题目,思维难度和代码难度都不是很高。

题意有点长,我们仔细剖析一下题意发现以下几个关键:

  1. 任何时候收养站里只可能有人和宠物中的其中一种,或者都没有
  2. 如果只有宠物并有人来时,那个人会立刻挑选一只宠物并离开(只有人的时候同理)。
  3. 如果没有宠物并有人来时,那个人会一直等着直到有宠物到来并且那个宠物被它领养了(没有人的时候同理)。

然后我们思考一下那个选宠物(或者是宠物选人)的过程,其实就是在一个集合找一个数和它最接近的过程。

然后这道题已经很良心地提示你了:

存在两个宠物a-b,a+b......

其实仔细想一下就是一个找前驱找后缀的问题,我们找出前驱和后继就可以直接统计并删除了。

具体实现的时候开两个Treap,一棵存宠物,一棵存人(这里由于宠物和人只会同时存在一种,所以其实开一棵也可以),然后按题意搞一下就可以了。

然后我写了两个namespace,所以主程序看上去有点乱。

最后注意开long long

CODE

#include<cstdio>
#include<cctype>
using namespace std;
typedef long long LL;
const LL N=80005,INF=1e18,mod=1000000;
LL n,opt,x,ans;
inline char tc(void)
{
static char fl[100000],*A=fl,*B=fl;
return A==B&&(B=(A=fl)+fread(fl,1,100000,stdin),A==B)?EOF:*A++;
}
inline void read(LL &x)
{
x=0; char ch; while (!isdigit(ch=tc()));
while (x=(x<<3)+(x<<1)+ch-'0',isdigit(ch=tc()));
}
inline LL rand(void)
{
static LL seed=233;
return seed=(LL)seed*482711LL%2147483647;
}
inline LL min(LL a,LL b)
{
return a<b?a:b;
}
namespace pet
{
struct Treap
{
LL ch[2],val,dat;
}node[N];
LL tot,rt,size;
inline LL build(LL v)
{
node[++tot].val=v; node[tot].dat=rand(); return tot;
}
inline void init(void)
{
rt=build(-INF); node[rt].ch[1]=build(INF);
}
inline void rotate(LL &rt,LL d)
{
LL temp=node[rt].ch[d^1]; node[rt].ch[d^1]=node[temp].ch[d];
node[temp].ch[d]=rt; rt=temp;
}
inline void insert(LL &rt,LL v)
{
if (!rt) { rt=build(v); return; }
LL d=v<node[rt].val?0:1; insert(node[rt].ch[d],v);
if (node[rt].dat<node[node[rt].ch[d]].dat) rotate(rt,d^1);
}
inline void remove(LL &rt,LL v)
{
if (node[rt].val==v)
{
if (node[rt].ch[0]||node[rt].ch[1])
{
if (!node[rt].ch[1]||node[node[rt].ch[1]].dat<node[node[rt].ch[0]].dat) rotate(rt,1),remove(node[rt].ch[1],v);
else rotate(rt,0),remove(node[rt].ch[0],v);
} else rt=0; return;
}
if (v<node[rt].val) remove(node[rt].ch[0],v); else remove(node[rt].ch[1],v);
}
inline LL get_pre(LL rt,LL v)
{
LL pre;
while (rt)
{
if (node[rt].val<=v) pre=node[rt].val,rt=node[rt].ch[1];
else rt=node[rt].ch[0];
}
return pre;
}
inline LL get_next(LL rt,LL v)
{
LL next;
while (rt)
{
if (node[rt].val>=v) next=node[rt].val,rt=node[rt].ch[0];
else rt=node[rt].ch[1];
}
return next;
}
};
namespace hum
{
struct Treap
{
LL ch[2],val,dat;
}node[N];
LL tot,rt,size;
inline LL build(LL v)
{
node[++tot].val=v; node[tot].dat=rand(); return tot;
}
inline void init(void)
{
rt=build(-INF); node[rt].ch[1]=build(INF);
}
inline void rotate(LL &rt,LL d)
{
LL temp=node[rt].ch[d^1]; node[rt].ch[d^1]=node[temp].ch[d];
node[temp].ch[d]=rt; rt=temp;
}
inline void insert(LL &rt,LL v)
{
if (!rt) { rt=build(v); return; }
LL d=v<node[rt].val?0:1; insert(node[rt].ch[d],v);
if (node[rt].dat<node[node[rt].ch[d]].dat) rotate(rt,d^1);
}
inline void remove(LL &rt,LL v)
{
if (node[rt].val==v)
{
if (node[rt].ch[0]||node[rt].ch[1])
{
if (!node[rt].ch[1]||node[node[rt].ch[1]].dat<node[node[rt].ch[0]].dat) rotate(rt,1),remove(node[rt].ch[1],v);
else rotate(rt,0),remove(node[rt].ch[0],v);
} else rt=0; return;
}
if (v<node[rt].val) remove(node[rt].ch[0],v); else remove(node[rt].ch[1],v);
}
inline LL get_pre(LL rt,LL v)
{
LL pre;
while (rt)
{
if (node[rt].val<=v) pre=node[rt].val,rt=node[rt].ch[1];
else rt=node[rt].ch[0];
}
return pre;
}
inline LL get_next(LL rt,LL v)
{
LL next;
while (rt)
{
if (node[rt].val>=v) next=node[rt].val,rt=node[rt].ch[0];
else rt=node[rt].ch[1];
}
return next;
}
};
int main()
{
//freopen("CODE.in","r",stdin); freopen("CODE.out","w",stdout);
pet::init(); hum::init(); read(n);
while (n--)
{
read(opt); read(x);
if (opt)
{
if (pet::size)
{
LL pre=pet::get_pre(pet::rt,x),next=pet::get_next(pet::rt,x); --pet::size;
if (pre==-INF||x-pre>next-x) ans=(ans+next-x)%mod,pet::remove(pet::rt,next);
else ans=(ans+x-pre)%mod,pet::remove(pet::rt,pre);
} else ++hum::size,hum::insert(hum::rt,x);
} else
{
if (hum::size)
{
LL pre=hum::get_pre(hum::rt,x),next=hum::get_next(hum::rt,x); --hum::size;
if (pre==-INF||x-pre>next-x) ans=(ans+next-x)%mod,hum::remove(hum::rt,next);
else ans=(ans+x-pre)%mod,hum::remove(hum::rt,pre);
} else ++pet::size,pet::insert(pet::rt,x);
}
}
return printf("%lld",ans),0;
}

Luogu P2286 [HNOI2004]宠物收养场的更多相关文章

  1. P2286 [HNOI2004]宠物收养场

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

  2. 洛谷P2286 [HNOI2004]宠物收养场【Treap】题解+AC代码

    题目传送门啦~啦~啦~ 题目描述 凡凡开了一间宠物收养场.收养场提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物. 每个领养者都希望领养到自己满意的宠物,凡凡根据领养者的要求通过他自己发明的 ...

  3. 洛谷P2286 [HNOI2004]宠物收养场

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

  4. 洛谷 P2286 [HNOI2004]宠物收养场

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

  5. 洛谷P2286 [HNOI2004]宠物收养所 [STL,平衡树]

    题目传送门 宠物收养所 题目描述 凡凡开了一间宠物收养场.收养场提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物. 每个领养者都希望领养到自己满意的宠物,凡凡根据领养者的要求通过他自己发明的 ...

  6. [HNOI2004]宠物收养场 Treap前驱后继

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

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

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

  8. [HNOI2004]宠物收养场 BZOJ1208 splay tree

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

  9. LG_2286_[HNOI2004]宠物收养场

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

随机推荐

  1. Android开发专业名词及工具概述

    前言: 系统的学习下Android开发中涉及到的一些专业名词 和Android开发工具 名词: 一.SDK(Software Development Kit) 软件开发工具包:一般都是一些软件工程师为 ...

  2. off by null 实战

    前言 off by null 是一个比较有意思的技术 下面通过 hctf2018 的 heapstrom_zero 实战一波. 相关文件(exp, 题目)位于 https://gitee.com/ha ...

  3. ionic打包报错Execution failed for task ':processDebugResources'

    ionic 打包的时候报了这样一个错误:Execution failed for task ':processDebugResources' 分析: compile "com.android ...

  4. SQL Server ltrim(rtrim()) 去不掉空格

    原因:中间存在回车符或者换行符,所以要先将此符号替换掉: LTRIM(RTRIM(REPLACE(REPLACE( A,char(13),''),char(10),'') )) LTRIM(A) -- ...

  5. php程序开发之实现网页跳转

    php程序开发之实现网页跳转的三种方式 2017年04月16日 20:44:14 阅读数:3352 PHP目前是用来开发WEB项目的首选语言.Web项目中,从一个网页跳转到另一个网页是最常用的技术之一 ...

  6. January 19th, 2018 Week 3rd Friday

    As iron sharpens iron, so a friend sharpens a friend. 铁磨铁,可以磨砺出刀刃,朋友相交,亦应如此. When making friends wit ...

  7. The resource configuration is not modifiable in this context.

    项目中使用了Jersey RESTful 框架, 更新代码后服务能正常起来, 在页面登录时验证码不显示 后台报错 java.lang.IllegalStateException: The resour ...

  8. Alpha冲刺! Day7 - 砍柴

    Alpha冲刺! Day7 - 砍柴 今日已完成 晨瑶:列了各模块目前的进度情况:确定了纯多媒体流星预览页的显示方式:给工具包函数列表新增了与服务器端的交互:玩华为软件云发现刚好可以试试它的测试,于是 ...

  9. 使用golang的slice来模拟栈

    slice(切片):底层数据结构是数组 stack(栈):一种先进后出的数据结构 普通版的模拟写入和读取的栈 package main import "fmt" //栈的特点是先进 ...

  10. 使用requests模块post payload请求

    import json import requests import datetime postUrl = 'https://sellercentral.amazon.com/fba/profitab ...