zoukankan      html  css  js  c++  java
  • bzoj 1588: [HNOI2002]营业额统计

    1588: [HNOI2002]营业额统计

    Time Limit: 5 Sec  Memory Limit: 162 MB
    Submit: 17705  Solved: 7275
    [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

    /*
        本题只要想到写splay,就不难想到找前驱和后继,但记住前驱和后继的定义
        前驱(后继)定义为小于(大于)x,且最大(最小)的数 
        它不包含相等的情况,所以需要特判一下,在这个问题上wa了好多下 
    */
    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<cstdlib>
    #define maxn 40000
    using namespace std;
    int n,ans,sz,root,ch[maxn][2],key[maxn],cnt[maxn],f[maxn],size[maxn];
    int get(int x){
        return ch[f[x]][1]==x;
    }
    void update(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]];
        }
    }
    void rotate(int x){
        int old=f[x],oldf=f[f[x]],whi=get(x);
        ch[old][whi]=ch[x][whi^1];
        f[ch[old][whi]]=old;
        ch[x][whi^1]=old;f[old]=x;
        f[x]=oldf;
        if(oldf)ch[oldf][ch[oldf][1]==old]=x;
        update(old);update(x);
    }
    void splay(int x){
        for(int fa;fa=f[x];rotate(x)){
            if(f[fa])rotate(get(x)==get(fa)?fa:x);
        }
        root=x;
    }
    void insert(int x){
        if(sz==0){
            sz++;ch[root][0]=ch[root][1]=f[root]=0;
            root=sz;size[root]=cnt[root]=1;key[root]=x;return;
        }
        int now=root,fa=0;
        while(1){
            if(key[now]==x){
                cnt[now]++;update(now);update(fa);splay(now);return;
            }
            fa=now;
            now=ch[now][key[now]<x];
            if(now==0){
                sz++;ch[sz][0]=ch[sz][1]=0;
                f[sz]=fa;
                size[sz]=cnt[sz]=1;
                ch[fa][key[fa]<x]=sz;
                key[sz]=x;
                update(fa);
                splay(sz);
                return;
            }
        }
    }
    int pre(){
        int now=ch[root][0];
        while(ch[now][1])now=ch[now][1];
        return now;
    }
    int nxt(){
        int now=ch[root][1];
        while(ch[now][0])now=ch[now][0];
        return now;
    }
    int main(){
        scanf("%d",&n);
        int x;
        for(int i=1;i<=n;i++){
            scanf("%d",&x);//cout<<key[root]<<' ';
            insert(x);
            if(i==1){ans+=x;continue;}
            if(cnt[root]>1){continue;}
            else {
                int w1,w2;
                int now=pre();if(now!=0)w1=key[now];else w1=0x7fffffff;
                    now=nxt();if(now!=0)w2=key[now];else w2=0x7fffffff;
                ans+=min(abs(w1-x),abs(w2-x));
            }
        }
        printf("%d",ans);
    }
  • 相关阅读:
    微信公众号非善意访问的限制 php curl 伪造UA
    cross-origin HTTP request
    String.prototype.charCodeAt()
    event.preventDefault(); Please enter your name using lowercase letters only.
    type属性对jq-post在ie、chrome、ff的兼容
    鼠标随动悬浮框实现思路
    ThinkPHP 3.1.2 模板中的基本语法<1>
    ThinkPHP 3.1.2 模板中的基本语法<1>
    ThinkPHP 3.1.2 模板中的变量
    ThinkPHP 3.1.2 模板中的变量
  • 原文地址:https://www.cnblogs.com/thmyl/p/8065280.html
Copyright © 2011-2022 走看看