1588. [HNOI2002]营业额统计【平衡树-splay 或 线段树】
Description
营业额统计 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况。 Tiger拿出了公司的账本,账本上记录了公司成立以来每天的营业额。分析营业情况是一项相当复杂的工作。由于节假日,大减价或者是其他情况的时候,营业额会出现一定的波动,当然一定的波动是能够接受的,但是在某些时候营业额突变得很高或是很低,这就证明公司此时的经营状况出现了问题。经济管理学上定义了一种最小波动值来衡量这种情况: 该天的最小波动值 当最小波动值越大时,就说明营业情况越不稳定。 而分析整个公司的从成立到现在营业情况是否稳定,只需要把每一天的最小波动值加起来就可以了。你的任务就是编写一个程序帮助Tiger来计算这一个值。 第一天的最小波动值为第一天的营业额。 输入输出要求
Input
Output
输出文件仅有一个正整数,即Sigma(每天最小的波动值) 。结果小于2^31 。
Sample Input
5
1
2
5
4
6
Sample Output
HINT
结果说明:5+|1-5|+|2-1|+|5-5|+|4-5|+|6-5|=5+4+1+0+1+1=12
第一个为线段树做法第二个为平衡树做法
线段树:
设每天的营业额在线段树数组中的下标为他本身的值
然后每次输入一个数时我们先在这个数的左边的区间查找最大值
再在右边的区间查找最小值
(这意味着我们要找比这个数小的最大值和比这个数大的最小值)
然后比较两者取差的绝对值最小的加到Ans里即可。
- #include<iostream>
- #include<cstdio>
- #include<cstring>
- #include<cstdlib>
- #define MAXN (32767+5)
- #define MAX (2000000)
- using namespace std;
- struct node
- {
- int max,min;
- }Segt[MAX*+];
- int n,a[MAXN],INF,ans;
- void Update(int node,int l,int r,int x,int k)
- {
- if (l==r)
- Segt[node].max=Segt[node].min=k;
- else
- {
- int mid=(l+r)/;
- if (x<=mid) Update(node*,l,mid,x,k);
- if (x>mid) Update(node*+,mid+,r,x,k);
- Segt[node].max=max(Segt[node*].max,
- Segt[node*+].max);
- Segt[node].min=min(Segt[node*].min,
- Segt[node*+].min);
- }
- }
- int QueryMax(int node,int l,int r,int l1,int r1)
- {
- if (l>r1||r<l1)
- return -INF;
- if (l1<=l && r<=r1)
- return Segt[node].max;
- int mid=(l+r)/;
- return max(QueryMax(node*,l,mid,l1,r1),
- QueryMax(node*+,mid+,r,l1,r1));
- }
- int QueryMin(int node,int l,int r,int l1,int r1)
- {
- if (l>r1||r<l1)
- return INF;
- if (l1<=l && r<=r1)
- return Segt[node].min;
- int mid=(l+r)/;
- return min(QueryMin(node*,l,mid,l1,r1),
- QueryMin(node*+,mid+,r,l1,r1));
- }
- int main()
- {
- memset(&INF,0x7f,sizeof(INF));
- for (register int i=;i<=;++i)
- Segt[i].min=INF,Segt[i].max=-INF;
- struct node Segt;
- Segt.min=INF;
- Segt.max=-INF;
- scanf("%d",&n);
- for (int i=;i<=n;++i)
- {
- scanf("%d",&a[i]);
- if (i==)
- {
- Update(,,MAX,a[i]+MAX/,a[i]+MAX/);
- ans+=a[i];
- continue;
- }
- int x=QueryMax(,,MAX,,a[i]+MAX/)-MAX/;
- int y=QueryMin(,,MAX,a[i]+MAX/,MAX)-MAX/;
- Update(,,MAX,a[i]+MAX/,a[i]+MAX/);
- ans+=min(abs(a[i]-x),abs(a[i]-y));
- }
- printf("%d",ans);
- }
平衡树:
近乎Splay裸题了……
一天天插入数字,然后查询前驱和后继累加最小值就好了
注意若当前天营业额在之前出现过时(即为Cnt>1)就不用累加了
- #include<iostream>
- #include<cstdio>
- #include<cstring>
- #include<cstdlib>
- #define MAXN (50000)
- using namespace std;
- int Cnt[MAXN];
- int Size[MAXN];
- int Key[MAXN];
- int Father[MAXN];
- int Son[MAXN][];
- int SIZE,ROOT;
- int INF;
- void Clear(int x)
- {
- Cnt[x]=Size[x]=Key[x]=Father[x]=Son[x][]=Son[x][]=;
- }
- void Update(int x)
- {
- if (x)
- {
- Size[x]+=Cnt[x];
- if (Son[x][]) Size[x]+=Cnt[Son[x][]];
- if (Son[x][]) Size[x]+=Cnt[Son[x][]];
- }
- }
- int Get(int x)
- {
- return Son[Father[x]][]==x;
- }
- void Rotate(int x)
- {
- int fa=Father[x];
- int fafa=Father[fa];
- int wh=Get(x);
- Father[fa]=x;
- Son[fa][wh]=Son[x][wh^];
- if (Son[fa][wh]) Father[Son[fa][wh]]=fa;
- Son[x][wh^]=fa;
- Father[x]=fafa;
- if (fafa) Son[fafa][Son[fafa][]==fa]=x;
- Update(fa);
- Update(x);
- }
- void Splay(int x)
- {
- for (int fa;fa=Father[x];Rotate(x))
- if (Father[fa])
- Rotate(Get(x)==Get(fa)?fa:x);
- ROOT=x;
- }
- void Insert(int x)
- {
- if (ROOT==)
- {
- ROOT=++SIZE;
- Key[SIZE]=x;
- Cnt[SIZE]=Size[SIZE]=;
- return;
- }
- int now=ROOT,fa=;
- while ()
- {
- if(Key[now]==x)
- {
- ++Cnt[now];
- Update(now);
- Splay(now);
- return;
- }
- fa=now;now=Son[now][x>Key[now]];
- if (now==)
- {
- ++SIZE;
- Key[SIZE]=x;
- Cnt[SIZE]=Size[SIZE]=;
- Father[SIZE]=fa;
- Son[fa][x>Key[fa]]=SIZE;
- Update(fa);
- Splay(SIZE);
- return;
- }
- }
- }
- int Pre()
- {
- int now=Son[ROOT][];
- if (now==) return INF;
- while (Son[now][])
- now=Son[now][];
- return Key[now];
- }
- int Next()
- {
- int now=Son[ROOT][];
- if (now==) return INF;
- while (Son[now][])
- now=Son[now][];
- return Key[now];
- }
- int main()
- {
- int n,x,Ans=;
- memset(&INF,0x7f,sizeof(INF));
- scanf("%d",&n);
- for (int i=;i<=n;++i)
- {
- scanf("%d",&x);
- Insert(x);
- if (i==)
- Ans+=x;
- else
- if (Cnt[ROOT]<=)
- Ans+=min(abs(x-Pre()),abs(x-Next()));
- }
- printf("%d",Ans);
- }
1588. [HNOI2002]营业额统计【平衡树-splay 或 线段树】的更多相关文章
- BZOJ 1588: [HNOI2002]营业额统计 双向链表 / splay / treap
1588: [HNOI2002]营业额统计 Description 营业额统计 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger ...
- bzoj 1588: [HNOI2002]营业额统计(splay入门)
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1588 题解:这题如果用普通的bst的话是可以过时间差不多4s左右如果用splay的话是14 ...
- 数据结构:(平衡树,链表)BZOJ 1588[HNOI2002]营业额统计
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 12173 Solved: 4354[Submit][Sta ...
- 1588: [HNOI2002]营业额统计 (splay tree)
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 5783 Solved: 1859[Submit][Stat ...
- Bzoj 1588: [HNOI2002]营业额统计(splay)
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MB Description 营业额统计 Tiger最近被公司升任为营业部经理,他上 ...
- BZOJ 1588: [HNOI2002]营业额统计
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 14396 Solved: 5521[Submit][Sta ...
- 1588: [HNOI2002]营业额统计
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 9203 Solved: 3097[Submit][Stat ...
- 2018.07.06 BZOJ 1588: HNOI2002营业额统计(非旋treap)
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MB Description 营业额统计 Tiger最近被公司升任为营业部经理,他上 ...
- bzoj 1588: [HNOI2002]营业额统计 treap
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 13902 Solved: 5225[Submit][Sta ...
- BZOJ 1588: [HNOI2002]营业额统计 双向链表
BZOJ 1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 512 MBSubmit: 9619 Solved: 3287 题目连接 ht ...
随机推荐
- MyBatis中插入并返回主键
开发过程中经常遇到需要插入一条数据,并且返回这条数据自增的主键,在MyBatis中只需要在mapper中添加keyProperty属性即可 在mapper中添加keyProperty属性 <in ...
- 四、curator recipes之共享重入互斥锁
简介 curator的recipes实现了可重入互斥锁,允许你在分布式场景下多个进程之间实现锁的互斥以协调多进程执行. 相关类:InterProcessMutex 官方文档:http://curato ...
- SSM迁移到Springboot记录
日志问题 Exception in thread "main" java.lang.IllegalArgumentException: LoggerFactory is not a ...
- 【SSH网上商城项目实战24】Struts2中如何处理多个Model请求
转自: https://blog.csdn.net/eson_15/article/details/51465067 1. 问题的提出 Struts2中如果实现了ModelDriven<m ...
- hdu 1075 What Are You Talking About 字典树模板
What Are You Talking About Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/204800 K ...
- HTTP 错误500.19 - 错误代码 0x80070021
1.错误描述 HTTP 错误500.19 -Internal Server Error 无法访问请求的页面,因为该页的相关配置数据无效. 详细错误信息 模块 IIS Web Core 通知 Begi ...
- 从JSON中读取数据追加到HTML中
本文来自https://www.jianshu.com/p/04127d74d88c,并非本人原创,只是作为自己学习使用的资料,如有浏览者请点击地址自行到原作者页面浏览 有时候我们需要将json数据直 ...
- BZOJ1093 [SCOI2003]字符串折叠
Description 折叠的定义如下: 1. 一个字符串可以看成它自身的折叠.记作S S 2. X(S)是X(X>1)个S连接在一起的串的折叠.记作X(S) SSSS…S(X个S). ...
- 【HTML&CSS】基本的入门
在公司培训一段时间不久就去流浪了一段时间,现在回来重新捧起心爱的编程,特别亲切. 自学HTML&CSS,别人说了很多,这那这那的,无论简单还是困难,不亲自去俯下身子学习,怎么都学不会HTML和 ...
- RGB与INT类型的转换
开发时遇到的问题,设置图层样式时颜色的返回值是uint,一时不知改怎么转换为C#常用的RGB值了. 一番百度,结果如下: RGB = R + G * 256 + B * 256 * 256 因此可得到 ...