1588: [HNOI2002]营业额统计

Time Limit: 5 Sec  Memory Limit: 162 MB
Submit: 5783  Solved: 1859
[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

  1. /* ***********************************************
  2. Author :kuangbin
  3. Created Time :2013/8/24 10:57:20
  4. File Name :F:\2013ACM练习\专题学习\splay_tree_2\营业额统计.cpp
  5. ************************************************ */
  6.  
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <iostream>
  10. #include <algorithm>
  11. #include <vector>
  12. #include <queue>
  13. #include <set>
  14. #include <map>
  15. #include <string>
  16. #include <math.h>
  17. #include <stdlib.h>
  18. #include <time.h>
  19. using namespace std;
  20.  
  21. #define Key_value ch[ch[root][1]][0]
  22. const int MAXN = ;
  23. int pre[MAXN],ch[MAXN][],key[MAXN];
  24. int root,tot1;
  25.  
  26. int s[MAXN],tot2;//内存池
  27.  
  28. void NewNode(int &r,int father,int k)
  29. {
  30. if(tot2) r = s[tot2--];//取的时候是tot2--,存的时候就要是++tot2
  31. else r = ++tot1;//这个必须是++tot1
  32. pre[r] = father;
  33. ch[r][] = ch[r][] = ;
  34. key[r] = k;
  35. }
  36. //初始化
  37. void Init()
  38. {
  39. root = tot1 = tot2 = ;
  40. ch[root][] = ch[root][] = key[root] = pre[root] = ;
  41. }
  42. //旋转,0为左旋,1为右旋
  43. void Rotate(int x,int kind)
  44. {
  45. int y = pre[x];
  46. ch[y][!kind] = ch[x][kind];
  47. pre[ch[x][kind]] = y;
  48. if(pre[y])
  49. ch[pre[y]][ch[pre[y]][]==y] = x;
  50. pre[x] = pre[y];
  51. ch[x][kind] = y;
  52. pre[y] = x;
  53. }
  54. //Splay调整,将r结点调整到goal下面
  55. void Splay(int r,int goal)
  56. {
  57. while(pre[r] != goal)
  58. {
  59. if(pre[pre[r]]==goal)
  60. Rotate(r,ch[pre[r]][] ==r);
  61. else
  62. {
  63. int y = pre[r];
  64. int kind = ch[pre[y]][]==y;
  65. if(ch[y][kind] == r)
  66. {
  67. Rotate(r,!kind);
  68. Rotate(r,kind);
  69. }
  70. else
  71. {
  72. Rotate(y,kind);
  73. Rotate(r,kind);
  74. }
  75. }
  76. }
  77. if(goal == )root = r;
  78. }
  79.  
  80. void Insert(int k)//插入一个值为k的结点(有重复插入)
  81. {
  82. int r = root;
  83. if(r == )
  84. {
  85. NewNode(root,,k);
  86. return;
  87. }
  88. while(ch[r][key[r]<k])
  89. r = ch[r][key[r]<k];
  90. NewNode(ch[r][key[r]<k],r,k);
  91. Splay(ch[r][key[r]<k],);//转到根部
  92. }
  93. //找前驱
  94. int Get_pre(int r)
  95. {
  96. if(ch[r][] == )return -;//不存在
  97. r = ch[r][];
  98. while(ch[r][])r = ch[r][];
  99. return r;
  100. }
  101. //找后继
  102. int Get_next(int r)
  103. {
  104. if(ch[r][] == )return -;
  105. r = ch[r][];
  106. while(ch[r][])r = ch[r][];
  107. return r;
  108. }
  109. const int INF = 0x3f3f3f3f;
  110.  
  111. int main()
  112. {
  113. //freopen("in.txt","r",stdin);
  114. //freopen("out.txt","w",stdout);
  115. int n;
  116. while(scanf("%d",&n) == )
  117. {
  118. Init();
  119. int a;
  120. int ans = ;
  121. for(int i = ;i < n;i++)
  122. {
  123. if(scanf("%d",&a) == EOF) a= ;
  124. Insert(a);
  125. if(i == )
  126. ans += a;
  127. else
  128. {
  129. int tmp = INF;
  130. int t1 = Get_pre(root);
  131. if(t1 != -)
  132. tmp = min(tmp,key[root] - key[t1]);
  133. int t2 = Get_next(root);
  134. if(t2 != -)
  135. tmp = min(tmp,key[t2] - key[root]);
  136. ans += tmp;
  137. }
  138. }
  139. printf("%d\n",ans);
  140. }
  141. return ;
  142. }

1588: [HNOI2002]营业额统计 (splay tree)的更多相关文章

  1. [HNOI2002]营业额统计 Splay tree入门题

    题目连接:http://www.lydsy.com/JudgeOnline/problem.php?id=1588 1588: [HNOI2002]营业额统计 Time Limit: 5 Sec   ...

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

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

  3. [HNOI2002]营业额统计 Splay tree

    Splay tree入门题,学好代码风格,学习HH大牛的,传送门.. #include <functional> #include <algorithm> #include & ...

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

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

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

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

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

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

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

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

  8. BZOJ1588 [HNOI2002]营业额统计 splay模板

    1588: [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MB Submit: 16189  Solved: 6482 [Submit][S ...

  9. bzoj1588: [HNOI2002]营业额统计(splay)

    1588: [HNOI2002]营业额统计 题目:传送门 题解: 复习splay所以来刷个水... 题目描述不是特别清楚:应该是找第i天以前一个最小的营业额和第i天做差的最小值作为第i天的最小波动值 ...

随机推荐

  1. Scala中“=>”用法及含义

    => has several meanings in Scala, all related to its mathematical meaning as implication. 1. In a ...

  2. DOS命令基础,包涵DOS库说明书

    20种常用的DOS命令小结 作者: 字体:[增加 减小] 类型:转载   DOS命令总共大约有一百个(包括文本编辑.查杀病毒.配置文件.批处理等),我们这里详细介绍二十个常用的DOS命令     先介 ...

  3. [USACO16OPEN]262144

    传送门啦 其实大家可以先看一下这个题 [USACO16OPEN]248 分析: 数据范围很奇特:n特别,a[i]特别——如果O(N^3)能接受就直接区间DP水过了,但是不行,于是考虑设计一个状态囊括a ...

  4. Linux基础 - crontab

    列出当前用户设置的定时任务 crontab -l 编辑定时任务 crontab -e 用法 m h dom mon dow * * * * * command 字段详解: *:any m: minut ...

  5. 四B象限图

  6. qlserver排序规则在全角与半角处理中的应用

    --1.查询区分全角与半角字符--测试数据DECLARE @t TABLE(col varchar(10))INSERT @t SELECT 'aa'UNION ALL SELECT 'Aa'UNIO ...

  7. Python学习笔记:出生日期转化为年龄

    在数据挖掘项目中,有时候个体的出生日期包含信息量过大,不适合作为一个有效数据进入模型算法训练,因此有必要把出生日期转化为年龄age,age是一个很好的特征工程指示变量. import pandas a ...

  8. Kaldi 安装

    以后要重点搞caldi了,虽然集群上有,但还是本地安装一下吧. 参考   Kaldi 学习手记(一):Kaldi 的编译安装   在 ubuntu 下安装 kaldi 基本步骤 两个文章基本差不多 1 ...

  9. Deepin Linux安装MySQL方法

    sudo apt-get install mysql-server apt-get install mysql-client sudo apt-get install libmysqlclient-d ...

  10. pct_free

    SQL> select table_name,pct_free,pct_used from user_tables; TABLE_NAME PCT_FREE PCT_USED---------- ...