算法竞赛进阶指南,238页,平衡树
本题要点:
1、该天的最小波动值=min{|该天以前某一天的营业额-该天营业额|}。
每天的最小波动值, 实际就是改天的营业额 cur 和之前的所有营业额一起排序,计算出 cur 与相邻两个营业额
之间的差值的较小值,就是 该天的最小波动值
2、平衡树容易满足,每天的营业额 income 插入到这颗 平衡树中,计算出 income 的前驱 pre 和后继 next,
该天的最小波动值 = min( abs(income - pre), abs(income, next))
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cstdlib>
using namespace std;
const int MaxN = 33000;
int tot, root, n, INF = 0x3f3f3f3f;
struct Treap
{
int l, r;
int val, dat;
}a[MaxN];
int New(int val)
{
a[++tot].val = val;
a[tot].dat = rand();
return tot;
}
void Build()
{
New(-INF), New(INF);
root = 1, a[1].r = 2;
}
void zig(int &p) //右旋转
{
int q = a[p].l;
a[p].l = a[q