BZOJ 1588: [HNOI2002]营业额统计

1588: [HNOI2002]营业额统计

Time Limit: 5 Sec  Memory Limit: 162 MB
Submit: 11089  Solved: 3906

Description

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

Input

第一行为正整数 ,表示该公司从成立一直到现在的天数,接下来的n行每行有一个整数(有可能有负数) ,表示第i天公司的营业额。

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


此题数据有问题,详见讨论版http://www.lydsy.com/JudgeOnline/wttl/wttl.php?pid=1588

 

Source

解题:Splay乱搞

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int INF = 0x3f3f3f3f;
 4 const int maxn = 50010;
 5 struct splayTree {
 6     int val[maxn],ch[maxn][2],fa[maxn];
 7     int root,tot;
 8     void newnode(int &x,int f,int key) {
 9         val[x = ++tot] = key;
10         fa[x] = f;
11         ch[x][0] = ch[x][1] = 0;
12     }
13     void init() {
14         root = tot = 0;
15         newnode(root,0,-INF);
16         newnode(ch[root][1],root,INF);
17     }
18     void rotate(int x,int kd) {
19         int y = fa[x];
20         fa[ch[x][kd]] = y;
21         ch[y][kd^1] = ch[x][kd];
22         fa[x] = fa[y];
23         if(fa[x]) ch[fa[y]][y == ch[fa[y]][1]] = x;
24         fa[y] = x;
25         ch[x][kd] = y;
26     }
27     void Splay(int x,int goal) {
28         while(fa[x] != goal) {
29             if(fa[fa[x]] == goal) rotate(x,x == ch[fa[x]][0]);
30             else {
31                 int y = fa[x],z = fa[y],f = (ch[z][0] == y);
32                 if(ch[y][f] == x) {
33                     rotate(x,!f);
34                     rotate(x,f);
35                 } else {
36                     rotate(y,f);
37                     rotate(x,f);
38                 }
39             }
40         }
41         if(!goal) root = x;
42     }
43     void insert(int key) {
44         int x = root;
45         while(ch[x][val[x] < key]) x = ch[x][val[x] < key];
46         newnode(ch[x][val[x] < key],x,key);
47         Splay(tot,0);
48     }
49     int precursor(int key) {
50         int x = root,ret = -INF;
51         while(x) {
52             if(val[x] <= key) {
53                 ret = max(val[x],ret);
54                 x = ch[x][1];
55             } else x = ch[x][0];
56         }
57         return ret;
58     }
59     int sucessor(int key) {
60         int x = root,ret = INF;
61         while(x) {
62             if(val[x] >= key) {
63                 ret = min(ret,val[x]);
64                 x = ch[x][0];
65             } else x = ch[x][1];
66         }
67         return ret;
68     }
69 } splay;
70 int main() {
71     int n,tmp,ret;
72     splay.init();
73     scanf("%d%d",&n,&tmp);
74     ret = tmp;
75     splay.insert(tmp);
76     for(int i = 1; i < n; ++i) {
77         if(scanf("%d",&tmp) == -1) tmp = 0;
78         ret += min(splay.sucessor(tmp) - tmp,tmp - splay.precursor(tmp));
79         splay.insert(tmp);
80     }
81     printf("%d\n",ret);
82     return 0;
83 }
View Code

 

posted @ 2015-10-09 13:50  狂徒归来  阅读(183)  评论(0编辑  收藏  举报