【题目链接】

点击打开链接

【算法】

观察式子 : 最小波动值 = min{|该天营业额 - 之前某天的营业额|}

= min{该天营业额 - 该天营业额的前驱,该天营业额的后继 - 该天营业额}

用Splay维护前驱和后继即可

【代码】

#include<bits/stdc++.h>
using namespace std;
#define MAXN 32767
const int INF = 2e9; int i,N,minn,x,ans; template <typename T> inline void read(T &x) {
int f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) { if (c == '-') f = -f; }
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
} template <typename T> inline void write(T x) {
if (x < ) { putchar('-'); x = -x; }
if (x > ) write(x/);
putchar(x%+'');
} template <typename T> inline void writeln(T x) {
write(x);
puts("");
} struct Splay {
int root,total;
struct Node {
int fa,size,val,son[],cnt;
} Tree[MAXN+];
inline bool get(int x) {
return Tree[Tree[x].fa].son[] == x;
}
inline void new_node(int index,int f,int x) {
Tree[index].val = x;
Tree[index].size = Tree[index].cnt = ;
Tree[index].fa = f;
Tree[index].son[] = Tree[index].son[] = ;
}
inline void update(int index) {
Tree[index].size = Tree[index].cnt;
Tree[index].size += Tree[Tree[index].son[]].size;
Tree[index].size += Tree[Tree[index].son[]].size;
}
inline void rotate(int x) {
int f = Tree[x].fa,g = Tree[f].fa,
tmpx = get(x),tmpf = get(f);
if (!f) return;
Tree[f].son[tmpx] = Tree[x].son[tmpx^];
if (Tree[x].son[tmpx^]) Tree[Tree[x].son[tmpx^]].fa = f;
Tree[x].son[tmpx^] = f;
Tree[f].fa = x;
Tree[x].fa = g;
if (g) Tree[g].son[tmpf] = x;
update(f);
update(x);
}
inline void splay(int x) {
int f;
for (f = Tree[x].fa; (f = Tree[x].fa); rotate(x))
rotate((get(x) == get(f)) ? (f) : (x));
root = x;
}
inline void Insert(int x) {
int index = root;
bool tmp;
if (!total) {
new_node(++total,,x);
root = total;
return;
}
while (true) {
if (Tree[index].val == x) {
++Tree[index].cnt;
splay(index);
return;
}
tmp = Tree[index].val < x;
if (!Tree[index].son[tmp]) {
new_node(++total,index,x);
Tree[index].son[tmp] = total;
splay(total);
return;
} else
index = Tree[index].son[tmp];
}
}
inline int query_min(int index) {
while (true) {
if (!Tree[index].son[]) return Tree[index].val;
else index = Tree[index].son[];
}
}
inline int query_max(int index) {
while (true) {
if (!Tree[index].son[]) return Tree[index].val;
else index = Tree[index].son[];
}
}
inline int pred(int x) {
int index = root;
bool tmp;
while (true) {
if (Tree[index].val == x) break;
tmp = Tree[index].val < x;
index = Tree[index].son[tmp];
}
splay(index);
if (Tree[index].cnt > ) return x;
else if (Tree[index].son[]) return query_max(Tree[index].son[]);
else return -INF;
}
inline int succ(int x) {
int index = root;
bool tmp;
while (true) {
if (Tree[index].val == x) break;
tmp = Tree[index].val < x;
index = Tree[index].son[tmp];
}
splay(index);
if (Tree[index].cnt > ) return x;
else if (Tree[index].son[]) return query_min(Tree[index].son[]);
else return INF;
}
} T; int main() { read(N);
for (i = ; i <= N; i++) {
minn = INF;
read(x);
if (i == ) { ans += x; T.Insert(x); continue; }
T.Insert(x);
if (x - T.pred(x) < minn) minn = x - T.pred(x);
if (T.succ(x) - x < minn) minn = T.succ(x) - x;
ans += minn;
} writeln(ans); return ; }

【HNOI 2002】 营业额统计的更多相关文章

  1. [HNOI 2002]营业额统计

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

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

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

  3. NOI 2002 营业额统计 (splay or fhq treap)

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

  4. bzoj 1588 [HNOI2002] 营业额统计 链表和Splay

    来自HNOI 2002营业额的统计一题,这题以前是用链表水过的,最近看见许多splay的题,赶紧张一下知识. 题目大意就是对于一个序列,输出每个元素与它之前元素的差的最小值的和.先说链表的方法吧. 大 ...

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

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

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

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

  7. CodeVS 1296 营业额统计

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

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

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

  9. 【Treap】bzoj1588-HNOI2002营业额统计

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

  10. 营业额统计(SBT)

    营业额统计(SBT) #include<cstdio> #include<cstring> #include<string> #include<cstdlib ...

随机推荐

  1. 程序防止SqlServer使用SqlServer Profiler跟踪

    思路: 1.使用默认函数(fn_trace_getinfo)查询跟踪列表: 2.调用系统存储过程(sp_trace_setstatus)修改跟踪状态. 相关Sql : declare @default ...

  2. 揭秘jbpm流程引擎内核设计思想及构架

    揭秘jbpm流程引擎内核设计思想及构架 作者 胡长城(银狐999)   1     前言 2     阅读本篇的基础准备 2.1      概念的基础 2.2      环境的基础 3     什么是 ...

  3. python遍历两个列表,若长度不等,用None填充

    zip经常会遇到截断问题,如:a = [1,2,3], b = [4,5,6,7],则zip(a,b) = [(1, 4), (2, 5), (3, 6)] 可考虑使用map: map(lambda ...

  4. Halcon导出的cpp, VC++环境配置

    方式一: 1.project ->设置(Alt+F7) -> C/C++ ->分类:预处理器 ->附加包括路径   添加:$(HALCONROOT)/include,$(HAL ...

  5. 图片异步载入之 Android-Universal-Image-Loader

    今天在做项目的时候用了之前写的图片载入类.尽管也能实现缓存什么的.可是在载入大图的时候非常慢非常慢.于是上网找解决方式,准备优化一下,无意中发现了Android-Universal-Image-Loa ...

  6. 用Perl发送邮件小例子

    据传,Perl发送邮件有很多方案,但我只会用Mail::Sender这种方式,也就只能简单谈谈这种方式. 在参考众多网页后,程序书写如下: #!/usr/bin/perl -w use Mail::S ...

  7. POJ3420 Quad Tiling DP + 矩阵高速幂

    题目大意是用1*2的骨牌堆积成4*N的矩形.一共同拥有多少种方法,N不超过10^9. 这题和以前在庞果网上做过的一道木块砌墙差点儿一样. 由于骨牌我们能够横着放.竖着放.我们如果以4为列,N为行这样去 ...

  8. Pat(Advanced Level)Practice--1018(Public Bike Management)

    Pat1018代码 题目描写叙述: There is a public bike service in Hangzhou City which provides great convenience t ...

  9. Python常用的包

    Python常用的处理数据的包和它的Tutorial(点击每个包的名称): Numpy:提供对多维数组的支持,支持矢量运算,速度快 matplotlib.pyplot:图表的绘制 Pandas:基于 ...

  10. WIN7不能被远程桌面问题

    不知从何时起,我的机器不能被远程桌面.在其他机器远程我,最后都提示"凭据不工作",账号和密码肯定是正确的. 我是开了远程桌面的: 也许是新近开了防火墙的缘故?检查防火墙,3389是 ...