BZOJ 1588 营业额统计(伸展树)

Description
营业额统计 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况。 Tiger拿出了公司的账本,账本上记录了公司成立以来每天的营业额。分析营业情况是一项相当复杂的工作。由于节假日,大减价或者是其他情况的时候,营业额会出现一定的波动,当然一定的波动是能够接受的,但是在某些时候营业额突变得很高或是很低,这就证明公司此时的经营状况出现了问题。经济管理学上定义了一种最小波动值来衡量这种情况: 该天的最小波动值 当最小波动值越大时,就说明营业情况越不稳定。 而分析整个公司的从成立到现在营业情况是否稳定,只需要把每一天的最小波动值加起来就可以了。你的任务就是编写一个程序帮助Tiger来计算这一个值。 第一天的最小波动值为第一天的营业额
Input
第一行为正整数 ,表示该公司从成立一直到现在的天数,接下来的n行每行有一个整数(有可能有负数) ,表示第i天公司的营业额
Output
输出文件仅有一个正整数,即Sigma(每天最小的波动值) 。结果小于2^31
Sample Input
6
5
1
2
5
4
6
Sample Output
12
Solution
伸展树,每次插入一个节点a进伸展树后找到其前序pre后继next,累加min(a-pre,next-a)即可
Code

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
#define maxn 110000
#define INF 0x3f3f3f3f 
struct splaytree//封装伸展树
{
    //maxn:伸展树的点的个数
    int pre[maxn];//前驱
    int key[maxn];//键值
    int ch[maxn][2];//左右孩子
    int root,tot;//根节点,节点数量
    void init()
    {
        tot=root=0;
        memset(ch,0,sizeof(ch));
        memset(pre,0,sizeof(pre));
        memset(key,0,sizeof(key));
    }
    void newnode(int &r,int father,int k) //在r位置,父亲为father,新建一个值为x的新节点。
    {
        r=++tot;
        pre[r]=father;
        key[r]=k;
        ch[r][0]=ch[r][1]=0;
    }
    void rot(int x,int kind) //kind=1右旋,kind=0左旋
    {
        int y=pre[x];
        ch[y][!kind]=ch[x][kind];
        pre[ch[x][kind]]=y;
        ch[x][kind]=y;
        if(pre[y])ch[pre[y]][ch[pre[y]][1]==y]=x;
        pre[x]=pre[y];
        pre[y]=x;
    }
    void splay(int x,int goal) //把x伸展到目标位置
    {
        while(pre[x]!=goal)
        {
            if(pre[pre[x]]==goal)rot(x,ch[pre[x]][0]==x);
            else
            {
                int y=pre[x];
                int kind=ch[pre[y]][0]==y;
                if(ch[y][kind]==x)
                {
                    rot(x,!kind);
                    rot(x,kind);
                }
                else
                {
                    rot(y,kind);
                    rot(x,kind);
                }
            }
        }
        if(goal==0)root=x;//把root更新成x
    }
    int insert(int x) //插入值x
    {
        int r=root;
        while(ch[r][key[r]<x])
        {
            if(key[r]==x)
            {
                splay(r,0);
                return 0;
            }
            r=ch[r][key[r]<x];
        }
        newnode(ch[r][x>key[r]],r,x);
        splay(ch[r][x>key[r]],0);
        return 1;
    }
    int get_pre(int x) //寻找节点x的前驱的值,未找到返回INF
    {
        int r=ch[x][0];
        if(r==0)return -INF;
        while(ch[r][1])r=ch[r][1];
        return key[r];
    }
    int get_next(int x) //寻找节点x的后继的值,未找到返回INF
    {
        int r=ch[x][1];
        if(r==0)return INF;
        while(ch[r][0])r=ch[r][0];
        return key[r];
    }
}tree;
int n,a,ans;
int main()
{
    while(~scanf("%d",&n))
    {
        ans=0;
        tree.init();
        for(int i=1;i<=n;i++)
        {
            if(scanf("%d",&a)==EOF)a=0;
            if(i==1)
            {
                ans+=a;
                tree.newnode(tree.root,0,a);
                continue;
            }
            if(tree.insert(a)==0)continue;
            int pre=tree.get_pre(tree.root),next=tree.get_next(tree.root);
            ans+=min(a-pre,next-a);
        }
        printf("%d\n",ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值