[BZOJ1588]营业额统计(Splay)

Description

题意:给定 n个数,每给定一个数,在之前的数里找一个与当前数相差最小的数,求相差之和(第一个数为它本身)

如:5 1 2 5 4 6

Ans=5+|1-5|+|2-1|+|5-5|+|4-5|+|6-5|=5+4+1+0+1+1=12

n<=32767

Solution

平衡树入门题,每读入一个数,将当前数旋转至树根 ,在左子树中找一个最大以及右子树中最小的与根进行比较即可

Code

#include <cstdio>
#include <algorithm>
#define N 33333
#define Inf 0x7fffffff/2//会爆int
#define lc(x) ch[(x)][0]
using namespace std;

int n,root,tmp,fa[N],ch[N][2],k[N],ind=1,Ans;
//k[]下标节点储存的值

inline void rotate(int p){//旋转
    int q=fa[p],y=fa[q],x=(ch[q][1]==p);
    ch[q][x]=ch[p][x^1];fa[ch[q][x]]=q;
    ch[p][x^1]=q;fa[q]=p;
    fa[p]=y;
    if(y){
        if(ch[y][0]==q) ch[y][0]=p;
        else if(ch[y][1]==q) ch[y][1]=p;
    } 
}

inline void splay(int x){
    for(int y;y=fa[x];rotate(x))
        if(fa[y]) rotate((x==lc(y))==(y==lc(fa[y]))?y:x);
    root=x;
}

inline void Ins(int x,int v){//插入
    int y;
    while(1){
        y=ch[x][k[x]<v];
        if(!y){//当前节点为空即储存
            y=++ind;
            k[y]=v;
            ch[y][0]=ch[y][1]=0;
            fa[y]=x;
            ch[x][k[x]<v]=y;
            break;
        }
        x=y;
    }
    splay(y);
}

inline int Min(int x){
    int t=ch[x][0];
    while(ch[t][1]) t=ch[t][1];
    return k[t];
}

inline int Max(int x){
    int t=ch[x][1];
    while(ch[t][0]) t=ch[t][0];
    return k[t];
}

int main(){
    scanf("%d%d",&n,&tmp);
    k[root=1]=Ans=tmp;
    Ins(root,Inf),Ins(root,-Inf);//初始化
    
    for(int i=2;i<=n;++i){
        scanf("%d",&tmp);
        Ins(root,tmp);
        int mi=Min(root),mx=Max(root);
        Ans+=min(tmp-mi,mx-tmp);
    }
    printf("%d\n",Ans);
    return 0;
}

转载于:https://www.cnblogs.com/void-f/p/8377276.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值