【题目链接】
【算法】
观察式子 : 最小波动值 = 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 = 1; x = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) { if (c == '-') f = -f; } for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0'; x *= f; } template <typename T> inline void write(T x) { if (x < 0) { putchar('-'); x = -x; } if (x > 9) write(x/10); putchar(x%10+'0'); } template <typename T> inline void writeln(T x) { write(x); puts(""); } struct Splay { int root,total; struct Node { int fa,size,val,son[2],cnt; } Tree[MAXN+10]; inline bool get(int x) { return Tree[Tree[x].fa].son[1] == x; } inline void new_node(int index,int f,int x) { Tree[index].val = x; Tree[index].size = Tree[index].cnt = 1; Tree[index].fa = f; Tree[index].son[0] = Tree[index].son[1] = 0; } inline void update(int index) { Tree[index].size = Tree[index].cnt; Tree[index].size += Tree[Tree[index].son[0]].size; Tree[index].size += Tree[Tree[index].son[1]].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^1]; if (Tree[x].son[tmpx^1]) Tree[Tree[x].son[tmpx^1]].fa = f; Tree[x].son[tmpx^1] = 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,0,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[0]) return Tree[index].val; else index = Tree[index].son[0]; } } inline int query_max(int index) { while (true) { if (!Tree[index].son[1]) return Tree[index].val; else index = Tree[index].son[1]; } } 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 > 1) return x; else if (Tree[index].son[0]) return query_max(Tree[index].son[0]); 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 > 1) return x; else if (Tree[index].son[1]) return query_min(Tree[index].son[1]); else return INF; } } T; int main() { read(N); for (i = 1; i <= N; i++) { minn = INF; read(x); if (i == 1) { 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 0; }