[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拿出了公司的账本,账本上记录了公司成立以来每天的营业额.分析营业情况是 ...
随机推荐
- 【JavaWeb开发】初步实现网站应用钉钉扫码登录
http://blog.csdn.net/baofeidyz/article/details/59059379 版权声明:转载请注明我的个人微信平台 暴沸 目录(?)[+] 写在前面:如果你还不知道钉 ...
- linux关闭地址空间随机化(ASLR)
转:http://www.xuebuyuan.com/1571079.html 确认ASLR是否已经被打开,"2"表示已经打开 shanks@shanks-ubuntu:/home ...
- Python类总结-描述符__get__(),__set__(),__delete__()
1 描述符是什么:描述符本质就是一个新式类,在这个新式类中,至少实现了__get__(),set(),delete()中的一个,这也被称为描述符协议 get():调用一个属性时,触发 set():为一 ...
- 分布式锁的理解,java自带的锁为什么会失效
前段时间在发送短信的代码块上通过网上找的工具类基于Redis实现了分布式锁的功能 对应的链接https://www.cnblogs.com/c-h-y/p/9391602.html 周末想细细看一下. ...
- [BZOJ4026]dC Loves Number Theory(线段树)
根据欧拉函数的定义式可知,可以先算出a[l]*a[l+1]*...*a[r]的值,然后枚举所有存在的质因子*(p-1)/p. 发现这里区间中一个质因子只要计算一次,所以指计算“上一个同色点在区间外”的 ...
- 去除List中的重复元素
/** * 去重list中的重复元素 * @param list * @return */ public static <T> List<T> removeRepeat(Lis ...
- HTTP状态码,400,404,500,503
HTTP状态码(HTTP Status Code) 一些常见的状态码为: 200 - 服务器成功返回网页 400 服务器不理解请求的语法 404 - 请求的网页不存在 503 - 服务不可用 所有状态 ...
- String是最基本的数据类型吗?
不是, 基本数据类型包括:byte,short,int,long,float,double,boolean,char. 而String是类代表字符串,属于引用类型,所谓引用类型包括:类,接口,数组.. ...
- [转]Intent和IntentFilter详解
Intent Android中提供了Intent机制来协助应用间的交互与通讯,Intent负责对应用中一次操作的动作.动作涉及数据.附加数据进行描述,Android则根据此Intent的描 ...
- PHP 基础函数(四)回调函数
array_walk($arr,'function','words'); 使用用户函数对数组中的每个成员进行处理(第三个参数传递给回调函数function)