[HNOI2002]营业额统计 Splay tree入门题
题目连接:http://www.lydsy.com/JudgeOnline/problem.php?id=1588
1588: [HNOI2002]营业额统计
Time Limit: 5 Sec Memory Limit: 162 MB
Submit: 6068
Solved: 1981
[
Submit][
Status]
Description
Input
Output
Sample Input
5
1
2
5
4
6
Sample Output
#include<stdio.h>
#include<string.h>
const int N=100100;
const int inf=0x3fffffff;
#define min(a,b) (a>b?b:a)
int root,tot;
struct Tree
{
int key,son[2],father;
}T[N];//son[0]左儿子,son[1]右儿子
void newnode(int &r,int w,int fa)//建新节点
{
r=++tot;
T[r].key=w;T[r].father=fa;
T[r].son[0]=T[r].son[1]=0;
}
void Rotate(int r,int kind)//旋转,kind为1是右旋,为0是左旋
{
int y=T[r].father;
T[y].son[!kind]=T[r].son[kind];
T[T[r].son[kind]].father=y;
if(T[y].father)
T[T[y].father].son[T[T[y].father].son[1]==y]=r;
T[r].father=T[y].father;
T[r].son[kind]=y;
T[y].father=r;
}
//Splay调整,将结点r调整到goal下面(以r为根节点)
void Splay(int r,int goal)
{
int y;
while(T[r].father!=goal)
{
y=T[r].father;
if(T[y].father==goal)
Rotate(r,T[y].son[0]==r);
else
{
int kind=T[T[y].father].son[0]==y;
if(T[y].son[kind]==r)
{
Rotate(r,!kind);
Rotate(r,kind);
}
else
{
Rotate(y,kind);
Rotate(r,kind);
}
}
}
if(goal==0)root=r;//更新根节点
}
bool insert(int w)
{
int r=root;
while(T[r].son[w>T[r].key])
{
if(T[r].key==w)//如果w已经在树中
{
Splay(r,0);
return false;
}
r=T[r].son[w>T[r].key];
}
newnode(T[r].son[w>T[r].key],w,r);
Splay(T[r].son[w>T[r].key],0);
return true;
}
int get_pre(int r)//在左子树中找到最大的值(找前驱)
{
int temp=T[r].son[0];
if(temp==0)return inf;
while(T[temp].son[1])
temp=T[temp].son[1];
return T[r].key-T[temp].key;
}
int get_next(int r)//在右子树中找到最小的值(找后继)
{
int temp=T[r].son[1];
if(temp==0)return inf;
while(T[temp].son[0])
temp=T[temp].son[0];
return T[temp].key-T[r].key;
}
int main()
{
int n,w,sum,j;
while(scanf("%d",&n)!=-1)
{
sum=0;root=tot=0;
if(scanf("%d",&w)==EOF)w=0;
sum+=w;
newnode(root,w,0);
for(j=1;j<n;j++)
{
if(scanf("%d",&w)==EOF)w=0;
if(!insert(w))continue;
sum+=min(get_pre(root),get_next(root));
}
printf("%d\n",sum);
}
return 0;
}
[HNOI2002]营业额统计 Splay tree入门题的更多相关文章
- [HNOI2002]营业额统计 Splay tree
Splay tree入门题,学好代码风格,学习HH大牛的,传送门.. #include <functional> #include <algorithm> #include & ...
- 1588: [HNOI2002]营业额统计 (splay tree)
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 5783 Solved: 1859[Submit][Stat ...
- BZOJ1588 HNOI2002 营业额统计 [Splay入门题]
[HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 4128 Solved: 1305 Description 营业额统计 ...
- Bzoj 1588: [HNOI2002]营业额统计(splay)
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MB Description 营业额统计 Tiger最近被公司升任为营业部经理,他上 ...
- BZOJ1588 [HNOI2002]营业额统计 splay模板
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MB Submit: 16189 Solved: 6482 [Submit][S ...
- bzoj1588: [HNOI2002]营业额统计(splay)
1588: [HNOI2002]营业额统计 题目:传送门 题解: 复习splay所以来刷个水... 题目描述不是特别清楚:应该是找第i天以前一个最小的营业额和第i天做差的最小值作为第i天的最小波动值 ...
- 洛谷P2234 [HNOI2002] 营业额统计 [splay]
题目传送门 营业额统计 题目描述 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成立以来每天 ...
- NOIP 营业额统计 splay tree 纯模板
2924: 营业额统计 Time Limit(Common/Java):1000MS/3000MS Memory Limit:65536KByteTotal Submit: 389 ...
- HNOI2002 营业额统计 [Splay]
题目描述 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成立以来每天的营业额.分析营业情况是 ...
随机推荐
- 转:LNMP虚拟主机PHP沙盒绕过/命令执行(php exec命令被禁之后)
LNMP虚拟主机PHP沙盒绕过/命令执行 lnmp更新1.2版本,很多东西都升级了,很棒.不过还是发现一个BUG. LNMP是一款linux下nginx.php.mysql一键安装包. 下载:http ...
- Wannafly挑战赛7 C - 小Q与氪金游戏
题目描述 “为世界上所有的美好而战!”小Q同学最近沉迷“稳固3”,为了从最新的蛋池中抽出自己喜欢的角色卡,不惜氪下重金.在这个游戏中,氪一单可以得到x个宝石,而抽一次卡需要花费y个宝石,由于游戏策划十 ...
- 利用linux判断elf文件是64位还是32位
readelf 命令,参数为-h 例如 文件名为python >>>readelf -h python 得到的是ELF Header中的项Magic 第五个数 02时为64位,01时 ...
- AFF镜像工具集afflib-tools
AFF镜像工具集afflib-tools Advanced Forensic Format(AFF)是一种开源免费的磁盘镜像格式.作为磁盘数字取证的三大格式之一,AFF提供数字取证的各项功能,如签 ...
- 【CF contest/792/problem/E】
E. Colored Balls time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- android studio 可以汉化的 文件 地方
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha 汉化包 百度云盘 下载地址:https://pan.baidu.com/s/1pLjwy ...
- BZOJ 4276: [ONTAK2015]Bajtman i Okrągły Robin
最大权值匹配,贪心匈牙利即可. 检查一些人是否能被全部抓住可以采用左端点排序,右端点优先队列处理. By:大奕哥 #include<bits/stdc++.h> using namespa ...
- BZOJ1002輪狀病毒 暴搜 + 找規律 + 高精度
@[暴搜, 找規律, 高精度] Description 轮状病毒有很多变种,所有轮状病毒的变种都是从一个轮状基产生的.一个\(n\)轮状基由圆环上\(n\)个不同的基原子和圆心处一个核原子构成的,2个 ...
- hdu 4112 Break the Chocolate 贪心
Break the Chocolate Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem. ...
- 如何监控ActiveMQ
如何监控ActiveMQ在ActiveMQ 4.x中你可以监控代理并查看使用了哪些目标,和连接一起的活动和订阅使用下面的工具进行查看: JMX和JMX控制台,如jConsole http://java ...