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相等的),比较哪个差值绝对 ...
随机推荐
- 第十七章——配置SQLServer(2)——32位和64位系统中的内存配置
原文:第十七章--配置SQLServer(2)--32位和64位系统中的内存配置 前言: 本文讲述32位和64位系统中的内存配置,在SQLServer 2005/2008中,DBA们往往尝试开启AWE ...
- 中颖电子AD操作
#define ADC_DIS 0 #define ADC_ENB 1 //ADC通道号定义 #define ADC_Chanel0 (unsigned char)(0x00<<1) #d ...
- UVA 239 - Tempus et mobilius. Time and motion(更换周期)
UVA 239 - Tempus et mobilius. Time and motion 题目链接 题意:这题题意也是吊得飞起,看了老半天,大概是这样: 有一个放球的队列.和3个轨道(说白了就是栈) ...
- Web层的搭建
Web层的搭建 前言:好久没更新博客了,每天被该死的业务缠身,今天正好一个模块完成了,继续来完善我们的代码.之前的六篇完成了领域层.应用层.以及基础结构层的部分代码,这篇打算搭建下UI层的代码. DD ...
- UVA10375 Choose and divide 质因数分解
质因数分解: Choose and divide Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %l ...
- git checkout 命令详解(转)
在日常的git操作中,git checkout——检出,是我们的常用命令.最为常用的两种情形是创建分支和切换分支. 在下面的命令中,使用了一些简写,在这里说明一下: git st # git stat ...
- Pro Aspnet MVC 4读书笔记(3) - Essential Language Features
Listing 4-1. The Initial Content of the Home Controller using System; using System.Collections.Gener ...
- Swift的74标准功能
Swift中共同拥有74个内建函数,可是在Swift官方文档("The Swift Programming Language")中仅仅记录了7中.剩下的67个都没有记录. 本文将列 ...
- 仿jQuery之链式调用
链式调用的形式其实就是对象调用一连串的方法.为什么能连续调用这么多的方法?因为调用方法返回调用的对象,于是乎就可以一如既往,一往无前地调用下去.链式调用的原理就是在方法中返回执行上下文this,每次调 ...
- 全网最全ASP.NET MVC 教程汇总
全网最全ASP.NET MVC 教程汇总 MVC架构已深得人心,微软也不甘落后,推出了Asp.net MVC.小编特意整理博客园乃至整个网络最具价值的MVC技术原创文章,为想要学习ASP.NET MV ...