[luogu2286][宠物收养所]
思路
比较裸的一道平衡树的题。用一个变量S来表示当前树的情况,当S为负数时树内为宠物,当S为正数时树内为人。然后每次分情况讨论一下。如果树为空或者是与来的东西(人或宠物)与树内存的相同。那么就无法领养,直接将这个东西扔到树里。否则就从树里面找一个与当前值最接近的数字,然后统计进答案。
一开始把INF设的太小了影响了统计答案。
代码
#include<cstdlib>
#include<ctime>
#include<cstdio>
#include<iostream>
#define ls TR[cur].ch[0]
#define rs TR[cur].ch[1]
using namespace std;
typedef long long ll;
const int N = 100000,mod = 1000000;
const ll INF = 1e17 + 10;
ll read() {
ll x=0,f=1;char c=getchar();
while(c<'0'||c>'9') {
if(c=='-') f=-1;
c=getchar();
}
while(c>='0'&&c<='9') {
x=x*10+c-'0';
c=getchar();
}
return x*f;
}
struct node {
int ch[2],id;
ll val;
}TR[N];
void rotate(int &cur,int f) {
int son = TR[cur].ch[f];
TR[cur].ch[f] = TR[son].ch[f ^ 1];
TR[son].ch[f ^ 1] = cur;
cur = son;
}
int tot;
void insert(int &cur,int val) {
if(!cur) {
cur = ++tot;
TR[cur].val = val;
TR[cur].id = rand();
return;
}
int d = val > TR[cur].val;
insert(TR[cur].ch[d],val);
if(TR[TR[cur].ch[d]].id < TR[cur].id) rotate(cur,d);
}
void del(int &cur,int val) {
if(!cur) return;
if(val == TR[cur].val) {
if(!ls || !rs) {cur = ls + rs;return;}
int d = TR[ls].id > TR[rs].val;
rotate(cur,d);
del(cur,val);
}
del(TR[cur].ch[val > TR[cur].val],val);
}
ll pred(int cur,int val) {
if(!cur) return -INF;
if(val <= TR[cur].val) return pred(ls,val);
return max(TR[cur].val,pred(rs,val));
}
ll nex(int cur,int val) {
if(!cur) return INF;
if(val >= TR[cur].val) return nex(rs,val);
return min(TR[cur].val,nex(ls,val));
}
int S;
ll ans;
int main() {
srand(time(0));
int n = read(),rt = 0;
while(n--) {
int bz = read(),x = read();
if(bz == 0) {
if(S <= 0) insert(rt,x);
else {
int k1 = pred(rt,x),k2 = nex(rt,x);
int dele = k1;
if(k2 - x < x - k1) dele = k2;
ans += abs(dele - x);
ans %= mod;
del(rt,dele);
}
S--;
}
if(bz == 1) {
if(S >= 0) insert(rt,x);
else {
int k1 = pred(rt,x),k2 = nex(rt,x);
int dele = k1;
if(k2 - x < x - k1) dele = k2;
ans += abs(dele - x);
ans %= mod;
del(rt,dele);
}
S++;
}
}
cout<<ans<<endl;
}
一言
无论做什么,记得为自己而做,那就毫无怨言。 ——流金岁月
[luogu2286][宠物收养所]的更多相关文章
- Bzoj1208 [HNOI2004]宠物收养所
Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 7457 Solved: 2960 Description 最近,阿Q开了一间宠物收养所.收养所提供两 ...
- BZOJ 1208: [HNOI2004]宠物收养所
1208: [HNOI2004]宠物收养所 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 7514 Solved: 2982[Submit][Sta ...
- 宠物收养所(bzoj1208)
Description 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物.每个领养者都希望领养到自己满意的宠物,阿Q根据领养者的要求通过他自己发明的一个特 ...
- 【BZOJ1208】[HNOI2004]宠物收养所 Splay
还是模板题,两颗splay,找点删即可. #include <iostream> #include <cstdio> #include <cstdlib> #def ...
- 【BZOJ-1208】宠物收养所 Splay
1208: [HNOI2004]宠物收养所 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 6638 Solved: 2601[Submit][Sta ...
- BZOJ1208 宠物收养所
Description 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物.每个领养者都希望领养到自己满意的宠物,阿Q根据领养者的要求通过他自己发明的一个特 ...
- C++之路进阶——codevs1285(宠物收养所)
1285 宠物收养所 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 最近,阿Q开了一间宠物收养所.收养所提供两种服 ...
- bzoj 1208: [HNOI2004]宠物收养所 set
1208: [HNOI2004]宠物收养所 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 7328 Solved: 2892[Submit][Sta ...
- BZOJ_1208_&_Codevs_1258_[HNOI2004]_宠物收养所_(平衡树/set)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1208 (据说codevs要更新?就不放codevs的地址了吧...) 有宠物和人,每个单位都有 ...
随机推荐
- Golang的面向对象实践method
最近在系统的学习go的语法,一切都弄好了之后准备弄个im项目出来玩.在这个过程中会把看到有趣的写法和语法啥的拿出来分析一下. 我一直以为go语言对面向对象没有支持,但是后面看到了类似类的概念,meth ...
- macOS & USB stick
macOS & USB stick why macOS can only read USB stick, can not write files to USB stick macos 无法写文 ...
- 在delphi中生成GUID/自动获取临时表名......
什么是 GUID ? 全球唯一标识符 (GUID) 是一个字母数字标识符,用于指示产品的唯一性安装.在许多流行软件应用程序(例如 Web 浏览器和媒体播放器)中,都使用 GUID. GUID 的格式为 ...
- 五、core开发
一.支付方面的 https://www.cnblogs.com/stulzq/p/7606164.htmlhttps://www.cnblogs.com/guolianyu/
- php重定向http请求
302 临时重定向 301 永久重定向 ( 302 和 301 的区别主要在于搜索引擎,搜索引擎一般不会抓取临时重定向的页面 ) 301 和302 适用于 普通的GET 请求: 如果 ...
- EFI Windows 7 activition
mountvol X: /s copy SLIC.aml X:\EFI\CLOVER\ACPI\WINDOWS BOOTICE X:\EFI\CLOVER\CLOVERX64.efi slmgr -i ...
- 【python练习题】程序9
#题目:暂停一秒输出. import time for i in range(5): print (i) time.sleep(1)
- 洛谷P3389 【模板】高斯消元法
P3389 [模板]高斯消元法 题目背景 Gauss消元 题目描述 给定一个线性方程组,对其求解 输入输出格式 输入格式: 第一行,一个正整数 n 第二至 n+1行,每行 n+1 个整数,为a1,a ...
- Codeforces Round #424 Div. 1
A:二分答案,从左往右考虑每个人,选尽量靠左的钥匙即可. #include<iostream> #include<cstdio> #include<cmath> # ...
- Codeforces Round #441 Div. 1
A:显然答案与原数的差不会很大. #include<iostream> #include<cstdio> #include<cmath> #include<c ...