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. [No00000E]PPT快捷键大全 PowerPoint2013/2010/2007/2003常用快捷

    熟练掌握PowerPoint快捷键可以让我们更快速的制作PPT模板,大大的节约时间成本.想提高工作效率吗?请熟悉PowerPoint快捷键吧!想成为高手吗?请先了解PPT快捷键吧!想制作出一个优秀的P ...

  2. 多项式FFT相关模板

    自己码了一个模板...有点辛苦...常数十分大,小心使用 #include <iostream> #include <stdio.h> #include <math.h& ...

  3. swift 集合类型(二)

    说到swift的集合类型,就不得不谈到Dictionary.包含一个键值对组合的集合. var air = ["name":"warner","tit ...

  4. android 中退出程序的两种方式

    转自:http://blog.sina.com.cn/s/blog_5da93c8f0100t76l.html 思考:如何安全的退出程序? finish是Activity的类,仅仅针对Activity ...

  5. linux ISO/IMG make

    sudo dd if=/PATH/*.ISO  of=/dev/sdb 1.制作启动U盘需要sdb,不能sdb1,否则会提示isolinux.bin文件丢失 2.TF卡,设置sdb1?忘了 /* sy ...

  6. scala 学习笔记(06) OOP(下)多重继承 及 AOP

    一.多继承 上篇trait中,已经看到了其用法十分灵活,可以借此实现类似"多重继承"的效果,语法格式为: class/trait A extends B with C with D ...

  7. python数字图像处理(17):边缘与轮廓

    在前面的python数字图像处理(10):图像简单滤波 中,我们已经讲解了很多算子用来检测边缘,其中用得最多的canny算子边缘检测. 本篇我们讲解一些其它方法来检测轮廓. 1.查找轮廓(find_c ...

  8. CareerCup All in One 题目汇总 (未完待续...)

    Chapter 1. Arrays and Strings 1.1 Unique Characters of a String 1.2 Reverse String 1.3 Permutation S ...

  9. PRML读书会第六章 Kernel Methods(核函数,线性回归的Dual Representations,高斯过程 ,Gaussian Processes)

    主讲人 网络上的尼采 (新浪微博:@Nietzsche_复杂网络机器学习) 网络上的尼采(813394698) 9:16:05 今天的主要内容:Kernel的基本知识,高斯过程.边思考边打字,有点慢, ...

  10. IDEA【 MyBatis Plugin】 插件免费完美运行

    mybatis_plus.jar 包 .Install plugin from disk...导入即能用. BaiDu云: 链接: http://pan.baidu.com/s/1geKtTbP 密码 ...