题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1588
题意:
Description
营业额统计 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况。 Tiger拿出了公司的账本,账本上记录了公司成立以来每天的营业额。分析营业情况是一项相当复杂的工作。由于节假日,大减价或者是其他情况的时候,营业额会出现一定的波动,当然一定的波动是能够接受的,但是在某些时候营业额突变得很高或是很低,这就证明公司此时的经营状况出现了问题。经济管理学上定义了一种最小波动值来衡量这种情况: 该天的最小波动值 当最小波动值越大时,就说明营业情况越不稳定。 而分析整个公司的从成立到现在营业情况是否稳定,只需要把每一天的最小波动值加起来就可以了。你的任务就是编写一个程序帮助Tiger来计算这一个值。 第一天的最小波动值为第一天的营业额。
Input
第一行为正整数 ,表示该公司从成立一直到现在的天数,接下来的n行每行有一个整数(有可能有负数) ,表示第i天公司的营业额。
Output
输出文件仅有一个正整数,即Sigma(每天最小的波动值) 。结果小于2^31 。
思路:初识伸展树,用来学伸展树的题目,代码是看了大神的博客:http://blog.csdn.net/acm_cxlove/article/details/7815019,多谢。刚开始学伸展树真是坑,旋转什么的完全不懂,然后这部分居然是看百度百科看懂的^_^||,懂了基础,看了大神博客,这绝对是我看过了最凝练的代码,真难看懂。。。。。。自己又加了一点注释。。。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int N = 100010, INF = 0x3f3f3f3f;
int son[N][2], key[N], pre[N];
int root, num;
void new_node(int &x, int fa, int v) //新建节点
{
x = ++num;
pre[x] = fa, key[x] = v;
son[x][0] = son[x][1] = 0;
}
//zag是左旋,zig是右旋
void _rotate(int x, int dir)
{ //旋转,dir==0为左旋,dir==1为右旋
int y = pre[x];
son[y][!dir] = son[x][dir], pre[son[x][dir]] = y;
if(pre[y]) son[pre[y]][son[pre[y]][1]==y] = x;
pre[x] = pre[y];
son[x][dir] = y, pre[y] = x;
}
void splay(int x, int goal) //splay操作
{
while(pre[x] != goal)
{
int y = pre[x];
if(pre[y] == goal) _rotate(x, son[y][0] == x); //父节点为根,直接旋转
else
{
int dir = son[pre[y]][0] == y;
if(son[y][dir] == x) _rotate(x, !dir), _rotate(x, dir); //之字形旋转
else _rotate(y, dir), _rotate(x, dir); //一字型旋转
}
}
if(goal == 0) root = x;
}
int get_prec(int x) //求前驱
{
int t = son[x][0];
while(son[t][1]) t = son[t][1];
return key[t];
}
int get_subs(int x) //求后继
{
int t = son[x][1];
while(son[t][0]) t = son[t][0];
return key[t];
}
bool _insert(int v) //插入节点
{
if(!root) new_node(root, 0, v); // 往空树中插入节点
else
{
int x = root;
while(son[x][key[x]<v])
{
if(key[x] == v)
{
splay(x, 0);
return false;
}
x = son[x][key[x]<v];
}
new_node(son[x][key[x]<v], x, v);
splay(son[x][key[x]<v], 0);
}
return true;
}
int main()
{
int n, a, res = 0;
scanf("%d", &n);
root = num = 0;
_insert(-INF);
_insert(INF);
for(int i = 1; i <= n; i++)
{
scanf("%d", &a);
if(i == 1) _insert(a), res += a;
else
{
if(_insert(a))
{
int b = get_prec(root), c = get_subs(root);
res += min(a-b, c-a);
}
}
}
printf("%d", res);
return 0;
}
另附我用set写的代码,效率和伸展树一样
//用set容器做法
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <set>
#include <algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
int main()
{
set<int> ste;
set<int> :: iterator p;
int n, a, res = 0;
scanf("%d", &n);
while(n--)
{
scanf("%d", &a);
if(ste.empty())
res += a, ste.insert(a);
else
{
p = ste.lower_bound(a);
if(*p != a)
{
int t1 = INF, t2 = INF;
t1 = abs(*p - a);
if(p != ste.begin())
t2 = abs(*(--p) - a);
res += min(t1, t2);
}
ste.insert(a);
}
}
printf("%d\n", res);
return 0;
}