[HNOI2002]营业额统计

Posted xayata

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[HNOI2002]营业额统计相关的知识,希望对你有一定的参考价值。

https://daniu.luogu.org/problemnew/show/P2234

#include <bits/stdc++.h>

const int N = 1e5 + 10;

#define gc getchar()
#define inf 0x3f3f3f3f

int fa[N], ch[N][2], cnt[N], siz[N], data[N];
int n, Ti, root, answer;

inline int read(){
    int x = 0, f = 1; char c = gc;
    while(c < 0 || c > 9) {if(c == -) f = -1; c = gc;} 
    while(c >= 0 && c <= 9) x = x * 10 + c - 0, c = gc;
    return x * f;
}

int son(int x){
    return x == ch[fa[x]][1];
}

void rotate(int x){
    int y = fa[x], z = fa[y], b = son(x), c = son(y), a = ch[x][!b];
    if(z) ch[z][c] = x; else root = x; fa[x] = z;
    if(a) fa[a] = y; ch[y][b] = a;
    ch[x][!b] = y; fa[y] = x;
}

void splay(int x, int i){
    while(fa[x] != i){
        int y = fa[x], z = fa[y];
        if(z == i) rotate(x);
        else if(son(x) == son(y)) rotate(y), rotate(x);
        else rotate(x), rotate(x);
    }
}

void ins(int rt, int v){
    int y, x = rt;
    while(1){
        y = ch[x][data[x] < v];
        if(!y){
            y = ++ n;
            root = n;
            data[y] = v;
            fa[y] = x;
            ch[x][data[x] < v] = y;
            break;
        }
        x = y;
    }
    splay(y, 0);
}

int getpre(int rt){
    int p = ch[rt][0];
    while(ch[p][1]) p = ch[p][1];
    return p;
}

int getsuc(int rt){
    int p = ch[rt][1];
    while(ch[p][0]) p = ch[p][0];
    return p;
} 

int main()
{
    Ti = read();
    ins(root, inf);
    ins(root, - inf);
    for(int i = 1; i <= Ti; i ++){
        int x = read();
        if(i == 1){
            answer += x;
            ins(root, x);
        }
        else {
            ins(root, x);
            int pre = getpre(root);
            int suc = getsuc(root);
            answer += std :: min(abs(x - data[pre]), abs(x - data[suc]));
        }
    }
    std :: cout << answer;
    return 0;
}

 

以上是关于[HNOI2002]营业额统计的主要内容,如果未能解决你的问题,请参考以下文章

BZOJ 1588: [HNOI2002]营业额统计

链表BZOJ1588: [HNOI2002]营业额统计

bzoj1588: [HNOI2002]营业额统计

bzoj1588:[HNOI2002]营业额统计

BZOJ 1588 [HNOI2002]营业额统计

[HNOI2002]营业额统计