n天

n个营业额;

sum(min(abs(wi-前面)));

splay维护一下就可以

 #include<stdio.h>
#include<algorithm>
#include<math.h> using namespace std;
#define inf 2000000000
int sum,time,flag,root; struct node
{
int fa,l,r,w;
}tree[];
void rightrotate(int x) //右旋
{
int y=tree[x].fa;
int z=tree[y].fa;
tree[y].l=tree[x].r;
if(tree[x].r!=-)
tree[tree[x].r].fa=y;
tree[x].fa=z;
if(z!=-)
{
if(tree[z].l==y)
tree[z].l=x;
else
tree[z].r=x;
}
tree[x].r=y;
tree[y].fa=x;
}
void leftrotate(int x)//左旋
{
int y=tree[x].fa;
int z=tree[y].fa;
tree[y].r=tree[x].l;
if(tree[x].l!=-)
tree[tree[x].l].fa=y;
tree[x].fa=z;
if(z!=-)
{
if(tree[z].l==y)
tree[z].l=x;
else
tree[z].r=x;
}
tree[x].l=y;
tree[y].fa=x;
}
void splay(int x)
{
while(tree[x].fa!=-)
{
int y=tree[x].fa;
int z=tree[y].fa;
if(z==-) //6种旋转情况
{
if(tree[y].l==x)
rightrotate(x);
else
leftrotate(x);
}
else
{
if(tree[z].l==y&&tree[y].l==x)
{
rightrotate(y);
rightrotate(x);
}
else if(tree[z].l==y&&tree[y].r==x)
{
leftrotate(x);
rightrotate(x);
}
else if(tree[z].r==y&&tree[y].r==x)
{
leftrotate(y);
leftrotate(x);
}
else
{
rightrotate(x);
leftrotate(x);
}
}
}
root=x;
}
void BST_insert(int w,int x) //插入 就和二叉排序树一样
{
if(w==tree[x].w)
{
flag=false;
splay(x);
return ;
}
else if(w<tree[x].w)
{
if(tree[x].l==-)
{
tree[x].l=time;
tree[time].fa=x;
tree[time].l=tree[time].r=-;
tree[time].w=w;
}
else
BST_insert(w,tree[x].l);
}
else
{
if(tree[x].r==-)
{
tree[x].r=time;
tree[time].fa=x;
tree[time].l=tree[time].r=-;
tree[time].w=w;
}
else
BST_insert(w,tree[x].r);
}
}
int f_pr(int x) //前面最大
{
int y=tree[x].l;
if(y==-)
return y;
while(tree[y].r!=-)
y=tree[y].r;
return y;
}
int f_ne(int x) //后面最小
{
int y=tree[x].r;
if(y==-)
return y;
while(tree[y].l!=-)
y=tree[y].l;
return y;
}
void Insert(int w)
{
flag=true;
time++;
BST_insert(w,root);
if(flag==false)
return ;
splay(time);
int q=f_pr(time);
int p=f_ne(time);
int mi=inf;
if(q!=-)
mi=min(mi,abs(tree[q].w-w));
if(p!=-)
mi=min(mi,abs(tree[p].w-w));
sum+=mi;
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
scanf("%d",&sum);
time=;
tree[time].fa=tree[time].l=tree[time].r=-;
tree[time].w=sum;
root=time;
for(int i=;i<n;i++)
{
int a;
scanf("%d",&a);
Insert(a);
}
printf("%d\n",sum);
}
return ;
}

splay HYSBZ1588的更多相关文章

  1. HYSBZ1588 营业额统计【Splay】

    转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4366582.html   ---by 墨染之樱花 [题目链接]http://www.lydsy ...

  2. 赤裸裸的splay平衡树

    HYSBZ1588 http://www.lydsy.com/JudgeOnline/problem.php?id=1588 给我们n天的营业额, 要求出每天的最小波动值,然后加起来.  当天最小波动 ...

  3. BZOJ 1251: 序列终结者 [splay]

    1251: 序列终结者 Time Limit: 20 Sec  Memory Limit: 162 MBSubmit: 3778  Solved: 1583[Submit][Status][Discu ...

  4. [bzoj1269][AHOI2006文本编辑器editor] (splay模版题 or pb_ds [rope]大法)

    Description 这些日子,可可不和卡卡一起玩了,原来可可正废寝忘食的想做一个简单而高效的文本编辑器.你能帮助他吗?为了明确任务目标,可可对“文本编辑器”做了一个抽象的定义:   文本:由0个或 ...

  5. splay最终模板

    来自wjmzbmr的splay模板 #include<cstdio> #include<iostream> #include<algorithm> using na ...

  6. bzoj 3506 && bzoj 1552 splay

    查最小值,删除,翻转... 显然splay啊... #include<iostream> #include<cstdio> #include<algorithm> ...

  7. 【splay】文艺平衡树 BZOJ 3223

    Description 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3  ...

  8. 【填坑】bzoj3224 splay裸题

    人生第一道splay不出所料是一道裸题,一道水题,一道2k代码都不到的题 #include <cstdio> ,n,p,q; ],c[][],size[],sp[]; void rot(i ...

  9. BZOJ 1014: [JSOI2008]火星人prefix [splay 二分+hash] 【未完】

    1014: [JSOI2008]火星人prefix Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 6243  Solved: 2007[Submit] ...

随机推荐

  1. 用Access作为后台数据库支撑,书写一个用C#写入记录的案例

    具体的步骤: 1.创建并打开一个OleDbConnection对象 2.创建插入的SQL语句 3.创建一个OleDbCommand对象 4.使用OleDbCommand对象来插入数据 5.关闭OleD ...

  2. 【Unity】矩阵运算

    http://www.cnblogs.com/wywnet/p/3585075.html Vector3:  Unity3D中Vector3类定义(只写有用的):  属性:  x,y,z       ...

  3. JSP前三章错题整理

    A: B: C: D:  web-inf目录中的文件不能被客户端直接访问. 正确答案是 C 您回答的是 D 回答错误 试题分析纠错设为收藏 (选择一项) 14 A: B: C: Tomcat作为Web ...

  4. Linux常用命令学习

    1.ls命令 就是list的缩写,通过ls 命令不仅可以查看linux文件夹包含的文件,而且可以查看文件权限(包括目录.文件夹.文件权限)查看目录信息等等 常用参数搭配: ls -a 列出目录所有文 ...

  5. [No000043]学习Linux操作系统必备基础知识

    Linux操作系统概述 Q1.什么是GNU?Linux与GNU有什么关系? A: 1)GNU是GNU is Not Unix的递归缩写,是自由软件基金会(Free Software Foundatio ...

  6. linux进入软连接所指向的原目录

    软连接就是一个快捷方式,建立软连接的方法 ln -s source-path-or-file link-file 建立硬连接 ln source-path-or-file link-file linu ...

  7. Linq学习笔记(转)

    开始Linq前你要知道的 扩展方法 顾名思义就是对现有类进行扩展的的方法,扩展方法可以在不修改现有类的情况下,为现有类增加公共的接口(不是C#中的interface). 扩展方法本质上是一个静态方法, ...

  8. 正在编译转换: 未能找到元数据文件 EntityFramework.dll

    错误 1 正在编译转换: 未能找到元数据文件“C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\Tools\..\IDE\Enti ...

  9. 获取iTextSharp 的image 报错

    获取itextsharp类库的image对象的时候报错  outofmemory  .经过艰苦的测试发现jpeg类型是可行的的 iTextSharp.text.Image je = iTextShar ...

  10. 设置word里的代码格式,使之有底纹的效果

    目录 1    实现效果:    1 2    怎么才能在word里实现这样的显示?    1 如何设置word里的代码格式,使之有底纹的效果    2     实现效果: 怎么才能在word里实现这 ...