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. java工程项目里,在一个包里面,不能出现同名的类名,这问题是刚接触java才会遇到的,特别是新手一般都没有建立包,而是使用默认的,易出现同名的类名,导致eclipse提示错误

    java工程项目里,在一个包里面,不能出现同名的类名,这问题是刚接触java才会遇到的,特别是新手一般都没有建立包,而是使用默认的,易出现同名的类名,导致eclipse提示错误. 问题: 创建了一个工 ...

  2. Avoid non-default constructors in fragments: use a default constructor plus Fragment#setArguments(Bundle) instead

    “Avoid non-default constructors in fragments: use a default constructor plus Fragment#setArguments(B ...

  3. 最新Connectify注冊码(序列号) Connectify3.7序列号 破解版

    Connectify序列号.最新注冊码 今天给大家公布一个Connectify最新版的序列号(注冊码) 今天给大家公布一个Connectify最新版的序列号(注冊码) 经本人測试该注冊码为最新版Con ...

  4. OpenCV与QT联合编译 分类: Eye_Detection ZedBoard OpenCV shell ubuntu 2014-11-08 18:54 143人阅读 评论(0) 收藏

    问题1:首先参考rainysky的博客,发现qmake时发生找不到目录,文件的错误,又找不到 qmake.conf 文件的写法.所以开始按照网上的程序修改 XXX.pro 文件. 问题2:使用QT C ...

  5. [RxJS] Utility operator: do

    We just saw map which is a transformation operator. There are a couple of categories of operators, s ...

  6. android 06 LinearLayout

    xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:la ...

  7. [转] Linux strace 简介

    http://www.cnblogs.com/ggjucheng/archive/2012/01/08/2316692.html 简介 strace常用来跟踪进程执行时的系统调用和所接收的信号. 在L ...

  8. Socket.IO 概述

    为了防止无良网站的爬虫抓取文章,特此标识,转载请注明文章出处.LaplaceDemon/SJQ. http://www.cnblogs.com/shijiaqi1066/p/3826251.html ...

  9. JavaScript 数据类型转换(显式与隐式)

    一.数据类型 JS中有5中简单数据类型(也称为基本数据类型):Undefined.Null.Boolean.Number.String.还有一种复杂数据类型------Object,Object本质是 ...

  10. HDFS的Java客户端操作代码(查看HDFS下的文件是否存在)

    1.查看HDFS目录下得文件是否存在 package Hdfs; import java.io.IOException; import java.net.URI; import org.apache. ...