BZOJ 1588 [HNOI2002]营业额统计(Splay)

思路:

算是很裸的Splay了。

我们知道 Splay 是一个排序二叉树。

结点 左子树 一定都小于 当前结点。

右子树一定都大于当前结点。

那么我们插入一个值后。

找前驱和后继 取个最小值不就ok了么。


有几个注意的。 

当插入值不成功时,  最小值肯定是0 不用算的, 算的话 会出错的, 因为这个值并没有加进去。

另外, 当是第一个数时, 直接加这个数的绝对值即可。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
const int maxn = 32767 + 10;
int val[maxn];
int cnt[maxn];
int ch[maxn][2];
int pre[maxn];
int tot, n;
int root;
const int inf = 0x3f3f3f3f;


map<int,int>ff;
//int ff[1000000 + 10];
void pushup(int o){
    if (o == 0) return;
    int lson = ch[o][0];
    int rson = ch[o][1];
    cnt[o] = cnt[lson] + cnt[rson];
}

void pushdown(int o){};

void Rotate(int x,int kind){
    int y = pre[x];
    ch[y][!kind] = ch[x][kind];
    pre[ch[x][kind] ] = y;

    if (pre[y]){
        int p = ch[pre[y] ][1] == y;
        ch[pre[y] ][p] = x;
    }
    pre[x] = pre[y];
    ch[x][kind] = y;
    pre[y] = x;
    pushup(y);
}


void Splay(int x,int f){
    pushdown(x);
    while(pre[x] != f){
        if (pre[pre[x] ] == f){
            int kind = ch[pre[x] ][0] == x;
            Rotate(x, kind);
        }
        else {

            int y = pre[x];
            int kind = ch[pre[y] ][0] == y;
            if (ch[y][kind] == x){
                Rotate(x, !kind);
                Rotate(x, kind);
            }
            else {

                Rotate(y, kind);
                Rotate(x, kind);
            }
        }
    }
    pushup(x);
    if (f == 0) root = x;
}

int get_next(int x){
    if (x == 0) return -1;
    x = ch[x][1];
    while(ch[x][0] != 0) x = ch[x][0];
    return val[x];
}
int get_pre(int x){
    if (x == 0) return -1;
    x = ch[x][0];
    while(ch[x][1] != 0) x = ch[x][1];
    return val[x];
}


int newnode(int& r, int f,int k){
    r = ++tot;
    val[r] = k;
    ff[k] = r;
    pre[r] = f;
    cnt[r] = 1;
    ch[r][0] = ch[r][1] = 0;
}
bool insert(int x){
    int cur = root;
    while(ch[cur][val[cur] < x ]){
        pushdown(cur);
        if (val[cur ] == x){
            Splay(cur, 0);
            return false;
        }
        cur = ch[cur ][val[cur] < x ];
    }
    newnode(ch[cur][val[cur] < x ], cur, x);
    Splay(ch[cur][val[cur] <  x ], 0);
    return true;
}
void init(){
    newnode(root, 0, -inf);
    newnode(ch[root][1], root, inf);
}
int aaa(int x){if (x < 0) return -x;return x;}


int main(){
    int ans = 0;
    tot = 0;
    scanf("%d",&n);
    init();
    int x;
    for (int i = 0; i < n; ++i){
        scanf("%d",&x);
        bool ok = insert(x);
        if(i == 0) ans += aaa(x);
        else if (ok)ans += min( aaa( get_next(ff[x]) - x ) , aaa( get_pre(ff[x]) - x ) );
    }


    printf("%d\n", ans);
    return 0;
}


1588: [HNOI2002]营业额统计

Time Limit: 5 Sec   Memory Limit: 162 MB
Submit: 16186   Solved: 6480
[ Submit][ Status][ Discuss]

Description

营业额统计 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况。 Tiger拿出了公司的账本,账本上记录了公司成立以来每天的营业额。分析营业情况是一项相当复杂的工作。由于节假日,大减价或者是其他情况的时候,营业额会出现一定的波动,当然一定的波动是能够接受的,但是在某些时候营业额突变得很高或是很低,这就证明公司此时的经营状况出现了问题。经济管理学上定义了一种最小波动值来衡量这种情况: 该天的最小波动值 当最小波动值越大时,就说明营业情况越不稳定。 而分析整个公司的从成立到现在营业情况是否稳定,只需要把每一天的最小波动值加起来就可以了。你的任务就是编写一个程序帮助Tiger来计算这一个值。 第一天的最小波动值为第一天的营业额。  输入输出要求

Input

第一行为正整数 ,表示该公司从成立一直到现在的天数,接下来的n行每行有一个整数(有可能有负数) ,表示第i
天公司的营业额。
天数n<=32767,
每天的营业额ai <= 1,000,000。
最后结果T<=2^31

Output

输出文件仅有一个正整数,即Sigma(每天最小的波动值) 。结果小于2^31 。

Sample Input

6
5
1
2
5
4
6

Sample Output

12

HINT

结果说明:5+|1-5|+|2-1|+|5-5|+|4-5|+|6-5|=5+4+1+0+1+1=12


该题数据bug已修复.----2016.5.15

Source

[ Submit][ Status][ Discuss]


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值