【题目链接】

点击打开链接

【算法】

观察式子 : 最小波动值 = 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. codevs——2693 上学路线(施工)

    2693 上学路线(施工)  时间限制: 2 s  空间限制: 16000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description 问题描述 你所在的城市街道好像一个 ...

  2. Codeforces 451 E Devu and Flowers

    Discription Devu wants to decorate his garden with flowers. He has purchased n boxes, where the i-th ...

  3. 微软自带的异步Ajax请求

    一.使用步骤 二.示例代码 using System; using System.Collections.Generic; using System.Linq; using System.Web; u ...

  4. JS那些事儿——Gulp的入门使用

    前言 新人使用gulp的一个记录. 首先对于第一个新事物,我会问gulp这是什么? 答:gulp是一个自动化构建工具,它可以做一些自动化的任务,比如: 检查Javascript 编译Sass(或Les ...

  5. 优秀的数据序列和还原类----TSimpleMsgPack

    优秀的数据序列和还原类----TSimpleMsgPack TSimpleMsgPack是D10天地弦的作品. 优点:至简,就一个单元文件实现,不需要引用其他单元. 缺点:不是标准的MSGPACK实现 ...

  6. String、StringBuffer和StringBuilder的差别

    String.StringBuffer和StringBuilder的差别 1.String类是不可变类,即一旦一个String对象被创建后.包括这个对象中的字符序列是不可改变的 2.在字符串拼接的过程 ...

  7. 空暇时候思考2(&#39;\0&#39;等价于数字0还是字符0)

    /********************************************************************** * * Copyright (c)2015,WK Stu ...

  8. python实现QQ机器人(自己主动登录,获取群消息,发送群消息)

    一次偶然的机会我看见了一个群里的一个QQ号总是依据你所发的消息自己主动回复,当时非常感觉到奇妙.我知道能够模拟登录站点,没想到居然也能模拟登录QQ,首先自己想到的就是怎样实现模拟登录PC端的QQ, 開 ...

  9. 2.6.2 用NPOI操作EXCEL--设置密码才可以修改单元格内容

    2.6.2 用NPOI操作EXCEL--设置密码       有时,我们可能需要某些单元格只读,如在做模板时,模板中的数据是不能随意让别人改的.在Excel中,可以通过“审阅->保护工作表”来完 ...

  10. 基于canvas和Web Audio的音频播放器

    wavesurfer.js是一款基于HTML5 canvas和Web Audio的音频播放器插件.通过wavesurfer.js你能够使用它来制作各种HTML5音频播放器,它能够在各种支持 Web A ...