bzoj1588: [HNOI2002]营业额统计

地址:http://www.lydsy.com/JudgeOnline/problem.php?id=1588

题目:

1588: [HNOI2002]营业额统计

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

  1 /**************************************************************
  2     Problem: 1588
  3     User: weeping
  4     Language: C++
  5     Result: Accepted
  6     Time:196 ms
  7     Memory:4028 kb
  8 ****************************************************************/
  9  
 10 #include <bits/stdc++.h>
 11  
 12 using namespace std;
 13  
 14 #define lc ch[x][0]
 15 #define rc ch[x][1]
 16  
 17 struct SplayTree
 18 {
 19  
 20     const static int maxn = 1e5 + 15;
 21  
 22     int tot,root,ch[maxn][2], key[maxn], val[maxn], sz[maxn], rev[maxn], fa[maxn];
 23  
 24     inline void init( int x, int ky, int v = 0, int par = 0 )
 25     {
 26         lc=rc=0, fa[x]= par, key[x] = ky, val[x] = v, sz[x] = 1, rev[x] = 0;
 27     }
 28  
 29     inline void init()
 30     {
 31         init( 0, 0, 0 );
 32         sz[0] = 0;
 33         tot = root = 0 ;
 34     }
 35  
 36     inline void push_up(int x)
 37     {
 38         sz[x] = sz[lc] + sz[rc] + 1;
 39     }
 40  
 41     inline void reverse(int x)
 42     {
 43         rev[x] ^= 1, swap( lc, rc);
 44     }
 45  
 46     inline void push_down(int x)
 47     {
 48         if(rev[x])
 49         {
 50             if(lc)  reverse(lc);
 51             if(rc)  reverse(rc);
 52             rev[x] = 0;
 53         }
 54     }
 55  
 56     void rotate( int x)
 57     {
 58         int f = fa[x], gf = fa[f];
 59         int t1 = (ch[f][1] == x), t2 = (ch[gf][1] == f);
 60         if( gf ) ch[gf][t2] = x;
 61         fa[x] = gf, ch[f][t1] = ch[x][1^t1], fa[ch[f][t1]] = f;
 62         ch[x][t1^1] = f, fa[f] = x;
 63         push_up( f ), push_up( x );
 64     }
 65  
 66     void splay( int x, int tar )
 67     {
 68         for(int f = fa[x], gf = fa[f]; f != tar; rotate(x), f = fa[x], gf = fa[f])
 69         if(gf != tar)
 70             rotate( ((ch[f][1] == x) == (ch[gf][1] == f) )? f: x);
 71         if( !tar ) root = x;
 72     }
 73  
 74     void insert( int ky, int v)
 75     {
 76         int x = root, ls = root;
 77         while(x)
 78         {
 79             push_down(x);
 80             sz[x] ++, ls = x;
 81             x = ch[x][ky > key[x]];
 82         }
 83         init( ++tot, ky, v, ls);
 84         ch[ls][ky > key[ls]] = tot;
 85         splay( tot, 0);
 86     }
 87  
 88     int find( int ky)
 89     {
 90         int x = root;
 91         while(x)
 92         {
 93             push_down(x);
 94             if(key[x] == ky) break;
 95             x = ch[x][ky > key[x]];
 96         }
 97         if(x)   splay(x,0);
 98         else x = -1;
 99         return x;
100     }
101  
102     // Delete Root
103     void Delete()
104     {
105         if( !ch[root][0] )
106         {
107             fa[ ch[root][1] ] = 0 ;
108             root = ch[root][1];
109         }
110         else
111         {
112             int cur = ch[root][0];
113             while( ch[cur][1] ) cur = ch[cur][1];
114             splay( cur, root );
115             ch[cur][1] = ch[root][1];
116             root = cur, fa[cur] = 0, fa[ch[root][1]] = root;
117             push_up( root );
118         }
119     }
120  
121     int kth( int k)
122     {
123         int x = root;
124         if(sz[x] < k) return -1;
125         while(x)
126         {
127             push_down(x);
128             if(k == sz[lc] + 1) break;
129             if(k > sz[lc])
130                 k -= sz[lc] + 1, x = rc;
131             else
132                 x = lc;
133         }
134         if(x)   splay(x,0);
135         else x = -1;
136         return x;
137     }
138  
139     int pred( void)
140     {
141         int x = root;
142         if(!x || !lc)   return -1;
143         x = lc;
144         while(rc)    push_down(x), x = rc;
145         //splay( x, 0);
146         return x;
147     }
148  
149     int succ( void)
150     {
151         int x = root;
152         if(!x || !rc) return -1;
153         x = rc;
154         while(lc)   push_down(x), x = lc;
155         //splay( x, 0);
156         return x;
157     }
158  
159     void debug( int x )
160     {
161         if( !x ) return;
162         if(lc) debug( lc );
163         printf("%d ", key[x] );
164         if(rc) debug( rc );
165     }
166  
167     int go(int x)
168     {
169         insert(x,0);
170         int ans = 0x3f3f3f3f, ta = pred(), tb = succ();
171         if(ta!=-1)
172             ans = abs(key[ta] - x);
173         if(tb!=-1)
174             ans = min( ans, abs(key[tb] - x));
175         //printf("==%d\n",ans);
176         return ans;
177     }
178 } sp;
179  
180 int main(void)
181 {
182     sp.init();
183     int n,ls,ans=0;
184     scanf("%d%d",&n,&ls);
185     sp.insert(ls,0);
186     ans=ls;
187     for(int i=1;i<n;i++)
188         scanf("%d",&ls),ans+=sp.go(ls);
189     printf("%d\n",ans);
190     return 0;
191 }

 

/**************************************************************
    Problem: 1588
    User: weeping
    Language: C++
    Result: Accepted
    Time:196 ms
    Memory:4028 kb
****************************************************************/
 
#include <bits/stdc++.h>
 
using namespace std;
 
#define lc ch[x][0]
#define rc ch[x][1]
 
struct SplayTree
{
 
    const static int maxn = 1e5 + 15;
 
    int tot,root,ch[maxn][2], key[maxn], val[maxn], sz[maxn], rev[maxn], fa[maxn];
 
    inline void init( int x, int ky, int v = 0, int par = 0 )
    {
        lc=rc=0, fa[x]= par, key[x] = ky, val[x] = v, sz[x] = 1, rev[x] = 0;
    }
 
    inline void init()
    {
        init( 0, 0, 0 );
        sz[0] = 0;
        tot = root = 0 ;
    }
 
    inline void push_up(int x)
    {
        sz[x] = sz[lc] + sz[rc] + 1;
    }
 
    inline void reverse(int x)
    {
        rev[x] ^= 1, swap( lc, rc);
    }
 
    inline void push_down(int x)
    {
        if(rev[x])
        {
            if(lc)  reverse(lc);
            if(rc)  reverse(rc);
            rev[x] = 0;
        }
    }
 
    void rotate( int x)
    {
        int f = fa[x], gf = fa[f];
        int t1 = (ch[f][1] == x), t2 = (ch[gf][1] == f);
        if( gf ) ch[gf][t2] = x;
        fa[x] = gf, ch[f][t1] = ch[x][1^t1], fa[ch[f][t1]] = f;
        ch[x][t1^1] = f, fa[f] = x;
        push_up( f ), push_up( x );
    }
 
    void splay( int x, int tar )
    {
        for(int f = fa[x], gf = fa[f]; f != tar; rotate(x), f = fa[x], gf = fa[f])
        if(gf != tar)
            rotate( ((ch[f][1] == x) == (ch[gf][1] == f) )? f: x);
        if( !tar ) root = x;
    }
 
    void insert( int ky, int v)
    {
        int x = root, ls = root;
        while(x)
        {
            push_down(x);
            sz[x] ++, ls = x;
            x = ch[x][ky > key[x]];
        }
        init( ++tot, ky, v, ls);
        ch[ls][ky > key[ls]] = tot;
        splay( tot, 0);
    }
 
    int find( int ky)
    {
        int x = root;
        while(x)
        {
            push_down(x);
            if(key[x] == ky) break;
            x = ch[x][ky > key[x]];
        }
        if(x)   splay(x,0);
        else x = -1;
        return x;
    }
 
    // Delete Root
    void Delete()
    {
        if( !ch[root][0] )
        {
            fa[ ch[root][1] ] = 0 ;
            root = ch[root][1];
        }
        else
        {
            int cur = ch[root][0];
            while( ch[cur][1] ) cur = ch[cur][1];
            splay( cur, root );
            ch[cur][1] = ch[root][1];
            root = cur, fa[cur] = 0, fa[ch[root][1]] = root;
            push_up( root );
        }
    }
 
    int kth( int k)
    {
        int x = root;
        if(sz[x] < k) return -1;
        while(x)
        {
            push_down(x);
            if(k == sz[lc] + 1) break;
            if(k > sz[lc])
                k -= sz[lc] + 1, x = rc;
            else
                x = lc;
        }
        if(x)   splay(x,0);
        else x = -1;
        return x;
    }
 
    int pred( void)
    {
        int x = root;
        if(!x || !lc)   return -1;
        x = lc;
        while(rc)    push_down(x), x = rc;
        //splay( x, 0);
        return x;
    }
 
    int succ( void)
    {
        int x = root;
        if(!x || !rc) return -1;
        x = rc;
        while(lc)   push_down(x), x = lc;
        //splay( x, 0);
        return x;
    }
 
    void debug( int x )
    {
        if( !x ) return;
        if(lc) debug( lc );
        printf("%d ", key[x] );
        if(rc) debug( rc );
    }
 
    int go(int x)
    {
        insert(x,0);
        int ans = 0x3f3f3f3f, ta = pred(), tb = succ();
        if(ta!=-1)
            ans = abs(key[ta] - x);
        if(tb!=-1)
            ans = min( ans, abs(key[tb] - x));
        //printf("==%d\n",ans);
        return ans;
    }
} sp;
 
int main(void)
{
    sp.init();
    int n,ls,ans=0;
    scanf("%d%d",&n,&ls);
    sp.insert(ls,0);
    ans=ls;
    for(int i=1;i<n;i++)
        scanf("%d",&ls),ans+=sp.go(ls);
    printf("%d\n",ans);
    return 0;
}
posted @ 2017-10-13 17:01  weeping  阅读(144)  评论(0编辑  收藏  举报