您的位置:首页 > 其它

Splay树-Codevs 1296 营业额统计

2015-08-28 11:19 381 查看

Codevs 1296 营业额统计

题目描述 Description

Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况。Tiger拿出了公司的账本,账本上记录了公司成立以来每天的营业额。分析营业情况是一项相当复杂的工作。由于节假日,大减价或者是其他情况的时候,营业额会出现一定的波动,当然一定的波动是能够接受的,但是在某些时候营业额突变得很高或是很低,这就证明公司此时的经营状况出现了问题。经济管理学上定义了一种最小波动值来衡量这种情况:该天的最小波动值 = min|该天以前某一天的营业额−该天营业额|当最小波动值越大时,就说明营业情况越不稳定。而分析整个公司的从成立到现在营业情况是否稳定,只需要把每一天的最小波动值加起来就可以了。你的任务就是编写一个程序帮助Tiger来计算这一个值。第一天的最小波动值为第一天的营业额。

输入描述 Input Description

第一行为正整数n(n<=32767),表示该公司从成立一直到现在的天数,接下来的n行每行有一个正整数ai(ai<=1000000),表示第i天公司的营业额。

输出描述 Output Description

输出文件仅有一个正整数,即每天最小波动值之和,小于231

样例输入 Sample Input

6
5
1
2
5
4
6

样例输出 Sample Output

12

数据范围及提示 Data Size & Hint

结果说明:5+|1−5|+|2−1|+|5−5|+|4−5|+|6−5|=5+4+1+0+1+1=12

题解

很显然,这道题是一道衤果的Splay树。手写Splay:
#include <iostream>
#define maxn 50005
#define INF 1E8
using namespace std;
int min(int a, int b)
{
return a < b ? a : b;
}
int abs(int x)
{
return x < 0 ? (-x) : x;
}
struct splay_tree
{
int ch[maxn][2], val[maxn], root, fa[maxn], n;
inline int wson(int x)
{
if(x == ch[fa[x]][0])
return 0;
return 1;
}
inline void rotate(int x)
{
int y = fa[x], z = fa[y], a = wson(x), b = wson(y);
ch[z][b] = x; fa[x] = z;
ch[y][a] = ch[x][!a]; fa[ch[x][!a]] = y;
ch[x][!a] = y; fa[y] = x;
}
inline void splay(int x, int p = 0)
{
while(fa[x] != p)
{
int y = fa[x], z = fa[y];
if(z == p)
rotate(x);
else
{
if(wson(x) == wson(y))
{
rotate(y);
rotate(x);
}
else
{
rotate(x);
rotate(x);
}
}
}
if(!p) root = x;
}
inline void insert(int a)
{
if(!n)
{
n = 1;
root = 1;
val[1] = a;
ch[1][0] = ch[1][1] = fa[1] = 0;
return;
}
int x = root, y;
while(x)
{
y = x;
if(a < val[x])
x = ch[x][0];
else x = ch[x][1];
}
++n;
if(a < val[y])
{
ch[y][0] = n;
val
= a;
fa
= y;
ch
[0] = ch
[1] = 0;
splay(n, 0);
}
else
{
ch[y][1] = n;
val
= a;
fa
= y;
ch
[0] = ch
[1] = 0;
splay(n, 0);
}
}
inline int at(int loc)
{
return val[loc];
}
inline int last(int loc)
{
int x = loc, y;
if(ch[x][0])
{
x = ch[x][0];
while(x)
{
y = x;
x = ch[x][1];
}
splay(y);
return y;
}
y = fa[x];
while(y && x == ch[y][0])
{
x = y;
y = fa[y];
}
splay(y);
return y;
}
inline int next(int loc)
{
int x = loc, y;
if(ch[x][1])
{
x = ch[x][1];
while(x)
{
y = x;
x = ch[x][0];
}
splay(y);
return y;
}
y = fa[x];
while(y && x == ch[y][1])
{
x = y;
y = fa[y];
}
splay(y);
return y;
}
inline int find_lower_bound(int key)
{
int x = root, y;
while(x)
{
y = x;
if(key == val[x])
return x;
else
{
if(key < val[x])
x = ch[x][0];
else x = ch[x][1];
}
}
if(val[y] < key)
return next(y);
splay(y);
return y;
}
};
splay_tree tree;
int main()
{
int n;
tree.insert(INF);
tree.insert(-INF);
cin >> n;
int ans, a, pos, de;
cin >> a;
ans = a;
tree.insert(a);
for(int i = 1; i < n; ++i)
{
cin >> a;
pos = tree.find_lower_bound(a);
de = min(abs(a - tree.at(pos)), abs(tree.at(tree.last(pos)) - a));
ans += de;
tree.insert(a);
}
cout << ans;
return 0;
}
STL模板(STL)大法好!代码:
据说智商达到180的人才会看到哦~,语文老师告诉我这叫留给读者想象的空间~
                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Splay 平衡树 树结构