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. 刷新本地的DNS缓存

    用“WIN +R”快捷键打开运行窗口,输入“cmd”命令,进行命令行窗口.

  2. 常用SQL操作(MySQL或PostgreSQL)与相关数据库概念

    本文对常用数据库操作及相关基本概念进行总结:MySQL和PostgreSQL对SQL的支持有所不同,大部分SQL操作还是一样的. 选择要用的数据库(MySQL):use database_name; ...

  3. Leetcode: Lexicographical Numbers

    Given an integer n, return 1 - n in lexicographical order. For example, given 13, return: [1,10,11,1 ...

  4. SQL top查询

    select *from emp;

  5. hdu 4998

    http://acm.hdu.edu.cn/showproblem.php?pid=4998 这道题,在比赛的时候看了很久,才明白题目的大意.都怪自己不好好学习英语.后来经过队友翻译才懂是什么意思. ...

  6. 数据库SQL 查询

    查询 1.简单查询 select * from info(表名)   --查所有数据 select  code(列名),name(列名)  from 表名        --查指定列的数据 selec ...

  7. C#: 启动画面设计

    Windows Form经常会在启动主界面的时候预先有启动画面,这也是因为用户体验的需要,用户知道已经启动application,而不是在load resource的时候等待.因此这里不能用单线程的思 ...

  8. demo16Toast

    /Users/alamps/AndroidStudioProjects/demo16Toast/demo16Toast/src/main/res/layout/activity_main.xml &l ...

  9. JQuery书写Ajax的几种方式?

    1 $.ajax({ type: "Post", //请求方式 ("POST" 或 "GET"), 默认为 "GET" ...

  10. python入门语法总结 zz

    http://renjie120.iteye.com/blog/680126 1.python是一个解释性语言: 一个用编译性语言比如C或C++写的程序可以从源文件(即C或C++语言)转换到一个你的计 ...