splay HYSBZ1588
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的更多相关文章
- HYSBZ1588 营业额统计【Splay】
转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4366582.html ---by 墨染之樱花 [题目链接]http://www.lydsy ...
- 赤裸裸的splay平衡树
HYSBZ1588 http://www.lydsy.com/JudgeOnline/problem.php?id=1588 给我们n天的营业额, 要求出每天的最小波动值,然后加起来. 当天最小波动 ...
- BZOJ 1251: 序列终结者 [splay]
1251: 序列终结者 Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 3778 Solved: 1583[Submit][Status][Discu ...
- [bzoj1269][AHOI2006文本编辑器editor] (splay模版题 or pb_ds [rope]大法)
Description 这些日子,可可不和卡卡一起玩了,原来可可正废寝忘食的想做一个简单而高效的文本编辑器.你能帮助他吗?为了明确任务目标,可可对“文本编辑器”做了一个抽象的定义: 文本:由0个或 ...
- splay最终模板
来自wjmzbmr的splay模板 #include<cstdio> #include<iostream> #include<algorithm> using na ...
- bzoj 3506 && bzoj 1552 splay
查最小值,删除,翻转... 显然splay啊... #include<iostream> #include<cstdio> #include<algorithm> ...
- 【splay】文艺平衡树 BZOJ 3223
Description 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 ...
- 【填坑】bzoj3224 splay裸题
人生第一道splay不出所料是一道裸题,一道水题,一道2k代码都不到的题 #include <cstdio> ,n,p,q; ],c[][],size[],sp[]; void rot(i ...
- BZOJ 1014: [JSOI2008]火星人prefix [splay 二分+hash] 【未完】
1014: [JSOI2008]火星人prefix Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 6243 Solved: 2007[Submit] ...
随机推荐
- Codeforces Round #371 (Div. 2)E. Sonya and Problem Wihtout a Legend[DP 离散化 LIS相关]
E. Sonya and Problem Wihtout a Legend time limit per test 5 seconds memory limit per test 256 megaby ...
- 精选30个优秀的CSS技术和实例
精选30个优秀的CSS技术和实例 投递人 墙头草 发布于 2008-12-06 20:57 评论(97) 有17487人阅读 原文链接 [收藏] « » 今天,我为大家收集精选了30个使用纯CSS ...
- [多图]Windows 10 Build 10565今推送:优化界面菜单 Cortana改进
酷站网软:此前的Windows Build 10558并没有向公众发布,而是直到近日才向Fast Ring用户推送了更多功能和改进的Build 10565版.除之前版本上的加入了Messaging.E ...
- 使用SecureCRT连接AWS EC2
AWS提供的XXX.pem文件, 如果使用Ubuntu等linux系统,直接使用ssh命令即可访问AWS上的Linux-EC2实例. $ ssh -i XXX.pem ec2-user@{IP/hos ...
- Apache Rewrite 拟静态配置
1.mod_rewrite 简介和配置 Rewirte主要的功能就是实现URL的跳转和隐藏真实地址,基于Perl语言的正则表达式规范.平时帮助我们实现拟静态,拟目录,域名跳转,防止盗链等 2.mod_ ...
- mongodb分布式查询
分布式查询:mongodb的分布式模型分为replica set和sharded cluster. sharded集群中将read根据sharding key(分片键)转发到指定的shard节点,re ...
- codevs1501 二叉树最大宽度和高度
难度等级:白银 1501 二叉树最大宽度和高度 题目描述 Description 给出一个二叉树,输出它的最大宽度和高度. 输入描述 Input Description 第一行一个整数n. 下面 ...
- node基础03:使用函数
1.使用函数 //server.js var http = require("http"); var output = require("./output"); ...
- C#:DataTable映射成Model
这是数据库开发中经常遇到的问题,当然,这可以用现成的ORM框架来解决,但有些时候,如果DataSet/DataTable是第三方接口返回的,ORM就不方便了,还得自己处理. 反射自然必不可少的,另外考 ...
- opencv7-ml之svm
因为<opencv_tutorial>这部分只有两个例子,就先暂时介绍两个例子好了,在refman中ml板块有:统计模型.普通的贝叶斯分类器.KNN.SVM.决策树.boosting.随机 ...