转载请注明出处,谢谢: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】的更多相关文章

  1. NOI 2002 营业额统计 (splay or fhq treap)

    Description 营业额统计 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成立以来每 ...

  2. 【BZOJ-1588】营业额统计 Splay

    1588: [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 12485  Solved: 4508[Submit][Sta ...

  3. BZOJ1588 HNOI2002 营业额统计 [Splay入门题]

    [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 4128  Solved: 1305 Description 营业额统计 ...

  4. NOIP 营业额统计 splay tree 纯模板

    2924: 营业额统计 Time Limit(Common/Java):1000MS/3000MS     Memory Limit:65536KByteTotal Submit: 389       ...

  5. 1588: [HNOI2002]营业额统计 (splay tree)

    1588: [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 5783  Solved: 1859[Submit][Stat ...

  6. 洛谷P2234 [HNOI2002] 营业额统计 [splay]

    题目传送门 营业额统计 题目描述 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成立以来每天 ...

  7. [HNOI2002]营业额统计 Splay tree入门题

    题目连接:http://www.lydsy.com/JudgeOnline/problem.php?id=1588 1588: [HNOI2002]营业额统计 Time Limit: 5 Sec   ...

  8. BZOJ1588 [HNOI2002]营业额统计 splay模板

    1588: [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MB Submit: 16189  Solved: 6482 [Submit][S ...

  9. bzoj1588: [HNOI2002]营业额统计(splay)

    1588: [HNOI2002]营业额统计 题目:传送门 题解: 复习splay所以来刷个水... 题目描述不是特别清楚:应该是找第i天以前一个最小的营业额和第i天做差的最小值作为第i天的最小波动值 ...

  10. Bzoj 1588: [HNOI2002]营业额统计(splay)

    1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MB Description 营业额统计 Tiger最近被公司升任为营业部经理,他上 ...

随机推荐

  1. hdu 4455 Substrings(找规律&DP)

    Substrings Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  2. ButterKnife使用小结

    项目官网:http://jakewharton.github.io/butterknife/ Github主页:https://github.com/JakeWharton/butterknife 这 ...

  3. 少部分手机浏览器对于COOKIE支持不够导致服务端无法读取session的解决方案

    相信大家都遇到过这样的问题,有手机浏览器的问题导致服务端SESSION读取不正常,目前在项目中的解决方法是采取H5手机本地存储唯一KEY解决的 代码片段 //定义json格式字符串 var userD ...

  4. oracle查询字符集语句

      (1)查看字符集(三条都是等价的) 复制代码 代码如下: select * from v$nls_parameters  where parameter='NLS_CHARACTERSET'sel ...

  5. Oracle ODI系列之一(ODI知识模块)

    Oracle ODI系列之一(ODI知识模块)     ODI简介 ODI(Oracle Data Integrator)前身是Sunopsis Active Integration Platform ...

  6. 转: Apache SSI详解及应用

    转: Apache SSI详解及应用 什么是 SSI? SSI(Server Side Includes),是嵌套在 HTML 网页中的指示语句,由后台服务器进行代码的解释计算.使用 SSI 可以动态 ...

  7. 基于MDK的ARM-GCC开发环境建立及新唐M0的HID类设备的C++开发

    一,下载安装测试arm-none-eabi-gcc编译工具链 1,查看arm-none-eabi-gcc编译工具版本        打开网页:https://sourcery.mentor.com/G ...

  8. hdoj 2620 Bone Collector(0-1背包)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 思路分析:该问题为经典的0-1背包问题:假设状态dp[i][v]表示前i件物品恰放入一个容量为v ...

  9. 纯JavaScript实现HTML5 Canvas六种特效滤镜

    纯JavaScript实现HTML5 Canvas六种特效滤镜  小试牛刀,实现了六款简单常见HTML5 Canvas特效滤镜,并且封装成一个纯 JavaScript可调用的API文件gloomyfi ...

  10. spring4.1.3+springmvc+mybatis3.2.1整合

    注意:这里使用了mybatis3.2.1版本,刚开始用了3.4.1的版本,会报一个很奇怪的错(java.lang.AbstractMethodError: org.mybatis.spring.tra ...