【练习】BZOJ1588 [HNOI2002]营业额统计(Splay/Set)

题意

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

题解

使用数据结构维护,保证插入和询问时间复杂度都在 n l o g n

方案一

直接使用stl中的set维护,插入使用insert,询问使用在set中二分。

方案二

splay,注意查询时的结果处理。

代码

方案一

#include<bits/stdc++.h>
using namespace std;
const int nmax = 50000;
const int INF = 0x3f3f3f3f;
int n,m;
set<int> s;
int main() {
    scanf("%d",&n);
        int ans = 0,temp;s.clear();
        for(int i = 1;i<=n;++i){
            scanf("%d",&temp);
            if(i == 1) ans += abs(temp), s.insert(temp);
            else{
                set<int> ::iterator it = s.lower_bound(temp);
                set<int> ::iterator it1 = it;
                set<int> ::iterator it2 = it;
                int t[2] = {INF,INF};
                t[0] = abs(temp - *it);
                if(it1 != s.begin()){ it1 --; t[0] = abs(temp - *it1);}
                if(it2 != s.end()){ t[1] = abs(temp - *it2); }
                ans += min(t[0],t[1]);
                s.insert(temp);
            }
        }
        printf("%d\n",ans);

    return 0;
}

方案二

#include<bits/stdc++.h>
using namespace std;
const int nmax = 1e6;
const int INF = 0x3f3f3f3f;
int ch[nmax][2],f[nmax],cnt[nmax],size[nmax],key[nmax],sz,root;
inline int get(int x){
    return ch[f[x]][1] == x;
}
inline void clear(int x){
    ch[x][0] = ch[x][1] = cnt[x] = size[x] = f[x] = key[x] = 0;
}
inline void updata(int x){
    if(x){
        size[x] += cnt[x];
        if(ch[x][0]) size[x]+=size[ch[x][0]];
        if(ch[x][1]) size[x]+=size[ch[x][1]];
    }
}
inline void rotate(int x){
    int fa = f[x], pa = f[fa], which = get(x);
    int son = ch[x][which^1];
    f[son] = fa, ch[fa][which] = son;
    f[fa] = x, ch[x][which^1] = fa;
    f[x] = pa;
    if(pa) ch[pa][ch[pa][1] == fa] = x;
    updata(fa); updata(x);
}
inline void splay(int x){
    for(int fa; fa = f[x];rotate(x))
        if(f[fa]) rotate(get(x) == get(fa)?fa:x);
    root = x;
}
inline void insert(int v){
    if(root == 0){
        sz++; ch[sz][0] = ch[sz][1] = f[sz] = 0;
        size[sz] = cnt[sz] = 1; root = sz; key[sz] = v;
        return;
    }
    int now = root,fa = 0;
    while(true){
        if(key[now] == v){ cnt[now]++; updata(now); updata(fa); splay(now); return;}
        fa = now;
        if(v < key[now]) now = ch[now][0]; else now = ch[now][1];
        if(now == 0){
            sz++; size[sz] = cnt[sz] = 1; key[sz] = v;
            ch[sz][0] = ch[sz][1] = 0;
            f[sz] = fa; ch[fa][v>key[fa]] = sz;
            updata(fa); splay(sz);
            return;
        }
    }
}
inline int pre(){
    if(cnt[root]>1) return key[root];
    if(!ch[root][0]) return INF;
    int now = ch[root][0];
    while(ch[now][1]) now = ch[now][1];
    return key[now];
}
inline int nxt(){
    if(cnt[root]>1) return key[root];
    if(!ch[root][1]) return INF;
    int now = ch[root][1];
    while(ch[now][0]) now = ch[now][0];
    return key[now];
}
int n,m;

int main() {
    scanf("%d",&n);
    int ans = 0;
    for(int i = 1;i<=n;++i){
        int temp = 0;
        scanf("%d",&temp);
        insert(temp);
        int pp = pre();
        int nn = nxt();
        if(i == 1){ ans += temp; continue;}
        ans += min(abs(temp-pp),abs(temp-nn));
    }
    printf("%d\n",ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/pengwill97/article/details/81054852