zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    MTGA天梯利用Ummored Ego进行针对核心卡列表
    三日狂欢_THDN_简介
    Unity_Dungeonize 随机生成迷宫
    Match3 Module For Game(THDN)
    UNITY->(width*height)style Inventory
    Mysql基本配置以及常见问题
    C++||变量
    c++||OOP
    c++||template
    实用的js函数
  • 原文地址:https://www.cnblogs.com/Cptraser/p/8125777.html
Copyright © 2011-2022 走看看