【题目链接】
【算法】
观察式子 : 最小波动值 = 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 ; }