bzoj1588 [HNOI2002]营业额统计 splay

来源:互联网 发布:安全狗软件管家 编辑:程序博客网 时间:2024/05/18 01:19

Description


营业额统计 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况。 Tiger拿出了公司的账本,账本上记录了公司成立以来每天的营业额。分析营业情况是一项相当复杂的工作。由于节假日,大减价或者是其他情况的时候,营业额会出现一定的波动,当然一定的波动是能够接受的,但是在某些时候营业额突变得很高或是很低,这就证明公司此时的经营状况出现了问题。经济管理学上定义了一种最小波动值来衡量这种情况: 该天的最小波动值 当最小波动值越大时,就说明营业情况越不稳定。 而分析整个公司的从成立到现在营业情况是否稳定,只需要把每一天的最小波动值加起来就可以了。你的任务就是编写一个程序帮助Tiger来计算这一个值。 第一天的最小波动值为第一天的营业额。

Solution


分两种情况讨论,分别找最大的小于x的和最小的大于x的取最优即可

treap调半天过不了还T了震惊,无奈打了splay,感觉各种旋转还是很有趣的

Code


#include <stdio.h>#include <string.h>#include <math.h>#include <algorithm>#define rep(i,st,ed) for (int i=st;i<=ed;++i)#define fill(x,t) memset(x,t,sizeof(x))#define min(x,y) ((x)<(y)?(x):(y))#define INF 0x3f3f3f3f#define N 160005struct treeNode{int fa,key,son[2];}t[N<<2];int root=0,cnt=0;bool flag;void read(int &x) {    x=0; int v=1; char ch=getchar();    for (;ch<'0'||ch>'9';v=(ch=='-')?(-1):(v),ch=getchar());    for (;ch<='9'&&ch>='0';x=x*10+ch-'0',ch=getchar());    x*=v;}void rrotate(int x) {    int y=t[x].fa; int z=t[y].fa;    t[y].son[0]=t[x].son[1];    if (t[x].son[1]) t[t[x].son[1]].fa=y;    t[x].fa=z;    t[x].son[1]=y; t[y].fa=x;    if (z) {        if (t[z].son[0]==y) t[z].son[0]=x;         else t[z].son[1]=x;    }}void lrotate(int x) {    int y=t[x].fa; int z=t[y].fa;    t[y].son[1]=t[x].son[0];    if (t[x].son[0]) t[t[x].son[0]].fa=y;    t[x].fa=z;    t[x].son[0]=y; t[y].fa=x;    if (z) {        if (t[z].son[0]==y) t[z].son[0]=x;        else t[z].son[1]=x;    }}void splay(int x) {    while (t[x].fa) {        int y=t[x].fa; int z=t[y].fa;        if (!z) {            if (t[y].son[0]==x) rrotate(x);            else lrotate(x);        } else {            if (t[z].son[0]==y&&t[y].son[0]==x) {rrotate(y); rrotate(x);}            else if (t[z].son[1]==y&&t[y].son[1]==x) {lrotate(y); lrotate(x);}            else if (t[z].son[0]==y&&t[y].son[1]==x) {lrotate(x); rrotate(x);}            else {rrotate(x); lrotate(x);}        }    }    root=x;}void insert(int x,int key) {    while (x) {        if (t[x].key==key) {            flag=true;            return ;        }        if (t[x].key>key) {            if (!t[x].son[0]) {t[x].son[0]=++cnt; t[t[x].son[0]].key=key; t[t[x].son[0]].fa=x; return;}            else x=t[x].son[0];        } else {            if (!t[x].son[1]) {t[x].son[1]=++cnt; t[t[x].son[1]].key=key; t[t[x].son[1]].fa=x; return;}            else x=t[x].son[1];        }    }}int query_max(int x) {    int y=t[x].son[0]; if (!y) return y;    while (t[y].son[1]) y=t[y].son[1];    return y;}int query_min(int x) {    int y=t[x].son[1]; if (!y) return y;    while (t[y].son[0]) y=t[y].son[0];    return y;}int main(void) {    int n,ans=0;    read(n); read(ans);    t[root=++cnt]=(treeNode){0,ans,{0,0}};    rep(i,2,n) {        int x; read(x);        flag=false;        insert(root,x); splay(cnt);        if (flag) continue;        int delta=INF;        int mx=query_max(cnt);        int mn=query_min(cnt);        if (mx) delta=min(delta,abs(x-t[mx].key));        if (mn) delta=min(delta,abs(t[mn].key-x));        ans+=delta;    }    printf("%d\n",ans);}
原创粉丝点击