BZOJ_1588_&_Codevs_1296_[HNOI2002]_营业额统计(平衡树/set)

描述


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

给出每一天的营业值,求出之前的天当中与它相差最小的营业值与它的差的绝对值(第一天的差值为他本身),求和.

1588: [HNOI2002]营业额统计

Time Limit: 5 Sec  Memory Limit: 162 MB
Submit: 12954  Solved: 4732
[Submit][Status][Discuss]

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


该题数据bug已修复.----2016.5.15

Source

分析


比宠物收养所更裸,果然十几年前的题都是水吗...然而我只会做水题.

注意:

1.第一次写Splay的时候重复的元素直接跳过导致一直T,后来就先把这个值伸展上来再跳过,就过了,果然这种均摊的复杂度不是可以瞎玩的...

另外,不判重似乎慢不了多少...

2.set里写成min(*it-x,x-*(--it))会出错,可能是min函数理由什么奥妙?it多减了几次?...

3.如果set里不先加入INF和-INF的话,每次需要判断*it是不是st.end()(最后一个的后面),st.begin()(第一个).

Treap:

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <algorithm>
 4 using namespace std;
 5 
 6 const int oo=~0u>>1;
 7 
 8 struct Treap{
 9     struct node{
10         node* ch[2];
11         int v,r;
12         node(int v,node* t):v(v){ ch[0]=ch[1]=t; r=rand(); }
13     }* root,* null;
14     Treap(){
15         null=new node(0,NULL); null->r=oo;
16         root=null;
17     }
18     void rot(node* &o,bool d){
19         node* k=o->ch[!d]; o->ch[!d]=k->ch[d]; k->ch[d]=o;
20         o=k;
21     }
22     void insert(node* &o,int x){
23         if(o==null) o=new node(x,null);
24         else{
25             if(x==o->v) return;
26             bool d=x>o->v;
27             insert(o->ch[d],x);
28             if(o->ch[d]->r<o->r) rot(o,!d);
29         }
30     }
31     int pre(node* o,int x){
32         if(o==null) return -oo;
33         if(o->v<=x) return max(pre(o->ch[1],x),o->v);
34         return pre(o->ch[0],x);
35     }
36     int suc(node* o,int x){
37         if(o==null) return oo;
38         if(o->v>=x) return min(suc(o->ch[0],x),o->v);
39         return suc(o->ch[1],x);
40     }
41 }tree;
42 
43 int n,a,ans;
44 
45 int main(){
46     scanf("%d",&n);
47     scanf("%d",&a);
48     ans+=a; tree.insert(tree.root,a); n--;
49     while(n--){
50         if(scanf("%d",&a)==EOF) a=0;
51         int pre=tree.pre(tree.root,a);
52         int suc=tree.suc(tree.root,a);
53         if(pre==-oo) ans+=suc-a;
54         else if(suc==oo) ans+=a-pre;
55         else ans+=min(suc-a,a-pre);
56         tree.insert(tree.root,a);
57     }
58     printf("%d
",ans);
59     return 0;
60 }
61 
62 Treap
View Code

Splay:

 1 #include <cstdio>
 2 #include <algorithm>
 3 using namespace std;
 4 
 5 const int oo=~0u>>1;
 6 
 7 struct Splay{
 8     struct node{
 9         node* ch[2],*pa;
10         int v;
11         node(int v,node* t):v(v){ ch[0]=ch[1]=pa=t; }
12         bool d(){ return pa->ch[1]==this; }
13         void setc(node* t,bool d) { ch[d]=t; t->pa=this; }
14     }*root,*null;
15     Splay(){
16         null=new node(0,NULL);
17         root=null;
18     }
19     void rot(node* o){
20         node* pa=o->pa; bool d=o->d();
21         pa->pa->setc(o,pa->d());
22         pa->setc(o->ch[!d],d);
23         o->setc(pa,!d);
24         if(pa==root) root=o;
25     }
26     void splay(node* o,node* pa){
27         while(o->pa!=pa){
28             if(o->pa->pa==pa) rot(o);
29             else o->d()==o->pa->d()?(rot(o->pa),rot(o)):(rot(o),rot(o));
30         }
31     }
32     void insert(int x){
33         if(root==null){ root=new node(x,null); return; }
34         node* t=root;
35         if(t->v==x) return;
36         while(t->ch[x>t->v]!=null&&t->ch[x>t->v]->v!=x) t=t->ch[x>t->v];
37         if(t->ch[x>t->v]!=null){ splay(t->ch[x>t->v],null); return; }
38         node* k=new node(x,null);
39         t->setc(k,x>t->v);
40         splay(k,null);
41     }
42     int pre(int x){
43         node* t=root;
44         int ret=-oo;
45         while(t!=null){
46             if(t->v==x) return x;
47             if(t->v<x) ret=t->v,t=t->ch[1];
48             else t=t->ch[0];
49         }
50         return ret;
51     }
52     int suc(int x){
53         node* t=root;
54         int ret=-oo;
55         while(t!=null){
56             if(t->v==x) return x;
57             if(t->v>x) ret=t->v,t=t->ch[0];
58             else t=t->ch[1];
59         }
60         return ret;
61     }
62 }tree;
63 
64 int n,a,ans;
65 
66 int main(){
67     scanf("%d",&n);
68     scanf("%d",&a);
69     ans+=a; tree.insert(a); n--;
70     while(n--){
71         if(scanf("%d",&a)==EOF) a=0;
72         int pre=tree.pre(a);
73         int suc=tree.suc(a);
74         if(pre==-oo) ans+=suc-a;
75         else if(suc==-oo) ans+=a-pre;
76         else ans+=min(suc-a,a-pre);
77         tree.insert(a);
78     }
79     printf("%d
",ans);
80     return 0;
81 }
View Code

 set:
 

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 const int INF=0x3fffffff;
 5 int n,x,ans;
 6 set <int> st;
 7 int main(){
 8     scanf("%d",&n);
 9     st.insert(INF); st.insert(-INF);
10     scanf("%d",&x); ans=x; st.insert(x);
11     set <int> :: iterator it;
12     for(int i=1;i<n;i++){
13         scanf("%d",&x);
14         it=st.lower_bound(x);
15         int a=*it-x,b=x-*(--it);
16         int ret=min(a,b);
17         if(ret) ans+=ret, st.insert(x);
18     }
19     printf("%d
",ans);
20     return 0;
21 }
View Code
原文地址:https://www.cnblogs.com/Sunnie69/p/5490350.html