[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 ...
随机推荐
- Lua面向对象之二:类继承
1.类继承 ①代码 Sharp = { } --① 父类 function Sharp:new() local new_sharp = { } self.__index = self --②,self ...
- Codeforces 797E - Array Queries
E. Array Queries 题目链接:http://codeforces.com/problemset/problem/797/E time limit per test 2 seconds m ...
- (转)C# Textbox的ImeMode取值对中文输入法的影响
取值 五笔加加 微软拼音3.0 搜狗拼音 说明 NoControl 首次调出后按一次ctrl+space才能正确使用 中西标点或全半角字符继承上次设置 调出后默认为英文输入状态 调出后默认为西文标点 ...
- HTML 第九章总结
前言 这一章节主要讲了关于 HTML 中关于留白的知识:在这一章节中,从大到小,有: margin border padding context 这四个部分. 关于margin和padding mar ...
- 雷林鹏分享:C# 判断
C# 判断 判断结构要求程序员指定一个或多个要评估或测试的条件,以及条件为真时要执行的语句(必需的)和条件为假时要执行的语句(可选的). 下面是大多数编程语言中典型的判断结构的一般形式: 判断语句 C ...
- 怎么检测自己fastq的Phred类型 | phred33 phred64
http://wiki.bits.vib.be/index.php/Identify_the_Phred_scale_of_quality_scores_used_in_fastQ # S - San ...
- python try 异常处理 史上最全
在程序出现bug时一般不会将错误信息显示给用户,而是现实一个提示的页面,通俗来说就是不让用户看见大黄页!!! 有时候我们写程序的时候,会出现一些错误或异常,导致程序终止. 为了处理异常,我们使用try ...
- 机器学习基石(台湾大学 林轩田),Lecture 2: Learning to Answer Yes/No
上一节我们跟大家介绍了一个具体的机器学习的问题,以及它的内容的设定,我们今天要继续下去做什么呢?我们今天要教大家说到底我们怎么样可以有一个机器学习的演算法来解决我们上一次提到的,判断银行要不要给顾客信 ...
- Python中字典和集合的用法
本人开始学习python 希望能够慢慢的记录下去 写下来只是为了害怕自己忘记. python中的字典和其他语言一样 也是key-value的形式 利用空间换时间 可以进行快速的查找 key 是唯一的 ...
- android -------- 混淆打包报错(warning - InnerClass annotations are missing corresponding EnclosingMember annotations)
最近做Android混淆打包遇到一些问题,Android Sdutio 3.1 版本打包的 错误如下: Android studio warning - InnerClass annotations ...