Problem

每次给你一个数,找出前面的数与这个数的差的绝对值的最小值

Solution

Splay

Notice

找不到前驱和后继时,会出错。

Code

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define sqz main
#define ll long long
#define reg register int
#define rep(i, a, b) for (reg i = a; i <= b; i++)
#define per(i, a, b) for (reg i = a; i >= b; i--)
#define travel(i, u) for (reg i = head[u]; i; i = edge[i].next)
const int INF = 1e9, N = 40000;
const double eps = 1e-6, phi = acos(-1);
ll mod(ll a, ll b) {if (a >= b || a < 0) a %= b; if (a < 0) a += b; return a;}
ll read(){ ll x = 0; int zf = 1; char ch; while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if (ch == '-') zf = -1, ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * zf;}
void write(ll y) { if (y < 0) putchar('-'), y = -y; if (y > 9) write(y / 10); putchar(y % 10 + '0');}
int pre, suf, point = 0, root;
struct node
{
int val[N + 5], count[N + 5], num[N + 5], son[2][N + 5], parent[N + 5];
inline void up(int u)
{
count[u] = count[son[0][u]] + count[son[1][u]] + num[u];
} void Rotate(int x, int &rt)
{
int y = parent[x], z = parent[y];
int l = (son[1][y] == x), r = 1 - l;
if (y == rt) rt = x;
else if (son[0][z] == y) son[0][z] = x;
else son[1][z] = x;
parent[x] = z;
parent[son[r][x]] = y, son[l][y] = son[r][x];
parent[y] = x, son[r][x] = y;
up(y);
up(x);
} void Splay(int x, int &rt)
{
while (x != rt)
{
int y = parent[x], z = parent[y];
if (y != rt)
{
if ((son[0][z] == y) ^ (son[0][y] == x))
Rotate(x, rt);
else Rotate(y, rt);
}
Rotate(x, rt);
}
} void Insert(int &u, int x, int last)
{
if (u == 0)
{
u = ++point;
val[u] = x, parent[u] = last, num[u] = count[u] = 1;
Splay(u, root);
}
else
{
if (x > val[u]) Insert(son[1][u], x, u);
else if (x < val[u]) Insert(son[0][u], x, u);
else if (x == val[u]) num[u]++, count[u]++, Splay(u, root);
}
} void Delete(int x)
{
Splay(x, root);
if (num[x] > 1)
{
num[x]--, count[x]--;
return;
}
if (son[0][x] * son[1][x] == 0) root = son[0][x] + son[1][x];
else
{
int t = son[1][x];
while (son[0][t] != 0) t = son[0][t];
Splay(t, root);
son[0][t] = son[0][x], parent[son[0][x]] = t;
up(t);
}
parent[root] = 0;
} void Find_pre(int u, int x)
{
if (u == 0) return;
if (x >= val[u])
{
pre = u;
Find_pre(son[1][u], x);
}
else Find_pre(son[0][u], x);
} void Find_suf(int u,int x)
{
if (u == 0) return;
if (x <= val[u])
{
suf = u;
Find_suf(son[0][u], x);
}
else Find_suf(son[1][u], x);
} int Find_num(int u, int x)
{
if (x <= count[son[0][u]]) return Find_num(son[0][u], x);
if (x > count[son[0][u]] + num[u]) return Find_num(son[1][u], x - count[son[0][u]] - num[u]);
return val[u];
} int Find_id(int u, int x)
{
if (x == val[u]) return u;
if (x > val[u]) return Find_id(son[1][u], x);
if (x < val[u]) return Find_id(son[0][u], x);
}
}Splay_tree;
int main()
{
int n = read(), x = read();
int ans = x;
Splay_tree.Insert(root, x, 0);
for (int i = 2; i <= n; i++)
{
x = read();
pre = 0, suf = n + 1;
Splay_tree.Find_pre(root, x);
Splay_tree.Find_suf(root, x);
if (pre == 0) ans += Splay_tree.val[suf] - x;
else if (suf == n + 1) ans += x - Splay_tree.val[pre];
else ans += min(x - Splay_tree.val[pre], Splay_tree.val[suf] - x);
Splay_tree.Insert(root, x, 0);
}
printf("%d\n", ans);
}

[BZOJ1588]营业额统计的更多相关文章

  1. p2234&bzoj1588 营业额统计

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

  2. BZOJ1588 营业额统计 (Splay)

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

  3. [BZOJ1588]营业额统计(Splay)

    Description 题意:给定 n个数,每给定一个数,在之前的数里找一个与当前数相差最小的数,求相差之和(第一个数为它本身) 如:5 1 2 5 4 6 Ans=5+|1-5|+|2-1|+|5- ...

  4. BZOJ1588: [HNOI2002]营业额统计[BST]

    1588: [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 14151  Solved: 5366[Submit][Sta ...

  5. 营业额统计(bzoj1588)

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

  6. 【BZOJ-1588】营业额统计 Splay

    1588: [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 12485  Solved: 4508[Submit][Sta ...

  7. BZOJ1588 HNOI2002 营业额统计 [Splay入门题]

    [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 4128  Solved: 1305 Description 营业额统计 ...

  8. bzoj1588 [HNOI2002]营业额统计(Treap)

    1588: [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 11485  Solved: 4062[Submit][Sta ...

  9. 【链表】BZOJ1588: [HNOI2002]营业额统计

    1588: [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 17555  Solved: 7179[Submit][Sta ...

随机推荐

  1. python入门知识点(上)

    1.硬件系统: 主机部分: 1.中央处理器(CPU): 电脑的大脑 运算器: 数值计算和逻辑判断 控制器: 可以电脑中的各个部件协同工作 2.内部存储器: 随机存储器:内存条 使用电信号表示数据; 特 ...

  2. Python PyQt5 Pycharm 环境搭建及配置

    PyQt5相关安装 python 版本 python 3.6.3  1.安装PyQt5 执行命令: pip install pyqt5 2.安装PyQt5-tools 执行命令:pip install ...

  3. every day a practice —— morning(5)

    Huawei has not been accused of wrongdoing. As an administrative subpoena, the Treasury document does ...

  4. 【源码分析】Mybatis使用中,同一个事物里,select查询不出之前insert的数据

    一.问题场景模拟问题:第二次查询和第一次查询结果一模一样,没有查询出我新插入的数据 猜测:第二次查询走了Mybatis缓存 疑问:那为什么会走缓存呢? 1.service方法 @Override @T ...

  5. Okhttp 插入缓存拦截器 解析

    我们在做网络请求的时候,如果网络请求过于频繁而且请求的数据变动不大,或者基本没有变动,这个时候如果没有缓存功能,我们想一下 会浪费掉多少资源,一次请求刷新一次,去请求一次,不但会消耗用户的流量,而且还 ...

  6. Confluence 6 空间标识

    每一个 Confluence 空间都有一个 空间标识(space key),这个空间标识是简短并且是唯一的,这个标识被用来构建到空间的 URL 中. 当你创建一个站点空间,Confluence 将会为 ...

  7. 数据库SQLserver(课本)

    一.SQL server的部署 1.数据库的基本概念 数据库通常是一个由行和列组成的二维表 数据表中的行通常叫做记录或元祖 数据表中的列通常叫做字段或属性 2.主键和外键 主键:定义主键可以保证数据的 ...

  8. Vladik and cards CodeForces - 743E (状压)

    大意: 给定序列, 求选出一个最长的子序列, 使得任选两个[1,8]的数字, 在子序列中的出现次数差不超过1, 且子序列中相同数字连续. 正解是状压dp, 先二分转为判断[1,8]出现次数>=x ...

  9. 关于*[pylint]E1101:Module 'xxx' has no 'xxx' member* 简单而有效的解决办法

    关于 pylint 的 *E1101* 错误: 概念: %s %r has no %r member Function %r has no %r member Variable %r has no % ...

  10. 【IDEA】【7】Git更新及提交

    如果是Git管理的项目,顶部会出现这样的按钮 绿色代表commit到本地 蓝色代表update最新代码 Push:推送到远程服务器:右键项目->Git->Repository->Pu ...