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相等的),比较哪个差值绝对 ...
随机推荐
- JBoss配置解决高并发连接异常问题(转)
这两天一个项目在做压力测试的时候,发现只要并发数超过250个,连续测试两轮就会有连接异常出现,测试轮数越多出现越频繁,异常日志如下: Caused by: com.caucho.hessian.cli ...
- C++中的class (2)
class Father { protected void methodA(){ //do something } private void methodB(){//do something } } ...
- 嵌入在网站上Flash播放机(2)
然后在一个博客.这里有一个flash嵌入式播放器.这是公司内部使用的flash播放机,支持格更多款式,同时支持swf格视频播放的类型. 以下是页面嵌入代码: <link rel="st ...
- 有JSON中字段最好是【字符】而非【enum】想到
最近听了牛人一句: 1,如果协议中定义了tag的话,协议的解析就不会依赖到变化,那么开发的话也更为独立. eg: good: name=“zl”, gender=“f” bad: name=" ...
- VS2012 安装出错 :通道正在关闭
从微软官网下的安装包iso,解压后安装时总是出现 3个错误,提示什么管道正在关闭,看了很多解决办法,挑了一个简单的:安装包有问题,重新下载一个,就好了(持续更新....)
- Unity GUI选择与评价
因为Unity内建的GUI不管是不是从开发效率或效率强制,因此,许多派生GUI插入,什么插件的选择,是一个非常值它被认为是. 既然是评价,就会有非常多的主观意识,这不一定成为选择的根据. 再比方.我已 ...
- 他们控制的定义-DragButton
一个.叙述性说明 可拖动Button 两.无图无真相 这是用在实际工程效果图.和demo不太一样. 三.源代码 https://github.com/mentor811/Demo_DragButton ...
- <%%>创建内联代码块(表达)
其实<%%>很早之前见过它,将一个小的功能仅.别人不理解.今天偶尔,我们看到它的真面目,今天,给大家分享. 语法 代码块呈现(<%%>)定义了当呈现页时运行的内联代码或内联表达 ...
- VisualStudio 怎么使用Visual Leak Detector
VisualStudio 怎么使用Visual Leak Detector 那么在Windows下有什么好的内存泄漏检测工具呢?微软提供Visual Studio开发工具本身没有什么太好的内存泄漏检测 ...
- 《高性能 JavaScript》读书笔记(一)
一. 加载和执行——优化JavaScript规则: 1. 将脚本放在底部:2. 减少页面中外链脚本文件的数量: 比如,下载单个100kb的文件将比下载4个25kb的文件更快.这个可以通过离线打包工具或 ...