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

第一个为线段树做法第二个为平衡树做法

线段树:
设每天的营业额在线段树数组中的下标为他本身的值
然后每次输入一个数时我们先在这个数的左边的区间查找最大值
再在右边的区间查找最小值
(这意味着我们要找比这个数小的最大值和比这个数大的最小值)
然后比较两者取差的绝对值最小的加到Ans里即可。

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<cstdlib>
  5. #define MAXN (32767+5)
  6. #define MAX (2000000)
  7. using namespace std;
  8.  
  9. struct node
  10. {
  11. int max,min;
  12. }Segt[MAX*+];
  13.  
  14. int n,a[MAXN],INF,ans;
  15.  
  16. void Update(int node,int l,int r,int x,int k)
  17. {
  18. if (l==r)
  19. Segt[node].max=Segt[node].min=k;
  20. else
  21. {
  22. int mid=(l+r)/;
  23. if (x<=mid) Update(node*,l,mid,x,k);
  24. if (x>mid) Update(node*+,mid+,r,x,k);
  25. Segt[node].max=max(Segt[node*].max,
  26. Segt[node*+].max);
  27. Segt[node].min=min(Segt[node*].min,
  28. Segt[node*+].min);
  29. }
  30. }
  31.  
  32. int QueryMax(int node,int l,int r,int l1,int r1)
  33. {
  34. if (l>r1||r<l1)
  35. return -INF;
  36. if (l1<=l && r<=r1)
  37. return Segt[node].max;
  38. int mid=(l+r)/;
  39. return max(QueryMax(node*,l,mid,l1,r1),
  40. QueryMax(node*+,mid+,r,l1,r1));
  41. }
  42.  
  43. int QueryMin(int node,int l,int r,int l1,int r1)
  44. {
  45. if (l>r1||r<l1)
  46. return INF;
  47. if (l1<=l && r<=r1)
  48. return Segt[node].min;
  49. int mid=(l+r)/;
  50. return min(QueryMin(node*,l,mid,l1,r1),
  51. QueryMin(node*+,mid+,r,l1,r1));
  52. }
  53.  
  54. int main()
  55. {
  56. memset(&INF,0x7f,sizeof(INF));
  57. for (register int i=;i<=;++i)
  58. Segt[i].min=INF,Segt[i].max=-INF;
  59.  
  60. struct node Segt;
  61. Segt.min=INF;
  62. Segt.max=-INF;
  63. scanf("%d",&n);
  64. for (int i=;i<=n;++i)
  65. {
  66. scanf("%d",&a[i]);
  67. if (i==)
  68. {
  69. Update(,,MAX,a[i]+MAX/,a[i]+MAX/);
  70. ans+=a[i];
  71. continue;
  72. }
  73. int x=QueryMax(,,MAX,,a[i]+MAX/)-MAX/;
  74. int y=QueryMin(,,MAX,a[i]+MAX/,MAX)-MAX/;
  75. Update(,,MAX,a[i]+MAX/,a[i]+MAX/);
  76. ans+=min(abs(a[i]-x),abs(a[i]-y));
  77. }
  78. printf("%d",ans);
  79. }

平衡树:

近乎Splay裸题了……
一天天插入数字,然后查询前驱和后继累加最小值就好了
注意若当前天营业额在之前出现过时(即为Cnt>1)就不用累加了

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<cstdlib>
  5. #define MAXN (50000)
  6. using namespace std;
  7.  
  8. int Cnt[MAXN];
  9. int Size[MAXN];
  10. int Key[MAXN];
  11. int Father[MAXN];
  12. int Son[MAXN][];
  13. int SIZE,ROOT;
  14. int INF;
  15.  
  16. void Clear(int x)
  17. {
  18. Cnt[x]=Size[x]=Key[x]=Father[x]=Son[x][]=Son[x][]=;
  19. }
  20.  
  21. void Update(int x)
  22. {
  23. if (x)
  24. {
  25. Size[x]+=Cnt[x];
  26. if (Son[x][]) Size[x]+=Cnt[Son[x][]];
  27. if (Son[x][]) Size[x]+=Cnt[Son[x][]];
  28. }
  29. }
  30.  
  31. int Get(int x)
  32. {
  33. return Son[Father[x]][]==x;
  34. }
  35.  
  36. void Rotate(int x)
  37. {
  38. int fa=Father[x];
  39. int fafa=Father[fa];
  40. int wh=Get(x);
  41. Father[fa]=x;
  42. Son[fa][wh]=Son[x][wh^];
  43. if (Son[fa][wh]) Father[Son[fa][wh]]=fa;
  44. Son[x][wh^]=fa;
  45. Father[x]=fafa;
  46. if (fafa) Son[fafa][Son[fafa][]==fa]=x;
  47. Update(fa);
  48. Update(x);
  49. }
  50.  
  51. void Splay(int x)
  52. {
  53. for (int fa;fa=Father[x];Rotate(x))
  54. if (Father[fa])
  55. Rotate(Get(x)==Get(fa)?fa:x);
  56. ROOT=x;
  57. }
  58.  
  59. void Insert(int x)
  60. {
  61. if (ROOT==)
  62. {
  63. ROOT=++SIZE;
  64. Key[SIZE]=x;
  65. Cnt[SIZE]=Size[SIZE]=;
  66. return;
  67. }
  68. int now=ROOT,fa=;
  69. while ()
  70. {
  71. if(Key[now]==x)
  72. {
  73. ++Cnt[now];
  74. Update(now);
  75. Splay(now);
  76. return;
  77. }
  78. fa=now;now=Son[now][x>Key[now]];
  79. if (now==)
  80. {
  81. ++SIZE;
  82. Key[SIZE]=x;
  83. Cnt[SIZE]=Size[SIZE]=;
  84. Father[SIZE]=fa;
  85. Son[fa][x>Key[fa]]=SIZE;
  86. Update(fa);
  87. Splay(SIZE);
  88. return;
  89. }
  90. }
  91. }
  92.  
  93. int Pre()
  94. {
  95. int now=Son[ROOT][];
  96. if (now==) return INF;
  97. while (Son[now][])
  98. now=Son[now][];
  99. return Key[now];
  100. }
  101.  
  102. int Next()
  103. {
  104. int now=Son[ROOT][];
  105. if (now==) return INF;
  106. while (Son[now][])
  107. now=Son[now][];
  108. return Key[now];
  109. }
  110.  
  111. int main()
  112. {
  113. int n,x,Ans=;
  114. memset(&INF,0x7f,sizeof(INF));
  115. scanf("%d",&n);
  116. for (int i=;i<=n;++i)
  117. {
  118. scanf("%d",&x);
  119. Insert(x);
  120. if (i==)
  121. Ans+=x;
  122. else
  123. if (Cnt[ROOT]<=)
  124. Ans+=min(abs(x-Pre()),abs(x-Next()));
  125. }
  126. printf("%d",Ans);
  127. }

1588. [HNOI2002]营业额统计【平衡树-splay 或 线段树】的更多相关文章

  1. BZOJ 1588: [HNOI2002]营业额统计 双向链表 / splay / treap

    1588: [HNOI2002]营业额统计 Description 营业额统计 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger ...

  2. bzoj 1588: [HNOI2002]营业额统计(splay入门)

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1588 题解:这题如果用普通的bst的话是可以过时间差不多4s左右如果用splay的话是14 ...

  3. 数据结构:(平衡树,链表)BZOJ 1588[HNOI2002]营业额统计

    1588: [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 12173  Solved: 4354[Submit][Sta ...

  4. 1588: [HNOI2002]营业额统计 (splay tree)

    1588: [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 5783  Solved: 1859[Submit][Stat ...

  5. Bzoj 1588: [HNOI2002]营业额统计(splay)

    1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MB Description 营业额统计 Tiger最近被公司升任为营业部经理,他上 ...

  6. BZOJ 1588: [HNOI2002]营业额统计

    1588: [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 14396  Solved: 5521[Submit][Sta ...

  7. 1588: [HNOI2002]营业额统计

    1588: [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 9203  Solved: 3097[Submit][Stat ...

  8. 2018.07.06 BZOJ 1588: HNOI2002营业额统计(非旋treap)

    1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MB Description 营业额统计 Tiger最近被公司升任为营业部经理,他上 ...

  9. bzoj 1588: [HNOI2002]营业额统计 treap

    1588: [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 13902  Solved: 5225[Submit][Sta ...

  10. BZOJ 1588: [HNOI2002]营业额统计 双向链表

    BZOJ 1588: [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 512 MBSubmit: 9619  Solved: 3287 题目连接 ht ...

随机推荐

  1. MyBatis中插入并返回主键

    开发过程中经常遇到需要插入一条数据,并且返回这条数据自增的主键,在MyBatis中只需要在mapper中添加keyProperty属性即可 在mapper中添加keyProperty属性 <in ...

  2. 四、curator recipes之共享重入互斥锁

    简介 curator的recipes实现了可重入互斥锁,允许你在分布式场景下多个进程之间实现锁的互斥以协调多进程执行. 相关类:InterProcessMutex 官方文档:http://curato ...

  3. SSM迁移到Springboot记录

    日志问题 Exception in thread "main" java.lang.IllegalArgumentException: LoggerFactory is not a ...

  4. 【SSH网上商城项目实战24】Struts2中如何处理多个Model请求

       转自: https://blog.csdn.net/eson_15/article/details/51465067 1. 问题的提出 Struts2中如果实现了ModelDriven<m ...

  5. hdu 1075 What Are You Talking About 字典树模板

    What Are You Talking About Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K ...

  6. HTTP 错误500.19 - 错误代码 0x80070021

    1.错误描述 HTTP 错误500.19 -Internal Server Error 无法访问请求的页面,因为该页的相关配置数据无效. 详细错误信息 模块 IIS Web Core  通知 Begi ...

  7. 从JSON中读取数据追加到HTML中

    本文来自https://www.jianshu.com/p/04127d74d88c,并非本人原创,只是作为自己学习使用的资料,如有浏览者请点击地址自行到原作者页面浏览 有时候我们需要将json数据直 ...

  8. BZOJ1093 [SCOI2003]字符串折叠

    Description 折叠的定义如下: 1. 一个字符串可以看成它自身的折叠.记作S  S 2. X(S)是X(X>1)个S连接在一起的串的折叠.记作X(S)  SSSS…S(X个S). ...

  9. 【HTML&CSS】基本的入门

    在公司培训一段时间不久就去流浪了一段时间,现在回来重新捧起心爱的编程,特别亲切. 自学HTML&CSS,别人说了很多,这那这那的,无论简单还是困难,不亲自去俯下身子学习,怎么都学不会HTML和 ...

  10. RGB与INT类型的转换

    开发时遇到的问题,设置图层样式时颜色的返回值是uint,一时不知改怎么转换为C#常用的RGB值了. 一番百度,结果如下: RGB = R + G * 256 + B * 256 * 256 因此可得到 ...