BZOJ1588 [HNOI2002]营业额统计

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


 
正解:splay or set
解题报告:
  这道题简直就是宠物收养所的翻版(明明这道题出现得更早一些!!!)
  操作简单,直接上set
 
 1 //It is made by jump~
 2 #include <iostream>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <cstdio>
 6 #include <cmath>
 7 #include <algorithm>
 8 #include <ctime>
 9 #include <vector>
10 #include <queue>
11 #include <map>
12 #include <set>
13 #ifdef WIN32   
14 #define OT "%I64d"
15 #else
16 #define OT "%lld"
17 #endif
18 using namespace std;
19 typedef long long LL;
20 const int inf = (1<<30);
21 set<int>bst;
22 int n;
23 LL ans,now;
24 
25 inline int getint()
26 {
27        int w=0,q=0;
28        char c=getchar();
29        while((c<'0' || c>'9') && c!='-') c=getchar();
30        if (c=='-')  q=1, c=getchar();
31        while (c>='0' && c<='9') w=w*10+c-'0', c=getchar();
32        return q ? -w : w;
33 }
34 
35 inline void solve(){
36     n=getint();
37     bst.insert(inf); bst.insert(-inf);
38     int x; int t1,t2;
39     int b1,b2;
40     x=getint(); ans=x; bst.insert(x);
41     for(int i=2;i<=n;i++) {
42     x=getint(); now=0;
43     t1=*--bst.lower_bound(x);
44     t2=*bst.lower_bound(x);
45     if(t1==-inf) now=t2-x;
46     else if(t2==inf) now=x-t1;
47     else{
48         b1=x-t1; b2=t2-x;
49         if(b1<b2) now=b1;
50         else now=b2;
51     }
52     ans+=now;
53     bst.insert(x);
54     }
55     printf(OT,ans);
56 }
57 
58 int main()
59 {
60   solve();
61   return 0;
62 }

 

 
 
posted @ 2016-07-07 15:28  ljh_2000  阅读(244)  评论(0编辑  收藏  举报