BZOJ1588 [HNOI2002]营业额统计 splay模板

1588: [HNOI2002]营业额统计

Time Limit: 5 Sec  Memory Limit: 162 MB Submit: 16189  Solved: 6482 [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 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<cstdlib>
 5 #include<cmath>
 6 #include<algorithm>
 7 using namespace std;
 8 int n;
 9 int sz=0;
10 struct data
11 {
12     int s[2];
13     int val;
14     int fa;
15 }a[50000];
16 int root=0;
17 void rorate(int x,int &k)
18 {
19     int y=a[x].fa,z=a[y].fa;
20     bool l,r;
21     if(a[y].s[0]==x) l=0;else l=1;r=l^1;
22     if(y==k) k=x;
23     else
24     {
25         if(a[z].s[0]==y) a[z].s[0]=x;
26         else a[z].s[1]=x;
27     }
28     a[x].fa=z;a[y].fa=x;a[a[x].s[r]].fa=y;
29     a[y].s[l]=a[x].s[r];a[x].s[r]=y;
30 }
31 void splay(int x,int &k)
32 {
33      while(x!=k)
34      {
35          int y=a[x].fa,z=a[y].fa;
36          if(y!=k)
37          {
38              if((a[y].s[0]==x)^(a[z].s[0]==y)) rorate(x,k);
39              else rorate(y,k);
40          }
41          rorate(x,k);
42      }
43 }
44 void insert(int x,int f,int &now)
45 {
46     if(!now){now=++sz;a[now].fa=f;a[now].val=x;splay(now,root);}
47     else if(x<a[now].val) insert(x,now,a[now].s[0]);
48     else insert(x,now,a[now].s[1]);
49 }
50 int t1=1147483647,t2=-1147483647;
51 void ask_next(int now)
52 {
53     if(!now) return;
54     t1=a[now].val;
55     ask_next(a[now].s[0]);
56 }
57 void ask_before(int now)
58 {
59     if(!now) return;
60     t2=a[now].val;
61     ask_before(a[now].s[1]);
62 }
63 int main()
64 {
65     int ans=0;
66     scanf("%d",&n);
67     for(int i=1;i<=n;i++)
68     {
69         int h;
70         scanf("%d",&h);
71         t1=1147483647,t2=-1147483647;
72         insert(h,0,root);
73         ask_next(a[root].s[1]);
74         ask_before(a[root].s[0]);
75         int add=0;
76         if(i!=1) add=min(t1-h,h-t2);
77         else add=h;
78         ans+=add;
79     }
80     cout<<ans;
81 }
View Code

 

posted @ 2017-07-14 13:52  wls001  阅读(131)  评论(0编辑  收藏  举报