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

平衡树裸题。
 #include<cstdio>
#include<cstdlib>
#include<cmath>
using namespace std;
#define maxn 33000
int n,share[maxn];
int lc[maxn],rc[maxn],father[maxn],root,ans; inline int small(int a,int b) {if (a < b) return a; return b;} inline void work();
inline void zig(int);
inline void zag(int);
inline int find(int);
inline void ins(int);
inline void splay(int); int main()
{
freopen("1588.in","r",stdin);
freopen("1588.out","w",stdout);
scanf("%d",&n);
int i;
for (i = ;i<=n;i++)
scanf("%d",share+i);
work();
printf("%d",ans);
fclose(stdin); fclose(stdout);
return ;
} inline int find(int a)
{
int ret = ,now = root;
ret = small(abs(share[now]-a),ret);
now = rc[root];
while (now > )
{
if (share[now] == a)
return ;
ret = small(abs(share[now]-a),ret);
if (share[now] > a)
now = rc[now];
else now = lc[now];
}
now = lc[root];
while (now > )
{
if (share[now] == a)
return ;
ret = small(abs(share[now]-a),ret);
if (share[now] > a)
now = rc[now];
else now = lc[now];
}
return ret;
} inline void work()
{
ans = share[]; root = ;
int i,j,k;
for (i = ;i<=n;i++)
{
k = find(share[i]);
ans += k;
if (k != )
ins(i);
}
} inline void ins(int a)
{
int last,now;
now = root;
while (now != )
{
last = now;
if (share[now] > share[a])
now = rc[now];
else now = lc[now];
}
father[a] = last;
if (share[a] > share[last])
lc[last] = a;
else rc[last] = a;
splay(a);
root = a;
} inline void zig(int x)
{
int y = father[x];
father[x] = father[y];
if (father[y] != )
{
if (lc[father[y]] == y)
lc[father[y]] = x;
else rc[father[y]] = x;
}
father[y] = x;
lc[y] = rc[x];
if (rc[x] != )
father[rc[x]] = y;
rc[x] = y;
} inline void zag(int x)
{
int y = father[x];
father[x] = father[y];
if (father[y] != )
{
if (lc[father[y]] == y)
lc[father[y]] = x;
else rc[father[y]] = x;
}
father[y] = x;
rc[y] = lc[x];
if (lc[x] != )
father[lc[x]] = y;
lc[x] = y;
} inline void splay(int a)
{
while (father[a] != )
{
if (father[father[a]] == )
{
if (a == lc[father[a]])
zig(a);
else zag(a);
}
else
{
if (lc[father[father[a]]]==father[a] && lc[father[a]] == a)
{
zig(father[a]),
zig(a);
}
else if (lc[father[father[a]]]==father[a] && rc[father[a]] == a)
{
zag(a);
zig(a);
}
else if (rc[father[father[a]]]==father[a] && lc[father[a]] == a)
{
zig(a);
zag(a);
}
else if (rc[father[father[a]]]==father[a] && rc[father[a]] == a)
{
zag(father[a]);
zag(a);
}
}
}
}

BZOJ 1588 营业额统计的更多相关文章

  1. BZOJ 1588 营业额统计 set

    题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1588 题目大意: 营业额统计 Tiger最近被公司升任为营业部经理,他上任后接受公司交 ...

  2. (HYSBZ)BZOJ 1588 营业额统计

    营业额统计 Time Limit: 5000MS   Memory Limit: 165888KB   64bit IO Format: %lld & %llu Description 营业额 ...

  3. bzoj 1588营业额统计(HNOI 2002)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1588 splay  bottom-up的数组实现. 题意就是给你一组数,求每个数与在其前面且与其最相 ...

  4. BZOJ 1588 营业额统计 Splay

    主要操作为Splay中插入节点,查找前驱和后继节点. 1: #include <cstdio> 2: #include <iostream> 3: #include <c ...

  5. [bzoj] 1588 营业额统计 || Splay板子题

    原题 给出一个n个数的数列ai ,对于第i个元素ai定义\(fi=min(|ai-aj|) (1<=j<i)\),f1=a1,求\(/sumfi\) Splay板子题. Splay讲解:h ...

  6. HYSBZ - 1588 营业额统计 (伸展树)

    题意:营业额统计 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成立以来每天的营业额.分析营 ...

  7. HYSBZ 1588 营业额统计

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1588 题意:详见题面,中文 思路:平衡树的模板题. 可用Treap,Splay,Scape ...

  8. HYSBZ 1588 营业额统计 (Splay树)

    题意:给出一个公司每一天的营业额,求每天的最小波动值之和.该天的最小波动值= min { 绝对值| 该天以前某一天的营业额-该天的营业额 | }.第一天的最小波动值就是其自己. 思路:Splay伸展树 ...

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

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

随机推荐

  1. 如何优化cocos2d程序的内存使用和程序大小:第二部分_(转)

    减少你的程序的大小 把纹理的颜色位深度减少到16位,不仅可以减少内存压力,还可以有效地减少程序的体积.但是,我们还有其它方法可以更进一步地减少程序的大小. TexturePacker PNG 图片优化 ...

  2. hdu1043Eight (经典的八数码)(康托展开+BFS)

    建议先学会用康托展开:http://blog.csdn.net/u010372095/article/details/9904497 Problem Description The 15-puzzle ...

  3. 使用php glob函数查找文件,遍历文件目录(转)

    函数说明:array glob ( string $pattern [, int $flags ] )功能:寻找与模式匹配的文件路径,返回包含匹配文件(目录)的数组(注:被检查的文件必须是服务器系统的 ...

  4. Linux之TCPIP内核参数优化

    /proc/sys/net目录 所有的TCP/IP参数都位于/proc/sys/net目录下(请注意,对/proc/sys/net目录下内容的修改都是临时的,任何修改在系统重启后都会丢失),例如下面这 ...

  5. [转] Mac OX上安装MongoDb

    https://scotch.io/tutorials/an-introduction-to-mongodb MongoDB的安装有好多种安装方法,有普通青年的HomeBrew方式,也有文艺青年的源码 ...

  6. ASP.NET图片验证码

    1. 新建一个Validate.aspx,然后在Validate.aspx.cs编写代码: using System; using System.Collections; using System.C ...

  7. IntelliJ IDEA 14

    新接触IntelliJ IDEA 14,使用起来还不是很称手,每天在使用中学习吧. 每学到一个新技能就来更新一下. (2015.11.17) " Ctrl + / " 代码批量注释 ...

  8. Member var and Static var.

    /* member variable and static variable: 1,invoke ways: member variable,also called 'instance' variab ...

  9. Struts2 中拦截器和Action的调用关系(写的很好)

    http://blog.csdn.net/hackerain/article/details/6991082

  10. PHP做支付宝即时到账需注意

    注意:1按照人家的参数规则,规范填写参数列表:2商家信息填写正确:3然后提交走后注意此时告别了咱们的服务器,将在咱们服务器的订单信息提交到了支付宝服务器,然后支付宝服务器进行支付宝支付流程,当如果支付 ...