[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拿出了公司的账本,账本上记录了公司成立以来每天的营业额.分析营业情况是 ...
随机推荐
- shell动画
在印象中,好像终端就是黑白界面,加扁平输出.是不是很乏味?其实现在 Linux/Unix 系统中带的终端模拟器是支持动画和彩色输出的.下面,一起来看看字符界面下的动画魅力! 1 定点输出 1.1 回车 ...
- USACO 控制公司 Controlling Companies
友情链接神犇520的博客 题目: 题目描述 有些公司是其他公司的部分拥有者,因为他们获得了其他公司发行的股票的一部分.(此处略去一句废话)据说,如果至少满足了以下三个条件之一,公司A就可以控制公司B了 ...
- scrapy抓取拉勾网职位信息(八)——使用scrapyd对爬虫进行部署
上篇我们实现了分布式爬取,本篇来说下爬虫的部署. 分析:我们上节实现的分布式爬虫,需要把爬虫打包,上传到每个远程主机,然后解压后执行爬虫程序.这样做运行爬虫也可以,只不过如果以后爬虫有修改,需要重新修 ...
- Linux命令之gdisk
gdisk -l [设备] gdisk又叫GPT fdisk,算是fdisk的延伸吧,主要使用的是GPT分区类型,用来划分容量大于2T的硬盘. 扩展1:分区类型GPT和MBR.GPT最大支持18EB( ...
- PlayMaker的特殊事件FINISHED
PlayMaker的特殊事件FINISHED 在PlayMaker中,每个状态机都有一个特殊事件START.当启用状态机,触发START事件.其中,每个状态都可以有一个特殊事件FINISHED.当 ...
- hdu 1690 Bus System (有点恶心)
Problem Description Because of the huge population of China, public transportation is very important ...
- 安卓 listView 优化
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha 韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_m ...
- linux命令: Netstat
在Internet RFC标准中,Netstat的定义是: Netstat是在内核中访问网络及相关信息的程序,它能提供TCP连接,TCP和UDP监听,进程内存管理的相关报告. Netstat是控 ...
- 'NSUnknownKeyException', reason:....etValue:forUndefinedKey:]: this class is not key value coding-compliant for the key
erminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<MainTableViewControl ...
- OSChina.net 的 Tomcat 配置 server.xml 参考
这是目前 oschina.net 正在使用的 tomcat 的 server.xml 的配置文件内容 <Server port="9005" shutdown="S ...