【题目链接】

点击打开链接

【算法】

观察式子 : 最小波动值 = 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 ; }

最新文章

  1. Core Java 总结(字符和字符串类问题)
  2. iOS - 对UIColor颜色反差
  3. Daily Scrum02 12.15
  4. ios 抓取真机的网络包
  5. PDA项目介绍
  6. C# 多线程线程池( 一 )
  7. C++内存分配及变长数组的动态分配
  8. 控制非模态弹出框(showModelessDialog)唯一且随父页面关闭
  9. 执行JDBC的executeUpdate()方法时,报错:数据类型不一致,应为number,但却为binary
  10. 使用NPOI导入导出标准的Excel
  11. Web开发入门疑问收集(不定期更新)
  12. SWIFT 闭包的简单使用二
  13. javascript进击(四)HTML DOM
  14. springmvc返回枚举属性值
  15. mybatis返回int类型报null
  16. LeetCode 380. Insert Delete GetRandom O(1) (插入删除和获得随机数 常数时间)
  17. ML技术 - 特征选择
  18. common lisp里的几个操作符
  19. 线程中为控件赋值Winform
  20. 【转】kafka集群搭建

热门文章

  1. CPU 内存 硬盘的区别
  2. Linux温习(三)Linux文件和文件夹管理
  3. iphone 消息推送 实现
  4. css设置背景图片自适应
  5. MVC上传文件并模拟进度条
  6. 【转载】C#之C#、.NET Framework、CLR的关系
  7. LeetCode232 Implement Queue using Stacks Java 题解
  8. struts2_13_OGNL表达式
  9. 通过/proc/cpuinfo判断CPU数量、Multicores、Multithreading、Hyper-threading
  10. linux led子系统(一)