http://www.lydsy.com/JudgeOnline/problem.php?id=1588

splay  bottom-up的数组实现。

题意就是给你一组数,求每个数与在其前面且与其最相近的数的差值的绝对值。

考虑splay二叉搜索树的特性,每新插入一个节点,比它小且最靠近它的数在是左子树中的最大值,另一半同理。

代码借鉴自:http://blog.csdn.net/ACM_cxlove?viewmode=contents

 #include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 1e5 + ;
const int inf = 0x3f3f3f3f;
int pre[maxn], key[maxn], ch[maxn][], root, size;
int n; void new_node(int &r, int father, int k){
r = ++size;
pre[r] = father;
key[r]= k;
ch[r][] = ch[r][] = ;
} void rotate(int x, bool d){// d = 1, right rotate
int y = pre[x];
ch[y][!d] = ch[x][d];
pre[ch[x][d]] = y;
if(pre[y]) ch[pre[y]][ch[pre[y]][] == y] = x;
//if y is not the root, link x with its grandparent
pre[x] = pre[y];
ch[x][d] = y;
pre[y] = x;
} void splay(int u, int dest){
//root u to root dest
while(pre[u] != dest){
if(pre[pre[u]] == dest) rotate(u, ch[pre[u]][] == u);
else{
int y = pre[u];
int d = ch[pre[y]][] == y;
if(ch[y][d] == u){//zig-zag
rotate(u, !d);
rotate(u, d);
}else{
rotate(y, d);//zig-zig
rotate(u, d);
}
}
}
if(!dest) root = u;
} int insert(int k){
int u = root;
while(ch[u][key[u] < k]){
if(key[u] == k) return splay(u, ), ;
u = ch[u][key[u] < k];
}
if(key[u] == k) return splay(u, ), ;
new_node(ch[u][key[u] < k], u, k);
splay(ch[u][key[u] < k], );
return ;
} int get_pre(int x){
int tem = ch[x][];
if(!tem) return inf;
while(ch[tem][]) tem = ch[tem][];
return key[x] - key[tem];
} int get_next(int x){
int tem = ch[x][];
if(tem == ) return inf;
while(ch[tem][]) tem = ch[tem][];
return key[tem] - key[x];
} int main(){
//freopen("in.txt", "r", stdin);
while(~scanf("%d", &n)){
root = size = ;
int ans = ;
for(int i = ; i <= n; i++){
int num;
if(scanf("%d", &num) == EOF) num = ;
if(i == ){
ans += num;
new_node(root, , num);
continue;
}
if(!insert(num)) continue;
int a = get_next(root);
int b = get_pre(root);
ans += min(a, b);
}
printf("%d\n", ans);
}
return ;
}

bzoj 1588营业额统计(HNOI 2002)的更多相关文章

  1. BZOJ 1588 营业额统计

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

  2. BZOJ 1588 营业额统计 set

    题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1588 题目大意: 营业额统计 Tiger最近被公司升任为营业部经理,他上任后接受公司交 ...

  3. (HYSBZ)BZOJ 1588 营业额统计

    营业额统计 Time Limit: 5000MS   Memory Limit: 165888KB   64bit IO Format: %lld & %llu Description 营业额 ...

  4. BZOJ 1588 营业额统计 Splay

    主要操作为Splay中插入节点,查找前驱和后继节点. 1: #include <cstdio> 2: #include <iostream> 3: #include <c ...

  5. [bzoj] 1588 营业额统计 || Splay板子题

    原题 给出一个n个数的数列ai ,对于第i个元素ai定义\(fi=min(|ai-aj|) (1<=j<i)\),f1=a1,求\(/sumfi\) Splay板子题. Splay讲解:h ...

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

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

  7. HYSBZ 1588 营业额统计

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

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

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

  9. [BZOJ 1588][HNOI 2002] 营业额统计

    这果然是在那个没有STL的年代出的题 1588: [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 16648  Solve ...

随机推荐

  1. Python xml

    第一部分:读 ######## ## # -*- coding:utf-8 -*- """ * User: not me * Date: 11-11-9 * Time: ...

  2. oracle pl sql 解锁表

    select   p.spid,a.serial#, c.object_name,b.session_id,b.oracle_username,b.os_user_name   from   v$pr ...

  3. iOS6:在你的企业系统中支持Passbook

    前言 这是一篇翻译,感谢Jonathan Tang. 原文地址:iOS 6 Tutorial: Supporting Passbook within Your Enterprise Systems 正 ...

  4. Java相对路径读取文件

    不管你是新手还是老鸟,在程序中读取资源文件总会遇到一些找不到文件的问题,这与Java底层的实现有关,不能算bug,只要方法得当,问题还是可以解决的. 项目的文件夹结构: repathtest ├─sr ...

  5. Eclipse安装Ruby插件应该注意的几点

    http://www.aptana.com/products/studio3/success_plugin.html Installing via Eclipse Please copy the fo ...

  6. 对ImageView.ScaleType的详解

    设置的方式有两种: 1.在layout.xml里面定义android:scaleType = "center" 2.在代码中调用imageview.setScaleType(Ima ...

  7. yii中sphinx,Ajax搜索分页

    效果图: 控制器: <?phpnamespace backend\controllers; use Yii;use yii\web\Controller;use yii\data\Paginat ...

  8. 变形--旋转 rotate()

    旋转rotate()函数通过指定的角度参数使元素相对原点进行旋转.它主要在二维空间内进行操作,设置一个角度值,用来指定旋转的幅度.如果这个值为正值,元素相对原点中心顺时针旋转:如果这个值为负值,元素相 ...

  9. SQL Server XML基础学习之<7>--XML modify() 方法对 XML 数据中插入、更新或删除

    /*------------------------------------------------------------------------------+ #| = : = : = : = : ...

  10. WebForm中<%=%>与<%#%>的区别?

    1<%=%>相当于Response.Write(),是输出变量的值 2<%#%>专门用于数据绑定,可以绑定一些变量或者数据源中的信息,中间绑定是数据源的条目,若想让它起作用,必 ...