题目描述

凡凡开了一间宠物收养场。收养场提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物。

每个领养者都希望领养到自己满意的宠物,凡凡根据领养者的要求通过他自己发明的一个特殊的公式,得出该领养者希望领养的宠物的特点值a(a是一个正整数,a<2^31),而他也给每个处在收养场的宠物一个特点值。这样他就能够很方便的处理整个领养宠物的过程了,宠物收养场总是会有两种情况发生:被遗弃的宠物过多或者是想要收养宠物的人太多,而宠物太少。

被遗弃的宠物过多时,假若到来一个领养者,这个领养者希望领养的宠物的特点值为a,那么它将会领养一只目前未被领养的宠物中特点值最接近a的一只宠物。(任何两只宠物的特点值都不可能是相同的,任何两个领养者的希望领养宠物的特点值也不可能是一样的)如果有两只满足要求的宠物,即存在两只宠物他们的特点值分别为a-b和a+b,那么领养者将会领养特点值为a-b的那只宠物。

收养宠物的人过多,假若到来一只被收养的宠物,那么哪个领养者能够领养它呢?能够领养它的领养者,是那个希望被领养宠物的特点值最接近该宠物特点值的领养者,如果该宠物的特点值为a,存在两个领养者他们希望领养宠物的特点值分别为a-b和a+b,那么特点值为a-b的那个领养者将成功领养该宠物。

一个领养者领养了一个特点值为a的宠物,而它本身希望领养的宠物的特点值为b,那么这个领养者的不满意程度为abs(a-b)。

你得到了一年当中,领养者和被收养宠物到来收养所的情况,请你计算所有收养了宠物的领养者的不满意程度的总和。这一年初始时,收养所里面既没有宠物,也没有领养者。

输入输出格式

输入格式:

第一行为一个正整数n,n<=80000,表示一年当中来到收养场的宠物和领养者的总数。接下来的n行,按到来时间的先后顺序描述了一年当中来到收养场的宠物和领养者的情况。每行有两个正整数a,

b,其中a=0表示宠物,a=1表示领养者,b表示宠物的特点值或是领养者希望领养宠物的特点值。(同一时间呆在收养所中的,要么全是宠物,要么全是领养者,这些宠物和领养者的个数不会超过10000个)

输出格式:

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

输入输出样例

输入样例#1:
复制

5
0 2
0 4
1 3
1 2
1 5
输出样例#1: 复制

3
注:abs(3-2) + abs(2-4)=3,
最后一个领养者没有宠物可以领养。 用一颗splay tree维护顾客和宠物;
当然也可以用两棵树分别维护;
重点就是寻找前驱和后继;
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize(2)
using namespace std;
#define maxn 200005
#define inf 0x3f3f3f3f
//#define INF 1e18
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-3
typedef pair<int, int> pii;
#define pi acos(-1.0)
const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii;
inline ll rd() {
ll x = 0;
char c = getchar();
bool f = false;
while (!isdigit(c)) {
if (c == '-') f = true;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f ? -x : x;
} ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
ll sqr(ll x) { return x * x; } /*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (!b) {
x = 1; y = 0; return a;
}
ans = exgcd(b, a%b, x, y);
ll t = x; x = y; y = t - a / b * y;
return ans;
}
*/ int n;
struct node {
int ch[2];
int val;
int ff;
}t[maxn<<1]; int rt;
int tot; void rotate(int x) {
int y = t[x].ff;
int z = t[y].ff;
int k = (x == t[y].ch[1]);
t[z].ch[y == t[z].ch[1]] = x;
t[x].ff = z;
t[y].ch[k] = t[x].ch[k ^ 1];
t[t[x].ch[k ^ 1]].ff = y;
t[x].ch[k ^ 1] = y;
t[y].ff = x;
} void splay(int x, int aim) {
while (t[x].ff != aim) {
int y = t[x].ff;
int z = t[y].ff;
if (z != aim) {
(t[z].ch[0] == y) ^ (t[y].ch[0] == x) ? rotate(x) : rotate(y);
}
rotate(x);
}
if (aim == 0)rt = x;
} void Insert(int x) {
int now = rt, ff = 0;
while (now&&t[now].val != x) {
ff = now; now = t[now].ch[t[now].val < x];
}
if (now);
else {
now = ++tot;
if (ff)t[ff].ch[t[ff].val < x] = now;
t[now].ff = ff;
t[now].ch[0] = t[now].ch[1] = 0;
t[now].val = x;
}
splay(now, 0);
} void Find(int x) {
int now = rt;
if (now == 0)return;
while (t[now].ch[x > t[now].val] && x != t[now].val) {
now = t[now].ch[x > t[now].val];
}
splay(now, 0);
} int nxt1(int x, int f) {
Find(x);
int now = rt;
if (t[now].val >= x && f)return now;
if (t[now].val <= x && !f)return now;
now = t[now].ch[f];
while (t[now].ch[f ^ 1])now = t[now].ch[f ^ 1];
return now;
} int nxt2(int x, int f) {
Find(x);
int now = rt;
if (t[now].val > x&&f)return now;
if (t[now].val < x && !f)return now;
now = t[now].ch[f];
while (t[now].ch[f ^ 1])now = t[now].ch[f ^ 1];
return now;
} void del(int x) {
int l = nxt2(x, 0);
int r = nxt2(x, 1);
splay(l, 0); splay(r, l);// split操作
t[r].ch[0] = 0;
} int main()
{
//ios::sync_with_stdio(0);
rdint(n);
int cnt = 0, ans = 0;
Insert(inf); Insert(-inf);
REP(i, n) {
int op, x; rdint(op); rdint(x);
if (cnt == 0)Insert(x);
if (cnt > 0) {// 宠物splay树
if (op == 0)Insert(x);
else {
int tmp1 = t[nxt1(x, 0)].val;//前驱
int tmp2 = t[nxt1(x, 1)].val;// 后继
if (abs(tmp1 - x) <= abs(tmp2 - x)) {
(ans += abs(tmp1 - x)) %= 1000000; del(tmp1);//删除节点
}
else {
(ans += abs(tmp2 - x)) %= 1000000; del(tmp2);
}
}
}
if (cnt < 0) {// 顾客splay树
if (op == 1)Insert(x);
else {
int tmp1 = t[nxt1(x, 0)].val;
int tmp2 = t[nxt1(x, 1)].val;
if (abs(tmp1 - x) <= abs(tmp2 - x)) {
(ans += (abs(tmp1 - x))) %= 1000000; del(tmp1);
}
else {
(ans += (abs(tmp2 - x))) %= 1000000; del(tmp2);
}
}
}
cnt = cnt + (op == 0 ? 1 : -1);
}
cout << ans << endl;
return 0;
}

[HNOI2004]宠物收养场 BZOJ1208 splay tree的更多相关文章

  1. 【题解】 [HNOI2004]宠物收养场(Splay)

    懒得复制,戳我戳我 Solution: \(Splay\)板子,注意交换的地方,然后就是注意不要越界node[x],应该是\(node[now]\),其次就是数组可以开大点 Code: //It is ...

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

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

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

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

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

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

  5. LG_2286_[HNOI2004]宠物收养场

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

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

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

  7. P2286 [HNOI2004]宠物收养场

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

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

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

  9. 1208. [HNOI2004]宠物收养场【平衡树-splay】

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

随机推荐

  1. Celery-4.1 用户指南: Application(应用)

    Application Celery 库在使用之前必须初始化,一个celery实例被称为一个应用(或者缩写 app). Celery 应用是线程安全的,所以多个不同配置.不同组件.不同任务的 应用可以 ...

  2. python学习笔记(一):python简介和入门

    最近重新开始学习python,之前也自学过一段时间python,对python还算有点了解,本次重新认识python,也算当写一个小小的教程.一.什么是python?python是一种面向对象.解释型 ...

  3. 问题:不支持Dictionary;结果:在Web Service中傳送Dictionary

    在Web Service中傳送Dictionary 有個需求,想在Web Service中傳遞Dictionary<string, string>參數,例如: 排版顯示純文字 [WebMe ...

  4. Python之POST登录测试

    不解释,直接上代码: #!/usr/bin/env python # -*- encoding: utf-8 -*- """ @version: v1.0 @author ...

  5. Mediaplayer

    Mediaplayer报错 prepareAsync called in state 1     是因为在setDataSource之前调用了prepare.因为setDataSource放到了线程里 ...

  6. 使用JSONObject类来生成json格式的数据

    JSONObject类不支持javabean转json 生成json格式数据的方式有: 1.使用JSONObject原生的来生成 2.使用map构建json格式的数据 3.使用javabean来构建j ...

  7. 如何选择RDBMS关系型数据库和Nosql非关系型数据库?

    RDBMS关系型数据库和Nosql非关系型数据库区别: 一.RDBMS是关系型数据库模式: 1.二维模式,由行列组成. 2.非常强调事务原子性,例如用户提出一个请求,DB完整的去执行,如果报错就全部回 ...

  8. ZF 语法

    Zend Framework Command Line Console Tool v1.11.11 Details for action "" and provider " ...

  9. Opencv中Rect_类

    Rect_类有些意思,成员变量x.y.width.height,分别为左上角点的坐标和矩形的宽和高.常用的成员函数有Size()返回值为一个Size,area()返回矩形的面积,contains(Po ...

  10. Java-马士兵设计模式学习笔记-工厂模式-用Jdom模拟Spring

    一.概述 1.目标:模拟Spring的Ioc 2.用到的知识点:利用jdom的xpath读取xml文件,反射 二.有如下文件: 1.applicationContext.xml <?xml ve ...