题目链接:http://www.wikioi.com/problem/1296/

算法:Splay

这是非常经典的一道题目,用Splay树来维护营业额,每天的最小波动值就等于 min{树根-树根的前驱, 树根的后继-树根)

所以用Splay来维护

PS: 本题数据有问题,所以当空行时,值为0

==========================14.06.13==========================

原来写的splay的bug太多,已换成数组= = ps:14.07.26又换成指针。。。。>_<

详细看另一篇splay文章,http://www.cnblogs.com/iwtwiioi/p/3537061.html

=================================很久以前===============================

代码:

#include <cstdio>
using namespace std;
#define F(rt) rt-> pa
#define K(rt) rt-> key
#define CH(rt, d) rt-> ch[d]
#define C(rt, d) (K(rt) > d ? 0 : 1)
#define NEW(d) new Splay(d)
#define PRE(rt) F(rt) = CH(rt, 0) = CH(rt, 1) = null int n, ans; struct Splay {
Splay* ch[2], *pa;
int key;
Splay(int d = 0) : key(d) { ch[0] = ch[1] = pa = NULL; }
}; typedef Splay* tree;
tree null = new Splay, root = null; void rot(tree& rt, int d) {
tree k = CH(rt, d^1), u = F(rt); int flag = CH(u, 1) == rt;
CH(rt, d^1) = CH(k, d); if(CH(k, d) != null) F(CH(k, d)) = rt;
CH(k, d) = rt; F(rt) = k; rt = k; F(rt) = u;
if(u != null) CH(u, flag) = k;
} void splay(tree nod, tree& rt) {
if(nod == null) return;
tree pa = F(rt);
while(F(nod) != pa) {
if(F(nod) == rt)
rot(rt, CH(rt, 0) == nod);
else {
int d = CH(F(F(nod)), 0) == F(nod);
int d2 = CH(F(nod), 0) == nod;
if(d == d2) { rot(F(F(nod)), d); rot(F(nod), d2); }
else { rot(F(nod), d2); rot(F(nod), d); }
}
}
rt = nod;
} tree maxmin(tree rt, int d) {
if(rt == null) return null;
while(CH(rt, d) != null) rt = CH(rt, d);
return rt;
} tree ps(tree rt, int d) {
if(rt == null) return null;
rt = CH(rt, d);
return maxmin(rt, d^1);
} tree search(tree& rt, int d) {
tree t = rt;
while(t != null && K(t) != d) t = CH(t, C(t, d));
splay(t, rt);
return t;
} void insert(tree& rt, int d) {
tree q = NULL, t = rt;
while(t != null) q = t, t = CH(t, C(t, d));
t = NEW(d);
PRE(t);
if(q) F(t) = q, CH(q, C(q, d)) = t;
else rt = t;
splay(t, rt);
} void del(tree& rt) {
if(rt == null) return;
tree t = rt;
if(CH(t, 0) == null) t = CH(rt, 1);
else {
t = CH(rt, 0);
splay(maxmin(t, 1), t);
CH(t, 1) = CH(rt, 1);
if(CH(rt, 1) != null) F(CH(rt, 1)) = t;
}
delete rt;
F(t) = null;
rt = t;
} void init(int key) {
if(root == null) { root = NEW(key); PRE(root); ans += key; return; }
insert(root, key);
tree succ = ps(root, 0), pred = ps(root, 1);
if(succ == null) { ans += K(pred) - K(root); splay(pred, root); return; }
if(pred == null) { ans += K(root) - K(succ); splay(succ, root); return; }
int l = K(root) - K(succ), r = K(pred) - K(root);
if(l <= r) { ans += l; splay(succ, root); }
else { ans += r; splay(pred, root); }
} int main() {
PRE(null);
scanf("%d", &n);
int c;
for(int i = 0; i < n; ++i) { if(scanf("%d", &c) == EOF) c = 0; init(c); } //坑爹的读入
printf("%d\n", ans);
return 0;
}

【wikioi】1296 营业额统计的更多相关文章

  1. Splay树-Codevs 1296 营业额统计

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

  2. CJOJ 1308 【HNOI 2002 】营业额统计 / CodeVS 1296 营业额统计(STL,二分)

    CJOJ 1308 [HNOI 2002 ]营业额统计 / CodeVS 1296 营业额统计(STL,二分) Description Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一 ...

  3. CodeVS 1296 营业额统计

    1296 营业额统计2002年  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 大师 Master   题目描述 Description Tiger最近被公司升任为营业部经理, ...

  4. treap 1296 营业额统计

    有一个点答案错误,求大神指教 #include<cstdio>#include<iostream>#include<cstdlib>#include<ctim ...

  5. codevs 1296 营业额统计 (splay 点操作)

    题目大意 每次加入一个值,并且询问之前加入的数中与该数相差最小的值. 答案输出所有相差值的总和. 解题分析 = = 参考程序 #include <bits/stdc++.h> using ...

  6. splay 1296 营业额统计

    有一个点超时,确实是个很简单的splay#include<cstdio> #include<iostream> using namespace std; int n,shu[1 ...

  7. AC日记——营业额统计 1296 codevs

    1296 营业额统计 2002年  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 大师 Master 题解  查看运行结果     题目描述 Description Tiger ...

  8. AC日记——营业额统计 codevs 1296 (splay版)

    营业额统计 思路: 每次,插入一个点: 然后找前驱后继: 来,上代码: #include <cmath> #include <cstdio> #include <iost ...

  9. BZOJ1588: [HNOI2002]营业额统计[BST]

    1588: [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 14151  Solved: 5366[Submit][Sta ...

随机推荐

  1. Linux lsof详解

    简介 lsof(list open files)是一个列出当前系统打开文件的工具.在linux环境下,任何事物都以文件的形式存在,通过文件不仅仅可以访问常规数据,还可以访问网络连接和硬件.所以如传输控 ...

  2. django inclusion_tag

    一种比较普遍的tag类型是只是渲染其它模块显示下内容,这样的类型叫做Inclusion Tag. 例如,实现以下tag: {% books_for_author author %} 渲染结果为: &l ...

  3. 25.在从1到n的正数中1出现的次数[NumberOf1Between1_N]

    [题目] 输入一个整数n,求从1到n这n个整数的十进制表示中1出现的次数.例如输入12,从1到12这些整数中包含1 的数字有1,10,11和12,1一共出现了5次. [分析] 这是一道广为流传的goo ...

  4. iOS 中使用Block时需要注意的retain circle

    现在在ios中,block是越来越多了.自己在类中定义block对象时,需要注意block对象的使用方法,防止产生retain circle,导致内存泄露. 现在分析一下产生retain circle ...

  5. Android 阅读Tasks and Back Stack文章后的重点摘抄

    这篇文章是做android的必读篇目,要仔细阅读,原文连接http://developer.android.com/guide/components/tasks-and-back-stack.html ...

  6. Heap:Sunscreen(POJ 3614)

    晒太阳 题目大意:一堆牛,为了避免晒太阳会灼烧自己,然后他们自己有自己的防晒指数(一个区间),防晒霜可以提高防晒因数SPF,大了不行小了不行,现在有一桶防晒霜,他们提供一定的SPF,但是最多可以提供k ...

  7. Ubuntu使用tcpdump工具

    Ubuntu默认是安装好了tcpdump工具的,如果没有安装的话使用sudo apt-get install tcpdump即可安装.   (如果遇到tcpdump: no suitable devi ...

  8. vm 负责虚拟机出现“”适配器 的mac地址在保留地址范围内‘’

    我自己在windows中文件中,直接将一个虚拟机进行复制了一份,后用vm打开, 选择我已经移到,结果会出现了一下的情况, 导致了我无法ping 通,故我看到一下提示: 原来是我两台虚拟机的MaC ,即 ...

  9. C++ 中 volatile 的使用

    一.作用 volatile的作用是: 作为指令关键字,确保本条指令不会因编译器的优化而省略,且要求每次直接读值. 简单地说就是防止编译器对代码进行优化.比如如下程序:XBYTE[2]=0x55;XBY ...

  10. sdut 2163:Identifiers(第二届山东省省赛原题,水题)

    Identifiers Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述  Identifier is an important c ...