bzoj 1588营业额统计(HNOI 2002)
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)的更多相关文章
- BZOJ 1588 营业额统计
Description 营业额统计 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成立以来每 ...
- BZOJ 1588 营业额统计 set
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1588 题目大意: 营业额统计 Tiger最近被公司升任为营业部经理,他上任后接受公司交 ...
- (HYSBZ)BZOJ 1588 营业额统计
营业额统计 Time Limit: 5000MS Memory Limit: 165888KB 64bit IO Format: %lld & %llu Description 营业额 ...
- BZOJ 1588 营业额统计 Splay
主要操作为Splay中插入节点,查找前驱和后继节点. 1: #include <cstdio> 2: #include <iostream> 3: #include <c ...
- [bzoj] 1588 营业额统计 || Splay板子题
原题 给出一个n个数的数列ai ,对于第i个元素ai定义\(fi=min(|ai-aj|) (1<=j<i)\),f1=a1,求\(/sumfi\) Splay板子题. Splay讲解:h ...
- HYSBZ - 1588 营业额统计 (伸展树)
题意:营业额统计 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成立以来每天的营业额.分析营 ...
- HYSBZ 1588 营业额统计
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1588 题意:详见题面,中文 思路:平衡树的模板题. 可用Treap,Splay,Scape ...
- HYSBZ 1588 营业额统计 (Splay树)
题意:给出一个公司每一天的营业额,求每天的最小波动值之和.该天的最小波动值= min { 绝对值| 该天以前某一天的营业额-该天的营业额 | }.第一天的最小波动值就是其自己. 思路:Splay伸展树 ...
- [BZOJ 1588][HNOI 2002] 营业额统计
这果然是在那个没有STL的年代出的题 1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 16648 Solve ...
随机推荐
- Java基础之线程——使用Runnable接口(JumbleNames)
控制台程序. 除了定义Thread新的子类外,还可以在类中实现Runnable接口.您会发现这比从Thread类派生子类更方便,因为在实现Runnable接口时可以从不是Thread的类派生子类,并且 ...
- Caused by: org.springframework.core.NestedIOException: ASM ClassReader failed to parse class file
springframework.version 3.2.6.RELEASE jdk 1.8
- 6.理解DispatcherServlet
DispatcherServlet的作用 DispatcherServlet是前端控制器设计模式的实现,提供Spring Web MVC的集中访问点,负责职责的分派, 且与Spring IoC容器无缝 ...
- 分布式领域CAP理论
分布式领域CAP理论,Consistency(一致性), 数据一致更新,所有数据变动都是同步的Availability(可用性), 好的响应性能Partition tolerance(分区容错性) 可 ...
- MySQL 常用函数列表
一.数学函数 select SQRT (2) --取平方根select ABS (-234) --取绝对值select FLOOR (COUNT (*)/5.0) from news --取小于这个小 ...
- 【你吐吧c#每日学习】10.29 C#字符串类型&Common operators
backslash \反斜杠 escape sequence 转义字符 double quote 双引号 new line 新行字符 Bell アラート Console.WriteLine(" ...
- [原创]java WEB学习笔记65:Struts2 学习之路--Struts的CRUD操作( 查看 / 删除/ 添加) ModelDriven拦截器 paramter 拦截器
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- [原创]java WEB学习笔记57:Struts2学习之路---ActionSupport类的说明
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- Android studio删除工程项目,androidstudio
本新手最近学Android都是用的eclipse.其实个人觉得eclipse不错,可能接触Android不久,倒也不觉得它慢还是怎样.对于Google的Android studio也是早有耳闻,前两天 ...
- 深入剖析PHP输入流 php://input (转载 http://www.nowamagic.net/academy/detail/12220520)
http://www.nowamagic.net/academy/detail/12220520