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. c++ learning note

    1/  int 转换成 string 格式 #include<sstream> std::stringstream ss; str::string temp; int n; ss<& ...

  2. Codeforces 549A. Face Detection[模拟]

    A. Face Detection time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  3. [No00004B]Windows 下面为Python3.5安装NoteBook

    python3.5安装NoteBook,网上搜了一下教程,几乎很多转帖,或者是2.x版本的,很少有直接可以用的.自己琢磨了一下午,终于搞定了,现在贴出来.希望大家以后转帖什么的都先测试一下,互联网时代 ...

  4. [No000044]你是否还傻到把最好的留在最后?

    想写这篇文章很久了. 因为一直觉得我们人有一个毛病,总是喜欢将最好的东西留到最后才享用,或者等最后再给别人. 但人们往往忽略了至关重要的一点,就是这个最好有一个保质期.可以说人能拥有的快乐触发点.有机 ...

  5. uva10344 23 out of 5

    Your task is to write a program that can decide whether you can nd an arithmetic expression consisti ...

  6. Android系统自带APP分析——短信app

    Android操作系统本身就是一个巨大的开源软件仓库,熟悉它既可以了解到Android系统的设计框架,也可以获得高效的应用程序编写方式.本文所分析的源码来自于Google官方的AOSP源码4.0.1_ ...

  7. grunt-contrib-uglify压缩插件的常用配置属性

    mangle:false(不混淆变量名和方法名,保留原有的名字),如果不配置,默认混淆,就是将所有方法名和变量都用a,b,c等字母替换 preserveComments : 'all'(不删除注释,还 ...

  8. JavaScript 运算符

    JavaScript 运算符 JavaScript 运算符用于赋值,比较值,执行算术运算等. JavaScript 算术运算符 算术运算符用于执行两个变量或值的运算. 赋值 y = 5, 以下表格将向 ...

  9. Java数据类型和变量

    Java中存在2种数据类型,下面我们来详解一下: 基本数据类型: 引用数据类型: 可以用一张表来记录: 基本数据类型 整型 byte:1个字节8位,取值范围为:[-128, 127],直接写值:(by ...

  10. QDir的mkdir和mkpath区别

    mkdir:上层目录不存在时,创建会失败.比如创建“c:\\test\test”,如果test不存在,那test也创建不了.目录已经存在时会返回false. mkpath:上层目录不存在也没关系,自动 ...