BZOJ1588_营业额统计_KEY

题目传送门

分析题意可得,希望求与每个数最相近的数。

二叉搜索树的简单题,因为可能被卡成O(N),考虑平衡树。

因为Treap较简单,此处用Treap编写代码。

code:

#include <cstdio>
#include <cstdlib>
#include <algorithm>
using namespace std;

char tc()
{
    static char fl[10000000],*A=fl,*B=fl;
    return A==B&&(B=(A=fl)+fread(fl,1,10000000,stdin),A==B)?EOF:*A++;
}

int read()
{
    char c;while(c=tc(),(c<'0'||c>'9')&&c!='-');int x=0,y=1;c=='-'?y=-1:x=c-'0';
    while(c=tc(),c>='0'&&c<='9')x=x*10+c-'0';
    return x*y;
}

int x,ans;
int N,v[32768],tp[32768][2],r[32768],cnt,rt;

int rotate(int &now,int x)
{
    int k=tp[now][x];
    tp[now][x]=tp[k][x^1];
    tp[k][x^1]=now;
    now=k;
}

int pre(int o,int x)
{
    int res=2e9;
        while(o){
            res=min(res,abs(v[o]-x));
            o=tp[o][v[o]<x]; 
        }
    return res;
}

void insert(int &now,int x)
{
    if(!now){
        now=++cnt;
        v[now]=x;r[now]=rand();
        return ;
    }
    if(x==v[now])return ;
     int to=x>v[now];
    insert(tp[now][to],x);
    if(r[now]<r[tp[now][to]])rotate(now,to);
}

main()
{
//    freopen("x.txt","r",stdin);
    N=read();
        for(int i=1;i<=N;i++){
            x=read();
            if(i==1)ans+=x;
            else ans+=pre(rt,x);
            insert(rt,x);
        }
    printf("%d",ans);
    return 0;
}

 

posted @ 2017-12-27 14:59  Cptraser  阅读(147)  评论(1编辑  收藏  举报