平衡二叉树---Shaolin
Description
When a young man passes all the tests and is declared a new monk of Shaolin, there will be a fight , as a part of the welcome party. Every monk has an unique id and a unique fighting grade, which are all integers. The new monk must fight with a old monk whose fighting grade is closest to his fighting grade. If there are two old monks satisfying that condition, the new monk will take the one whose fighting grade is less than his.
The master is the first monk in Shaolin, his id is 1,and his fighting grade is 1,000,000,000.He just lost the fighting records. But he still remembers who joined Shaolin earlier, who joined later. Please recover the fighting records for him.
Input
In each test case:
The first line is a integer n (0 <n <=100,000),meaning the number of monks who joined Shaolin after the master did.(The master is not included).Then n lines follow. Each line has two integer k and g, meaning a monk's id and his fighting grade.( 0<= k ,g<=5,000,000)
The monks are listed by ascending order of jointing time.In other words, monks who joined Shaolin earlier come first.
The input ends with n = 0.
Output
Sample Input
2 1
3 3
4 2
0
Sample Output
3 2
4 2
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
struct data
{
int l,r,v,vo;
int rnd;
}tr[];
int size,root,ans1,ans2;///定义全局整型变量默认初值为0; void rturn(int &k)
{
int t=tr[k].l;
tr[k].l=tr[t].r;
tr[t].r=k;
k=t;
} void lturn(int &k)
{
int t=tr[k].r;
tr[k].r=tr[t].l;
tr[t].l=k;
k=t;
} void insert(int &k,int x,int xo)
{
if(k==)
{
size++;///记录已经使用的结构体数目;
k=size;
tr[k].v=x;
tr[k].vo=xo;
tr[k].rnd=rand();
return;
}
if(x>tr[k].v)
{
insert(tr[k].r,x,xo);
if(tr[tr[k].r].rnd<tr[k].rnd)
lturn(k);
}
else
{
insert(tr[k].l,x,xo);
if(tr[tr[k].l].rnd<tr[k].rnd)
rturn(k);
}
} void query_pro(int k,int x)///求x的前驱(前驱定义为小于x,且最大的数);
{
if(k==)return;
if(tr[k].v<x)
{
ans1=k;
query_pro(tr[k].r,x);
}
else query_pro(tr[k].l,x);
} void query_sub(int k,int x)///求x的后继(后继定义为大于x,且最小的数);
{
if(k==)return;
if(tr[k].v>x)
{
ans2=k;
query_sub(tr[k].l,x);
}
else query_sub(tr[k].r,x);
} int main()
{
int n,xo,x;
while(scanf("%d",&n)!=EOF&&n)
{
root=;
size=;
for(int i=;i<;i++)
{
tr[i].l=;
tr[i].r=;
tr[i].v=;
tr[i].vo=;
tr[i].rnd=;
}
insert(root,,);
while(n--)
{
scanf("%d %d",&xo,&x);
insert(root,x,xo);
ans1=;
ans2=;
query_pro(root,x);
query_sub(root,x);
if(ans1==) printf("%d %d\n",xo,tr[ans2].vo);\
else
{
if(x-tr[ans1].v<=tr[ans2].v-x)
printf("%d %d\n",xo,tr[ans1].vo);
else printf("%d %d\n",xo,tr[ans2].vo);
}
}
}
return ;
}
平衡二叉树---Shaolin的更多相关文章
- 算法与数据结构(十一) 平衡二叉树(AVL树)
今天的博客是在上一篇博客的基础上进行的延伸.上一篇博客我们主要聊了二叉排序树,详情请戳<二叉排序树的查找.插入与删除>.本篇博客我们就在二叉排序树的基础上来聊聊平衡二叉树,也叫AVL树,A ...
- [LeetCode] Balanced Binary Tree 平衡二叉树
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- Java数据结构——平衡二叉树的平衡因子(转自牛客网)
若向平衡二叉树中插入一个新结点后破坏了平衡二叉树的平衡性.首先要找出插入新结点后失去平衡的最小子树根结点的指针.然后再调整这个子树中有关结点之间的链接关系,使之成为新的平衡子树.当失去平衡的最小子树被 ...
- 【数据结构】平衡二叉树—AVL树
(百度百科)在计算机科学中,AVL树是最先发明的自平衡二叉查找树.在AVL树中任何节点的两个子树的高度最大差别为一,所以它也被称为高度平衡树.查找.插入和删除在平均和最坏情况下都是O(log n).增 ...
- 平衡二叉树AVL删除
平衡二叉树的插入过程:http://www.cnblogs.com/hujunzheng/p/4665451.html 对于二叉平衡树的删除采用的是二叉排序树删除的思路: 假设被删结点是*p,其双亲是 ...
- 平衡二叉树AVL插入
平衡二叉树(Balancedbinary tree)是由阿德尔森-维尔斯和兰迪斯(Adelson-Velskiiand Landis)于1962年首先提出的,所以又称为AVL树. 定义:平衡二叉树或为 ...
- 数据结构快速回顾——平衡二叉树 AVL (转)
平衡二叉树(Balanced Binary Tree)是二叉查找树的一个进化体,也是第一个引入平衡概念的二叉树.1962年,G.M. Adelson-Velsky 和 E.M. Landis发明了这棵 ...
- LeetCode——Balanced Binary Tree(判断是否平衡二叉树)
问题: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bin ...
- 数据结构之平衡二叉树(AVL树)
平衡二叉树(AVL树)定义如下:平衡二叉树或者是一棵空树,或者是具有以下性质的二叉排序树: (1)它的左子树和右子树的高度之差绝对值不超过1: (2)它的左子树和右子树都是平衡二叉树. AVL树避免了 ...
随机推荐
- Net框架下-ORM框架LLBLGen的简介
>对于应用程序行业领域来说,涉及到Net框架的,在众多支持大型项目的商用ORM框架中,使用最多的目前了解的主要有三款: 1.NHibernate(从Java版移植来的Net版). 2.微软的EF ...
- Sencha Touch+PhoneGap打造超级奶爸之喂养记(一) 源码免费提供
起源 非常高兴我的宝宝健康平安的出生了.对于初次做奶爸的我,喜悦过后,面临着各中担心,担心宝宝各项指标是否正常.最初几天都是在医院待着,从出生那一天开始,护士妹妹隔一段时间就会来问宝宝的喂奶,大小便, ...
- sql2008清空日志
USE[master] GO ALTER DATABASE MeSizeSNS SET RECOVERY SIMPLE WITH NO_WAIT GO ALTER DATABASE MeSizeSNS ...
- 使用word和pdf进行仿书编辑的经验
一.问题的提出: 一本书扫描好,要将书中的图片转换为文字版的word文档.二.问题的分析: 1.文字的提取 2.文字的编排三.问题的解决 1.如果用的是Adobe Acroba ...
- 读书笔记_Effective_C++_条款四十六:需要类型转换时请为模板定义非成员函数
这个条款可以看成是条款24的续集,我们先简单回顾一下条款24,它说了为什么类似于operator *这样的重载运算符要定义成非成员函数(是为了保证混合乘法2*SomeRational或者SomeRat ...
- C#基础课程之六(临时表)DataTable使用方法
DataTable 用法:赋取值操作,及报错情况 dataTable.Columns.Add("Name"); //Columns 对象获取该集合的全部列,添加列名. 默认stri ...
- sp_executesql 两种写法
写法1: AlTER PROCEDURE TryAgain @ReturnValue int output AS declare @aa nvarchar(), @ForumID int, @coun ...
- c# 时间戳转换为Datetime类型的时间
private static DateTime GetConvertTime(long ltime) { DateTime dt_time=new DateTime(1970,1,1,8,0,0,0) ...
- 修复AWS上EC2损坏的sshd_config文件
常识: AWS是没有root用户的,登陆也都是通过SSH KEY完成授权认证. 背景: 正在AWS上搭一个CI (GO),与gitlab,为了将其进行集成,需将gitlab的deploy key设置成 ...
- Python开源框架Scrapy安装及使用
一.安装问题 环境: CentOS + Python 2.7 + Pip 1) 安装时遇到 ”UnicodeDecodeError: 'ascii' codec can't decode byte ...