[台州学院 2924] 营业额统计

2924: 营业额统计 分享至QQ空间 去爱问答提问或回答

Time Limit(Common/Java):1000MS/3000MS     Memory Limit:65536KByte
Total Submit: 416            Accepted:130

Description

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

该天的最小波动值 = min{ |该天以前某天的营业额 - 该天的营业额 | }

当最小波动值越大时,就说明营业情况越不稳定。而分析整个公司的从成立到现在营业情况是否稳定,只需要把每一天的最小波动值加起来就可以了。你的任务就是编写一个程序帮助Tiger来计算这一个值。第一天的最小波动值为第一天的营业额。

 

Input

测试数据多组,每组的第一行为正整数n(1 <= n <= 32767), 表示该公司从成立一直到现在的天数. 接下来的n行每行有一个整数Ai(Ai <= 1000000) , 表示第i天的营业额。处理到EOF为止。

 

Output

每组数据占一行,每行输出一个整数,每天最小波动值的和。结果小于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

也可以用STL、Treap什么的、= =

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
#define INF 0x7fffffff
#define N 40000

struct SplayTree
{
    int ch[N][2],pre[N],sz[N],val[N];
    int root,top;

    inline void Insert(int k) //按大小插入、注意插入后别忘了旋转
    {
        int r=root;
        if(r==0)
        {
            NewNode(root,0,k);
            return;
        }
        while(ch[r][val[r]<k])
        {
            r=ch[r][val[r]<k];
        }
        NewNode(ch[r][val[r]<k],r,k);
        Splay(ch[r][val[r]<k],0);
    }
    inline void Rotate(int x,int c) 
    {
        int y=pre[x];
        ch[y][!c]=ch[x][c];
        if(ch[x][c]) pre[ch[x][c]]=y;
        pre[x]=pre[y];
        if(pre[y]) ch[pre[y]][ch[pre[y]][1]==y]=x;
        ch[x][c]=y;
        pre[y]=x;
        if(y==root) root=x;
    }
    inline void Splay(int x,int f)
    { 
        while(pre[x]!=f) 
        {
            if(pre[pre[x]]==f) Rotate(x,ch[pre[x]][0]==x);
            else
            {
                int y=pre[x],z=pre[y];
                int c=(ch[z][0]==y);
                if(ch[y][c]==x) Rotate(x,!c),Rotate(x,c);
                else Rotate(y,c),Rotate(x,c);
            }
        }
        if(f==0) root=x;
    }
    inline int GetMax(int x) //找以x为根的子树的最大值
    {
        while(ch[x][1])
            x=ch[x][1];
        return val[x];
    }
    inline int GetMin(int x) //找以x为根的子树的最小值
    {
        while(ch[x][0])
            x=ch[x][0];
        return val[x];
    }
    inline void NewNode(int &x,int f,int k)
    {
        x=++top;
        pre[x]=f;
        val[x]=k;
        ch[x][0]=ch[x][1]=0;
    }
    void Init()
    {
        root=top=0;
        ch[root][0]=ch[root][1]=val[root]=pre[root]=0;
    }
}t;
int main()
{
    int n,i,ans,x;
    while(scanf("%d",&n)!=EOF)
    {
        ans=0;
        t.Init();
        for(i=1;i<=n;i++)
        {
            scanf("%d",&x);
            t.Insert(x);
            if(i==1) ans+=x;
            else
            {
                int tmp=INF;
                if(t.ch[t.root][0])
                    tmp=min(tmp,t.val[t.root]-t.GetMax(t.ch[t.root][0]));
                if(t.ch[t.root][1])
                    tmp=min(tmp,t.GetMin(t.ch[t.root][1])-t.val[t.root]);
                ans+=tmp;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

 

posted @ 2014-10-21 17:58  哈特13  阅读(143)  评论(0编辑  收藏  举报