[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拿出了公司的账本,账本上记录了公司成立以来每天的营业额.分析营业情况是 ...
随机推荐
- zookeeper分布式算法和部署
算法摘要 安装 配置 监控 创建节点 二阶段提交(Two-Phase Commit) 投票和执行 协调者向参与者发送事务内容,询问是否可以提交,各参与者节点执行事务并向协调者反馈 如果所有参与者反馈y ...
- COMP COMP-3
Comp (Computational) Comp (with no suffix) leaves the choice of the data type to the compiler writer ...
- codeforce 429D. Tricky Function (思维暴力过)
题目描述 Iahub and Sorin are the best competitive programmers in their town. However, they can't both qu ...
- 「THUWC 2017」随机二分图
「THUWC 2017」随机二分图 解题思路 : 首先有一个 \(40pts\) 的做法: 前 \(20pts\) 暴力枚举最终的匹配是怎样的,check一下计算方案数,后 \(20pts\) 令 \ ...
- [JZYZOJ 1288][洛谷 1005] NOIP2007 矩阵取数 dp 高精度
https://www.luogu.org/problem/show?pid=1005 dp好想,高精度练手题,有点不舒服的是前后取数位置的计算,代码量太少才会写题这么慢,noip之前虽然重点放在 ...
- Language Modeling---NLP学习笔记(原创)
本栏目来源于对Coursera 在线课程 NLP(by Michael Collins)的理解.课程链接为:https://class.coursera.org/nlangp-001 1. 语言模型定 ...
- 没有调用PageHelper.startPage()分页方法,最后还是执行了PageHelper分页方法的原因
SELECT * FROM ( SELECT TMP_PAGE.*, ROWNUM ROW_ID FROM ( SELECT * FROM ( SELECT A.*, ROWNUM RN FROM ( ...
- 一次经典的tcp三次握手
TCP报头 在三次握手中使用的字段: 32位序列号 seq:表示的是本次报文发送的数据的第一个字节的序号. 32位确认号:ack 表示期望下一次应该接受到的报文的第一个字节的序号,若ack = N则 ...
- word2007-2010排版中解决段后插入分页符 新页首行空行问题
word2007-2010排版中,很多人都会遇到 这个问题.当你在 段后插入分页符 想开启新的一页的时候,新页首行有个空行.如果删除,会连同分页符一起删除.不删除有影响排版美观.那怎么解决呢: 解决办 ...
- require(): open_basedir restriction in effect. 解决方法
在linux服务器部署thinkphp5的时候PHP报了这个错误, 如下: Warning: require(): open_basedir restriction in effect. File(/ ...