Splay 学习

F.A.QsHomeProblemSetStatusRanklistContestLoginRegister捐赠本站
Notice:开心刷题:)

1588: [HNOI2002]营业额统计

Time Limit: 5 Sec  Memory Limit: 162 MB
Submit: 8877  Solved: 2949
[Submit][Status]

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

Source

 
[Submit][Status]


HOME Back

Splay 模板题,对于每天的营业额只需要将其插入到bst中,找其前驱和后继,把和他们和营业额差值绝对值较小的加到答案中即可,也可以用线段树来搞。

模板套的cxlove巨巨的。。。

  1 #include<iostream>
  2 #include<cstring>
  3 #include<cstdio>
  4 #include<algorithm>
  5 #define N 100005
  6 #define inf 1<<29
  7 using namespace std;
  8 int pre[N],key[N],ch[N][2],root,tot1;  //分别表示父结点,键值,左右孩子(0为左孩子,1为右孩子),根结点,结点数量
  9 int n;
 10 //新建一个结点
 11 void NewNode(int &r,int father,int k){
 12     r=++tot1;
 13     pre[r]=father;
 14     key[r]=k;
 15     ch[r][0]=ch[r][1]=0;  //左右孩子为空
 16 }
 17 //旋转,kind为1为右旋,kind为0为左旋
 18 void Rotate(int x,int kind){
 19     int y=pre[x];
 20     //类似SBT,要把其中一个分支先给父节点
 21     ch[y][!kind]=ch[x][kind]; 
 22     pre[ch[x][kind]]=y;
 23     //如果父节点不是根结点,则要和父节点的父节点连接起来
 24     if(pre[y])
 25         ch[pre[y]][ch[pre[y]][1]==y]=x;
 26     pre[x]=pre[y];
 27     ch[x][kind]=y;
 28     pre[y]=x;
 29 }
 30 //Splay调整,将根为r的子树调整为goal
 31 void Splay(int r,int goal){
 32     while(pre[r]!=goal){
 33         //父节点即是目标位置,goal为0表示,父节点就是根结点
 34         if(pre[pre[r]]==goal)
 35             Rotate(r,ch[pre[r]][0]==r);
 36         else{
 37             int y=pre[r];
 38             int kind=ch[pre[y]][0]==y;
 39             //两个方向不同,则先左旋再右旋
 40             if(ch[y][kind]==r){
 41                 Rotate(r,!kind);
 42                 Rotate(r,kind);
 43             }
 44             //两个方向相同,相同方向连续两次
 45             else{
 46                 Rotate(y,kind);
 47                 Rotate(r,kind);
 48             }
 49         }
 50     }
 51     //更新根结点
 52     if(goal==0) root=r;
 53 }
 54 int Insert(int k){
 55     int r=root;
 56     while(ch[r][key[r]<k]){
 57         //不重复插入
 58         if(key[r]==k){
 59             Splay(r,0);
 60             return 0;
 61         }
 62         r=ch[r][key[r]<k];
 63     }
 64     NewNode(ch[r][k>key[r]],r,k);
 65     //将新插入的结点更新至根结点
 66     Splay(ch[r][k>key[r]],0);
 67     return 1;
 68 }
 69 //找前驱,即左子树的最右结点
 70 int get_pre(int x){
 71     int tmp=ch[x][0];
 72     if(tmp==0)  return inf;
 73     while(ch[tmp][1])
 74         tmp=ch[tmp][1];
 75     return key[x]-key[tmp];
 76 }
 77 //找后继,即右子树的最左结点
 78 int get_next(int x){
 79     int tmp=ch[x][1];
 80     if(tmp==0)  return inf;
 81     while(ch[tmp][0])
 82         tmp=ch[tmp][0];
 83     return key[tmp]-key[x];
 84 }
 85 int main(){
 86     while(scanf("%d",&n)!=EOF){
 87         root=tot1=0;
 88         int ans=0;
 89         for(int i=1;i<=n;i++){
 90             int num;
 91             if(scanf("%d",&num)==EOF) num=0;
 92             if(i==1){
 93                 ans+=num;
 94                 NewNode(root,0,num);
 95                 continue;
 96             }
 97             if(Insert(num)==0) continue;
 98             int a=get_next(root);
 99             int b=get_pre(root);
100             ans+=min(a,b);
101         }
102         printf("%d
",ans);
103     }
104     return 0;
105 }
View Code

 

原文地址:https://www.cnblogs.com/nowornever/p/4053295.html