HYSBZ1588 营业额统计【Splay】
转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4366582.html ---by 墨染之樱花
【题目链接】http://www.lydsy.com/JudgeOnline/problem.php?id=1588
【题目描述】逐个将数插入序列,定义最小波动为某个数与其之前的小于等于它的最大数与大于等于它的最小数和它的差值的较小值,求整个序列的最小波动之和。
【思路】经典的不能再经典的平衡树题目,可用于测试各种平衡树模板。今天刚写了一棵自己风格的splay,用这道题测试一下,156ms效果还不错
/* ***********************************************
Author :Kirisame_Marisa
blog :http://www.cnblogs.com/KirisameMarisa/
Created Time :2015年03月24日 星期二 20时45分12秒
File Name :splay.cpp
************************************************ */ #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
using namespace std;
const int INF=0x3f3f3f3f;
const int MAXN=;
#define eps 1e-10
#define zero(x) (fabs(x)<eps)
#define REP(X,N) for(int X=0;X<N;X++)
#define REP2(X,L,R) for(int X=L;X<=R;X++)
#define CLR(A,X) memset(A,X,sizeof(A))
#define PB(X) push_back(X)
#define MP(X,Y) make_pair(X,Y)
#define IT iterator
#define test puts("OK")
typedef long long ll;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<PII> VII; const int null=-; struct node
{
int par;
int cld[]; //0是左儿子,1是右儿子
int key;
} ts[MAXN];
int root,cnt; void init()
{
root=null;
cnt=;
} int newnode(int p,int k)
{
ts[cnt].key=k;
ts[cnt].par=p;
ts[cnt].cld[]=ts[cnt].cld[]=null;
return cnt++;
} void rotate(int x,int k) //k=0为左旋,k=1为右旋
{
int y=ts[x].par;
ts[y].cld[!k]=ts[x].cld[k];
if(ts[x].cld[k]!=null)
ts[ts[x].cld[k]].par=y;
ts[x].par=ts[y].par;
if(ts[y].par!=null)
{
if(y==ts[ts[y].par].cld[])
ts[ts[y].par].cld[]=x;
else
ts[ts[y].par].cld[]=x;
}
ts[y].par=x;
ts[x].cld[k]=y;
} void splay(int x,int S) //伸展操作,将x旋转到目标节点,其中S为目标节点的parent
{
while(ts[x].par!=S)
{
int p=ts[x].par;
if(ts[p].par==S)
rotate(x,ts[p].cld[]==x);
else
{
int d=(ts[ts[p].par].cld[]==p);
if(ts[p].cld[d]==x)
rotate(x,!d),rotate(x,d);
else
rotate(p,d),rotate(x,d);
}
}
if(S==-)
root=x;
} bool insert(int x)
{
if(root==null)
{
root=newnode(null,x);
return ;
}
int r=root,pre=null;
while(r!=null)
{
if(ts[r].key==x)
{
splay(r,null); //如果直接找到的话就不新建节点,直接splay
return ;
}
else
{
pre=r;
r=ts[r].cld[ts[r].key<x];
}
}
int &t=ts[pre].cld[ts[pre].key<x];
t=newnode(pre,x);
splay(t,null);
return ;
} int getlow(int x) //获取比它小的最大值。由于插入操作x已经被旋转到根节点,所以只要寻找左子树的最大值即可,下同
{
int d=ts[root].cld[];
if(d==null)
return INF;
while(ts[d].cld[]!=null)
d=ts[d].cld[];
return x-ts[d].key;
} int getup(int x) //获取比它大的最小值
{
int d=ts[root].cld[];
if(d==null)
return INF;
while(ts[d].cld[]!=null)
d=ts[d].cld[];
return ts[d].key-x;
} void debug(int x)
{
int l=ts[x].cld[],r=ts[x].cld[];
if(l!=null)
debug(l);
printf("id:%2d key:%2d par:%2d lcd:%2d rcd:%2d\n",x,ts[x].key,ts[x].par,l,r);
if(r!=null)
debug(r);
} int main()
{
//freopen("in","r",stdin);
//freopen("out","w",stdout);
init();
int n,x,sum=;
scanf("%d%d",&n,&x);
sum+=x;
insert(x);
REP(i,n-)
{
x=;
scanf("%d",&x);
bool temp=insert(x);
if(temp)
sum+=min(getlow(x),getup(x));
}
printf("%d\n",sum);
return ;
}
代码君
HYSBZ1588 营业额统计【Splay】的更多相关文章
- NOI 2002 营业额统计 (splay or fhq treap)
Description 营业额统计 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成立以来每 ...
- 【BZOJ-1588】营业额统计 Splay
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 12485 Solved: 4508[Submit][Sta ...
- BZOJ1588 HNOI2002 营业额统计 [Splay入门题]
[HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 4128 Solved: 1305 Description 营业额统计 ...
- NOIP 营业额统计 splay tree 纯模板
2924: 营业额统计 Time Limit(Common/Java):1000MS/3000MS Memory Limit:65536KByteTotal Submit: 389 ...
- 1588: [HNOI2002]营业额统计 (splay tree)
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 5783 Solved: 1859[Submit][Stat ...
- 洛谷P2234 [HNOI2002] 营业额统计 [splay]
题目传送门 营业额统计 题目描述 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成立以来每天 ...
- [HNOI2002]营业额统计 Splay tree入门题
题目连接:http://www.lydsy.com/JudgeOnline/problem.php?id=1588 1588: [HNOI2002]营业额统计 Time Limit: 5 Sec ...
- BZOJ1588 [HNOI2002]营业额统计 splay模板
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MB Submit: 16189 Solved: 6482 [Submit][S ...
- bzoj1588: [HNOI2002]营业额统计(splay)
1588: [HNOI2002]营业额统计 题目:传送门 题解: 复习splay所以来刷个水... 题目描述不是特别清楚:应该是找第i天以前一个最小的营业额和第i天做差的最小值作为第i天的最小波动值 ...
- Bzoj 1588: [HNOI2002]营业额统计(splay)
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MB Description 营业额统计 Tiger最近被公司升任为营业部经理,他上 ...
随机推荐
- 阿里云ECS每天一件事D4:安装mysql5.5.40
Linux平台上MySQL也没什么好说的了,首先准备一下软件环境: yum install gcc gcc-c++ gcc-g77 autoconf automake make cmake bison ...
- WM_SYSCOMMAND包括很多功能,比如:拖动左边框、拖动标题栏、滚动条滚动、点击最小化、双击标题栏——Delphi 通过事件代替了大部分常用的消息,所以Delphi 简单、易用、高效
procedure TForm1.WMSysCommand(var Message: TWMSysCommand); var str: string; begin case Message.CmdTy ...
- HDU 1012 u Calculate e
题解:直接模拟 #include <cstdio> int main(){ puts("n e");puts("- -----------");pu ...
- 收藏:左路Deep Learning+右路Knowledge Graph,谷歌引爆大数据
发表于2013-01-18 11:35| 8827次阅读| 来源sina微博 条评论| 作者邓侃 数据分析智能算法机器学习大数据Google 摘要:文章来自邓侃的博客.数据革命迫在眉睫. 各大公司重兵 ...
- 我的Hook学习笔记
关于Hook 一.基本概念: 钩子(Hook),是Windows消息处理机制的一个平台,应用程序能够在上面设置子程以监视指定窗体的某种消息,并且所监视的窗体能够是其它进程所创建的.当消息到达后,在目标 ...
- Android 开发UI牛博[转]
Android 新兴的UI模式——侧边导航栏 侧边导航栏也就是大家熟知的SliddingMenu,英文也叫Fly-In App Menu.Side Navigation等.当然谷歌现在已经推出类似这个 ...
- 关于android 双击事件
大家好,关于android双击事件 我相信大家都知道 API中是有个方法的,但是必须在Activity中在能使用. 对于到底用不用android 双击事件API各有各的看法. 在Activity中使用 ...
- 手把手教你在openshift上搭建wordpress博客(二)
相同公布于:http://www.longgaming.com/archives/128 推荐前往阅读 这一篇文章主要介绍一些经常使用插件的使用和配置. 下面是我个人安装的一些插件.大家能够依据须要自 ...
- 提前防止Non-PIE错误,检测app是否包含PIE标志
//Howard 2013-07-19 //如何检测app是否包含PIE标志? 答:使用xCode自带的otool工具. otool程序在Xcode.app/Contents/Developer/us ...
- javascript条件运算符
variablename=(condition)?value1:value2 javascript条件运算符