[BZOJ1588]营业额统计
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]营业额统计的更多相关文章
- p2234&bzoj1588 营业额统计
传送门 题目 营业额统计 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成立以来每天的营业额 ...
- BZOJ1588 营业额统计 (Splay)
营业额统计 营业额统计 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成立以来每天的营业额. ...
- [BZOJ1588]营业额统计(Splay)
Description 题意:给定 n个数,每给定一个数,在之前的数里找一个与当前数相差最小的数,求相差之和(第一个数为它本身) 如:5 1 2 5 4 6 Ans=5+|1-5|+|2-1|+|5- ...
- BZOJ1588: [HNOI2002]营业额统计[BST]
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 14151 Solved: 5366[Submit][Sta ...
- 营业额统计(bzoj1588)
Description 营业额统计 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成立以来每 ...
- 【BZOJ-1588】营业额统计 Splay
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 12485 Solved: 4508[Submit][Sta ...
- BZOJ1588 HNOI2002 营业额统计 [Splay入门题]
[HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 4128 Solved: 1305 Description 营业额统计 ...
- bzoj1588 [HNOI2002]营业额统计(Treap)
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 11485 Solved: 4062[Submit][Sta ...
- 【链表】BZOJ1588: [HNOI2002]营业额统计
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 17555 Solved: 7179[Submit][Sta ...
随机推荐
- 学习笔记36—坚果云 | Papership或Zotero使用webDAV验证服务器不成功怎么办?
很多人都喜欢用坚果云作为Zotero的第三方云盘,从而无限扩展Zotero的存储空间.可是大家在Papership或zotero客户端中验证坚果云webDAV服务器时,会出现验证不成功的问题,相信这个 ...
- Codeforces 932D - Tree
932D - Tree 思路: 树上倍增 anc[i][u]:u的2^i祖先 mx[i][u]:u到它的2^i祖先之间的最大值,不包括u pre[i][u]:以u开始的递增序列的2^i祖先 sum[i ...
- BGP - 3,BGP重要概念(EBGP,IBGP,防环/黑洞/全互连/同步)
1,防环/黑洞/同步/全互连(为出现大于号,现在通常都是要下一跳可达+关同步) a)EBGP邻居传来的路由可以通过AS_PATH防环,所以收到的不会有问题,因此直接是优化的(>),也就是直接装表 ...
- LeetCode--844--比较含退格的字符串(java)
给定 S 和 T 两个字符串,当它们分别被输入到空白的文本编辑器后,判断二者是否相等,并返回结果. # 代表退格字符. 示例 1: 输入:S = "ab#c", T = " ...
- 通过ambari安装hadoop集群,ZT
通过ambari安装hadoop集群,ZT http://www.cnblogs.com/cenyuhai/p/3295635.html http://www.cnblogs.com/cenyuhai ...
- vux, vue如何控制微信自带的返回按钮,让其返回其他页面?
<script> import { mapState } from 'vuex' export default{ name: 'clockFx', data () { return { } ...
- python模块安装报错大全
报错 环境 解决 手动安装pip install mysqlclient 报错: _mysql.c(29) : fatal error C1083: Cannot open include file: ...
- hash、hashchange事件
1.hash即URL中"#"字符后面的部分. ①使用浏览器访问网页时,如果网页URL中带有hash,页面就会定位到id(或name)与hash值一样的元素的位置: ②hash还有另 ...
- reflow 和 repaint
Reflow(渲染):对于DOM结构中的各个元素都有自己的盒模型,浏览器根据各种样式(浏览器的.开发人员定义的等)来计算,并根据计算结果将元素放到它该出现的位置,这个过程称之为reflow. refl ...
- python-跨域问题
跨域:因为浏览器的同源策略,在你请求返回的时候会进行拦截 jsonp 只能发 get 请求 cors 可以发任何请求 ,在响应时加个响应头就行 同源策略对ajax阻拦 同源策略对src或href属性的 ...