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相等的),比较哪个差值绝对 ...
随机推荐
- SynchronousQueue、LinkedBlockingQueue、ArrayBlockingQueue性能测试(转)
听说JDK6对SynchronousQueue做了性能优化,避免对竞争资源加锁,所以想试试到底平时是选择SynchronousQueue还是其他BlockingQueue. 对于容器类在并发环境下的比 ...
- js cookie设置最大过期时间 Infinity
Note: 对于永久cookie我们用了Fri, 31 Dec 9999 23:59:59 GMT作为过期日.如果你不想使用这个日期,可使用世界末日Tue, 19 Jan 2038 03:14:07 ...
- 定义你自己ViewGroup
尊重原创:http://blog.csdn.net/yuanzeyao/article/details/40264433 好久都没有写文章了,如今利用周末的时间对一些知识进行总结.便于加深理解,今天我 ...
- windows 10 install oracle 12c error:[ INS-30131 ]
"[ INS-30131 ] the Initial Setup That Is Required to Run the Installation Program Validation Wa ...
- Android Studio中导入Android项目StepbyStep
想把在eclipse的项目导入Android studio,有两种方法,但是我喜欢的是不改变项目文件结构的方法,因为这样可以兼容eclipse. 第一步: 导入的项目不能运行,需要配置运行环境.And ...
- Maven直接部署Web应用Tomcat
1. 下载解压版tomcat,并配置环境变量.所以tomcat你可以成功启动. 使用版本解压tomcat可以方便查看tomcat的后台输出的出错信息,便于调试. 2. 给tomcat配置用户名密码. ...
- directX--大约CSource和CSourceStream (谁在叫fillbuffer)
CSourceStream类别,它是CSource类别OutputPin[source.h/source.cpp] 派生自CAMThread和CBaseOutputPinl 成员变量: ...
- Hadoop 它们的定义Writable NullpointerException
Hadoop周边环境:Hadoop2.4 定义中的Hadoop的Writable时间,有时你需要使用数组,而不是简单的单一值或串.例如,下面的代码: package test; import java ...
- Azure VM Public IP设置
Azure虚拟机的Public IP是用于客户端直连云中的虚拟机,可以认为是一个外网IP,一般我们为虚拟机设置终结点,例如HTTP的80端口,如果使用Public IP可以不使用Azure Porta ...
- 试图加载格式不正确的程序。 (Exception from HRESULT: 0x8007000B)
原文:试图加载格式不正确的程序. (Exception from HRESULT: 0x8007000B) 今天在电脑上部署公司的项目,出现这个错误.Bing后,找到原来是因为项目是32位的,而我的系 ...