题意:每天给你一个数,要求统计最小波动值,强制在线的就是每次从已经出现过的数值中找与当前值间隔最小的加起来

题解:splay维护,同时求解当前值的前驱和后继,找距离小的那个就好了

splay是一种二叉搜索树,可以在log(n)的时间内维护,而且通过左旋和右旋避免二叉搜索树退化成一条链,而且可以利用二叉搜索树性质方便的查找前驱和后继

推荐博客

#include<bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define C 0.5772156649
#define pi acos(-1.0)
#define ll long long
#define mod 20090717
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1 using namespace std; const double g=10.0,eps=1e-;
const int N=+,maxn=+,inf=0x3f3f3f; int rightson[N],leftson[N],father[N],value[N];
ll ans;
bool f;
int tot,n,root;
void right_rotate(int x)
{
int y=father[x],z=father[y];
leftson[y]=rightson[x];
if(rightson[x]!=-)father[rightson[x]]=y;
father[x]=z;
if(z!=-)
{
if(leftson[z]==y)leftson[z]=x;
else rightson[z]=x;
}
father[y]=x;rightson[x]=y;
}
void left_rotate(int x)
{
int y=father[x],z=father[y];
rightson[y]=leftson[x];
if(leftson[x]!=-)father[leftson[x]]=y;
father[x]=z;
if(z!=-)
{
if(leftson[z]==y)leftson[z]=x;
else rightson[z]=x;
}
father[y]=x;leftson[x]=y;
}
void splay(int x)//一直旋转到x成为root
{
// cout<<x<<"-----------"<<endl;
while(father[x]!=-)
{
// cout<<x<<" "<<father[x]<<endl;
int y=father[x],z=father[y];
if(z==-)
{
if(rightson[y]==x)left_rotate(x);
else right_rotate(x);
}
else
{
if(rightson[z]==y&&rightson[y]==x)left_rotate(y),left_rotate(x);
else if(rightson[z]==y&&leftson[y]==x)right_rotate(x),left_rotate(x);
else if(leftson[z]==y&&rightson[y]==x)left_rotate(x),right_rotate(x);
else right_rotate(y),right_rotate(x);
}
}
root=x;
}
void BSTinsert(int v,int x)//在二叉搜索树里插入
{
// cout<<x<<"-------"<<value[x]<<"---------"<<v<<endl;
if(value[x]==v)//已经存在了
{
splay(x);
f=;
return ;
}
if(value[x]>v)
{
if(leftson[x]==-)//插在此处
{
leftson[x]=tot;
father[tot]=x;
leftson[tot]=rightson[tot]=-;
value[tot]=v;
}
else BSTinsert(v,leftson[x]);
}
else
{
if(rightson[x]==-)
{
rightson[x]=tot;
father[tot]=x;
leftson[tot]=rightson[tot]=-;
value[tot]=v;
}
else BSTinsert(v,rightson[x]);
}
}
int qq(int x)//前驱
{
int y=leftson[x];
if(y==-)return y;
while(rightson[y]!=-)y=rightson[y];
return y;
}
int hj(int x)//后继
{
int y=rightson[x];
if(y==-)return y;
while(leftson[y]!=-)y=leftson[y];
return y;
}
void insertpoint(int v)
{
tot++;
f=;
BSTinsert(v,root);
if(!f)
{
tot--;
return ;//有重复的点
}
// cout<<value[tot]<<"*****************"<<father[tot]<<endl;
splay(tot);
int q=qq(tot),h=hj(tot);//找前驱和后继
int res=;
if(q!=-)res=min(res,abs(value[tot]-value[q]));
if(h!=-)res=min(res,abs(value[tot]-value[h]));
ans+=res;
}
int main()
{
int n;
while(~scanf("%d",&n))
{
scanf("%lld",&ans);
tot=;father[tot]=-;
leftson[tot]=rightson[tot]=-;
value[tot]=ans,root=tot;
for(int i=;i<=n;i++)
{
int a;
scanf("%d",&a);
insertpoint(a);
}
printf("%lld\n",ans);
}
return ;
}
/******************** ********************/

HYSBZ - 1588 splay的更多相关文章

  1. HYSBZ 1588 营业额统计 (Splay树)

    题意:给出一个公司每一天的营业额,求每天的最小波动值之和.该天的最小波动值= min { 绝对值| 该天以前某一天的营业额-该天的营业额 | }.第一天的最小波动值就是其自己. 思路:Splay伸展树 ...

  2. HYSBZ 1588 营业额统计

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1588 题意:详见题面,中文 思路:平衡树的模板题. 可用Treap,Splay,Scape ...

  3. bzoj 1588 splay模板题

    用晚自习学了一下splay模板,没想象中那么难,主要是左旋和右旋可以简化到一个函数里边,减少代码长度... #include<iostream> #include<cstdio> ...

  4. HYSBZ - 1588 营业额统计 (伸展树)

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

  5. 营业额统计 HYSBZ - 1588

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

  6. bzoj 1588营业额统计(HNOI 2002)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1588 splay  bottom-up的数组实现. 题意就是给你一组数,求每个数与在其前面且与其最相 ...

  7. CSU训练分类

    √√第一部分 基础算法(#10023 除外) 第 1 章 贪心算法 √√#10000 「一本通 1.1 例 1」活动安排 √√#10001 「一本通 1.1 例 2」种树 √√#10002 「一本通 ...

  8. BZOJ 1588: [HNOI2002]营业额统计 双向链表 / splay / treap

    1588: [HNOI2002]营业额统计 Description 营业额统计 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger ...

  9. BZOJ 1588:营业额统计(Splay)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1588 题意:中文题意. 思路:每一个点每一个点插入Splay,然后插入新的一个点之后,查这个节点的前 ...

随机推荐

  1. ionic新手教程第七课-简要说明几种界面之间的參数传递及优缺点

    截至2016年4月13日19点32分,我公布的ionic新手教程,已经公布6课了, 总訪问量将近6000,平均每节课能有1000的訪问量.当中訪客最多的是第三课有2700的訪客. watermark/ ...

  2. nginx 查看接口请求时间 每个请求图片的时间或者文件的

    根据nginx的access_log查看接口请求时间 muyuren 发表于 1年前 阅读 2300 收藏 0 推荐 0 评论 0 推荐 收藏 首先修改修改生成日志的格式,在nginx配置文件的htt ...

  3. Windows server2008 搭建ASP接口訪问连接oracle数据库全过程记录

    真的是太不easy了,曾经的时候在window server 2003上面搭建了一套asp+oracle的接口系统.就费了好大的劲儿,事实上那会迷迷瞪瞪的也不知道怎么的就弄好了,也懒得管了.OK,从昨 ...

  4. USB设备驱动程序(二)

    首先我们来看USB设备描述符的结构: 在USB总线识别设备阶段就将USB描述符发送给了USB总线驱动程序,设备的数据传输对象是端点,端点0是特殊端点,在USB总线驱动程序识别阶段, 会分配一个地址给U ...

  5. tomcat下发布项目,遇到的问题总结

    以前一直是在eclipse下启动tomcat,然后访问web项目.今天脑门一热,就想用tomcat的bin目录下的startup.bat来启动tomcat,虽然tomcat的启动很顺利,但是访问网页的 ...

  6. android菜鸟学习笔记20----Android数据存储(四))Android数据库操作

    Android内置了一个名为SQLite的关系型数据库,这是一款轻量型的数据库,操作十分简便.SQLite与别的数据库不同的是,它没有数据类型.可以保存任何类型的数据到你所想要保存的任何表的任何列中. ...

  7. dubbo启动报错多个资源争缓存问题

    Dubbo Failed to save registry store file, cause: Can not lock the registry cache file 目录(?)[+] 启动的Du ...

  8. SVM vs. Softmax

    http://cs231n.github.io/linear-classify/

  9. vue v-on命令

    <!-- 阻止单击事件冒泡 --> <a v-on:click.stop="doThis"></a>   <!-- 提交事件不再重载页面 ...

  10. iOS 推送跳转到相关页面

    哈哈哈 我又来窃取别人的劳动成果了 写的很好呦 http://www.jianshu.com/p/c0eb32443915