BZOJ 1208 HNOI2004 宠物收容所 平衡树/set
标题效果:有一个宠物收容所。目前还没有被采纳的宠物或谁想要领养宠物,每个宠物有个性值,大家谁想要领养宠物具有理想人格值。每一刻,宠物收容所只是为了有谁想要领养宠物或宠物的人。
当领走宠物,将有一定程度的不愉快。最低要求是不舒服程度。
思路:就是个模拟+数据结构维护。用set能够水过,时间卡的不是非常紧。练手写了Treap。
注意极大值不能开太大。会re
CODE:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define INF 0x3f3f3f3f
using namespace std; struct Complex{
int val,random,cnt,size;
Complex *son[2]; Complex() {
son[0] = son[1] = NULL;
random = rand();
cnt = size = 1;
}
void Maintain() {
size = cnt;
if(son[0] != NULL) size += son[0]->size;
if(son[1] != NULL) size += son[1]->size;
}
int Compare(int x) {
if(x == val) return -1;
return x > val;
}
}*root; int cnt,ans;
int total; int flag; inline void Rotate(Complex *&a,bool dir);
void Insert(Complex *&a,int x);
void Delete(Complex *&a,int x);
int FindSucc(Complex *a,int x);
int FindPred(Complex *a,int x); int main()
{
cin >> cnt;
for(int x,i = 1;i <= cnt; ++i) {
scanf("%d%d",&flag,&x);
if(total > 0) {
if(flag) Insert(root,x),total++;
else {
int pred = FindPred(root,x);
int succ = FindSucc(root,x);
int will_delete = (x - pred <= succ - x) ? pred:succ;
Delete(root,will_delete);
total--;
ans += abs(will_delete - x);
}
}
else if(total < 0) {
if(!flag) Insert(root,x),total--;
else {
int pred = FindPred(root,x);
int succ = FindSucc(root,x);
int will_delete = (x - pred <= succ - x) ? pred:succ;
Delete(root,will_delete);
total++;
ans += abs(will_delete - x);
}
}
else Insert(root,x),total = (flag ? 1:-1);
ans %= 1000000;
}
cout << ans << endl;
return 0;
} inline void Rotate(Complex *&a,bool dir)
{
Complex *k = a->son[!dir];
a->son[!dir] = k->son[dir];
k->son[dir] = a;
a->Maintain(),k->Maintain();
a = k;
} void Insert(Complex *&a,int x)
{
if(a == NULL) {
a = new Complex();
a->val = x;
return ;
}
int dir = a->Compare(x);
if(dir == -1)
a->cnt++;
else {
Insert(a->son[dir],x);
if(a->son[dir]->random > a->random)
Rotate(a,!dir);
}
a->Maintain();
} void Delete(Complex *&a,int x)
{
int dir = a->Compare(x);
if(dir != -1)
Delete(a->son[dir],x);
else {
if(a->cnt > 1)
a->cnt--;
else {
if(a->son[0] == NULL) a = a->son[1];
else if(a->son[1] == NULL) a = a->son[0];
else {
bool _dir = (a->son[0]->random > a->son[1]->random);
Rotate(a,_dir);
Delete(a->son[_dir],x);
}
}
}
if(a != NULL) a->Maintain();
} int FindPred(Complex *a,int x)
{
if(a == NULL) return -INF;
if(a->val > x) return FindPred(a->son[0],x);
return max(a->val,FindPred(a->son[1],x));
} int FindSucc(Complex *a,int x)
{
if(a == NULL) return INF;
if(a->val < x) return FindSucc(a->son[1],x);
return min(a->val,FindSucc(a->son[0],x));
}
版权声明:本文博客原创文章。博客,未经同意,不得转载。
BZOJ 1208 HNOI2004 宠物收容所 平衡树/set的更多相关文章
- BZOJ 1208: [HNOI2004]宠物收养所
1208: [HNOI2004]宠物收养所 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 7514 Solved: 2982[Submit][Sta ...
- bzoj 1208: [HNOI2004]宠物收养所 set
1208: [HNOI2004]宠物收养所 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 7328 Solved: 2892[Submit][Sta ...
- BZOJ 1208: [HNOI2004]宠物收养所(BST)
本来想先用set写一遍,再自己写个splay或treap,不过用set过了之后就懒得去写了....以后有空再来写吧..(不会有空的吧= = ------------------------------ ...
- bzoj 1208: [HNOI2004]宠物收养所 (Treap)
链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1208 题面: 1208: [HNOI2004]宠物收养所 Time Limit: 10 ...
- BZOJ 1208: [HNOI2004]宠物收养所 SET的妙用
1208: [HNOI2004]宠物收养所 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 4902 Solved: 1879 题目连接 http:/ ...
- Bzoj 1208: [HNOI2004]宠物收养所(splay)
1208: [HNOI2004]宠物收养所 Time Limit: 10 Sec Memory Limit: 162 MB Description 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收 ...
- bzoj 1208 HNOI2004宠物收养所 平衡树
裸平衡树,恢复手感用的 //By BLADEVIL var n :longint; i :longint; x, y :longint; t, tot :longint; key, s, left, ...
- BZOJ 1208 [HNOI2004]宠物收养所:Splay(伸展树)
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1208 题意: 有一个宠物收养所,在接下来一段时间内会陆续有一些宠物进到店里,或是一些人来领 ...
- BZOJ 1208 [HNOI2004]宠物收养所 | SPlay模板题
题目: 洛谷也能评 题解: 记录一下当前树维护是宠物还是人,用Splay维护插入和删除. 对于任何一次询问操作都求一下value的前驱和后继(这里前驱和后继是可以和value相等的),比较哪个差值绝对 ...
随机推荐
- ExtJs--02--MessageBox相关弹出窗口alert,prompt,confirm采用
/* Ext.onReady(function(){ Ext.MessageBox.alert("jack","tom"); Ext.MessageBox.al ...
- AM335x(TQ335x)学习笔记——u-boot-2014.10移植
根据最近移植u-boot-2014.10至TQ335x,基于这样的假设am335x evm移植.不是很多地方需要改变. 因为TI的am335x evm开发了使用eeprom船上保存配置信息.它使用不同 ...
- mod_wsgi + pymssql通路SQL Server座
靠pymssql通路SQL Server时刻,直接地python没有问题的执行.靠mod_wsgi和Apache当部署.所有请求被发现hang然后数据库查询. 通过google查到了答案,感谢goog ...
- RH253读书笔记(7)-Lab 7 Electronic Mail
Lab 7 Electronic Mail Goal: To build common skills with MTA configuration Estimated Duration: 90 min ...
- 微软MVP社区夏日巡讲诚邀您的参与: 北京,上海,西宁,成都,西安
- 编写高性能Javascript
编写高性能Javascript 多年来,Javascript一直在web应用开发中占据重要的地位,但是很多开发者往往忽视一些性能方面的知识,特别是随着计算机硬件的不断升级,开发者越发觉得Javascr ...
- window.open的小技巧分享(转)
今天再次谈起window.open是因为发现了一个比较好玩的小技巧,详细内容我们稍后详细说明. 聊到window.open,不得不说明一下他的使用方法,主要有两种形式: window. ...
- 浅析pinyin4j源码 简单利用pinyin4j对中文字符进行自然排序(转)
pinyin4j项目 官网地址 http://pinyin4j.sourceforge.net/ 我们先把资源下载下来,连同源码和jar包一起放入工程.如下图: 接下来在demo包下,我们写一个测试 ...
- Java它配备了一个很好的工具2
Jconsole 本机java自带的系统monitor具,它也可以连接到的本地远程连接java process,联系java process申请后可查看CPU,内存,主题.GC事件,能帮忙看看系统是否 ...
- HDU 1086:You can Solve a Geometry Problem too
pid=1086">You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others) Mem ...