HNOI2002
对于当天的营业额,你要找到之前的数和他差的绝对值最小的和。由于这个是不断在插入的,所以用伸展树来维护。
http://www.lydsy.com/JudgeOnline/problem.php?id=1588
照着kuangbin大牛的代码敲下来的。
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define INF 99999999
#define ll __int64
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
const int MAXN = ;
int pre[MAXN],key[MAXN],ch[MAXN][],root,tot;
//父亲结点 键值 左右子树 根节点 节点数量
int n;
void Newnode(int &rt,int pa,int k)
{
rt = ++tot;
pre[rt] = pa;
key[rt] = k;
ch[rt][] = ch[rt][] = ;//空
} void Rotate(int x,int kind)
{
int y = pre[x];
ch[y][!kind] = ch[x][kind];
pre[ch[x][kind]] = y;
if(pre[y]){
ch[pre[y]][ch[pre[y]][]==y] = x;
}
pre[x] = pre[y];
ch[x][kind] = y;
pre[y] = x;
} //将结点rt 调整到goal下面
void splay(int rt,int goal)
{
while(pre[rt] != goal)
{
if(pre[pre[rt]] == goal)
Rotate(rt,ch[pre[rt]][]==rt);
else {
int y = pre[rt];
int kind = (ch[pre[y]][]==y);
if(ch[y][kind] == rt){//rt 和 pre[rt]不在同一方向上
Rotate(rt,!kind); //kind=1时,说明rt在y的右边 而y在pre[y]的左边
Rotate(rt,kind);
}
else {
Rotate(y,kind);
Rotate(rt,kind);
}
}
}
if(goal == )
root = rt;
} int Insert(int k)
{
int rt = root;
while(ch[rt][key[rt]<k]){//如果key[r]<k,表明当前k在右边,不然去左边
if(key[rt] == k){
splay(rt,);
return ;
}
rt = ch[rt][key[rt]<k];
}
Newnode(ch[rt][key[rt]<k],rt,k);
splay(ch[rt][key[rt]<k],);
return ;
} int get_pre(int rt)
{
int tmp = ch[rt][];
if(tmp == )
return INF;
while(ch[tmp][])
{
tmp = ch[tmp][];
}
return key[rt] - key[tmp];
} int get_next(int rt)
{
int tmp = ch[rt][];
if(tmp == )
return INF;
while(ch[tmp][])
{
tmp = ch[tmp][];
}
return key[tmp] - key[rt];
}
int main()
{
int i,j;
while(~scanf("%d",&n))
{
root = tot = ;
int ans = ;
for(i=; i<=n; i++){
int num;
if(scanf("%d",&num)==EOF)num = ;
if(i == ){
ans += num;
Newnode(root,,num);
continue;
}
if(Insert(num) == )
continue;
int a = get_pre(root);
int b = get_next(root);
//cout<<a<<" "<<b<<endl;
ans += min(a,b);
}
printf("%d\n",ans);
}
}
HNOI2002的更多相关文章
- BZOJ1588: [HNOI2002]营业额统计[BST]
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 14151 Solved: 5366[Submit][Sta ...
- BZOJ 1588: [HNOI2002]营业额统计
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 14396 Solved: 5521[Submit][Sta ...
- BZOJ 1588: [HNOI2002]营业额统计 双向链表 / splay / treap
1588: [HNOI2002]营业额统计 Description 营业额统计 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger ...
- BZOJ1588 HNOI2002 营业额统计 [Splay入门题]
[HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 4128 Solved: 1305 Description 营业额统计 ...
- [BZOJ1220][POJ1091][HNOI2002]跳蚤
[BZOJ1220][POJ1091][HNOI2002]跳蚤 试题描述 Z城市居住着很多只跳蚤.在Z城市周六生活频道有一个娱乐节目.一只跳蚤将被请上一个高空钢丝的正中央.钢丝很长,可以看作是无限长. ...
- bzoj 1588: [HNOI2002]营业额统计 treap
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 13902 Solved: 5225[Submit][Sta ...
- BZOJ_1588_&_Codevs_1296_[HNOI2002]_营业额统计(平衡树/set)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1588 给出每一天的营业值,求出之前的天当中与它相差最小的营业值与它的差的绝对值(第一天的差值为 ...
- bzoj 1223: [HNOI2002]Kathy函数 数位DP 高精度
1223: [HNOI2002]Kathy函数 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 207 Solved: 90[Submit][Stat ...
- 数据结构:(平衡树,链表)BZOJ 1588[HNOI2002]营业额统计
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 12173 Solved: 4354[Submit][Sta ...
- bzoj1588 [HNOI2002]营业额统计(Treap)
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 11485 Solved: 4062[Submit][Sta ...
随机推荐
- 测试思考[持续更新ing]
1.如何设计好的测试用例,提高测试覆盖率?提高测试效率? 2.如何展现测试成果? 3.如何编写专业化.优质的测试文档? 4.如何适时借助测试工具,测试一些重复性比较高的测试? 5.如何引入自动化测试? ...
- Vector3D - AS3
Vector3D 类使用笛卡尔坐标 x.y 和 z 表示三维空间中的点或位置.与在二维空间中一样,x 属性表示水平轴,y 属性表示垂直轴.在三维空间中,z 属性表示深度.当对象向右移动时,x 属性的值 ...
- web安全测试工具的局限性
讨论安全漏洞的原理,谈谈工具的局限. 先说下扫描工具的原理: 扫描工具可以看做由两部分组成:爬虫+校验机构.爬虫的作用是搜集整个被采集对象的链接,然后校验机构对这些链接逐一进行验证. 说扫描工具的局限 ...
- Eclipse打开xml文件报校验错误解决办法
XML文件在Eclipse中报校验错误: The content of element type "web-app" must match "(icon?,display ...
- java 16-2 ArrayList的练习2
需求:去除集合中自定义对象的重复值(对象的成员变量值都相同 注意: 我们按照和字符串一样的操作,发现出问题了. 为什么呢? 我们必须思考哪里会出问题? 通过简单的分析,我们知道问题出现在了判断上. ...
- xcode插件XAlign
一款十分强大的自定义对齐模式插件 开源地址:https://github.com/qfish/XAlign/ 终端输入命令: $ curl http://qfi.sh/XAlign/build/ins ...
- 24Mybatis_延迟加载——用association来实现
resultMap可以实现高级映射(使用association.collection实现一对一及一对多映射),association.collection具备延迟加载功能. 需求: 如果查询订单并且关 ...
- C# 通过消息捕获处理窗体最大化/最小化
通过以下的一些代码可以实现捕获相关的一些消息事件; 以及可以通过调用 SetCloseMenu();实现关闭一些按钮功能如屏蔽关闭按钮功能等; 需要添加命名空间:using System.Runtim ...
- [vm]exsi的名词
早上想了想高可用.昨晚想学学虚拟vmware的一些东西. 物理机---exsi---vm 物理机-win7-vmware-vm 服务器高可用: 一 名词 1,虚拟网络 2,虚拟交换机 3,vm ...
- ntp时间同步服务器配置
ntp同步的两种方式:1.使用ntpdate命令直接同步 2.使用NTPD服务平滑同步直接同步方式的缺陷:会导致已经做的定时任务再做一遍.平滑同步每次同步时间的偏移量不会太陡,根据偏移量,均方差等值每 ...