Description

Shaolin temple is very famous for its Kongfu monks.A lot of young men go to Shaolin temple every year, trying to be a monk there. The master of Shaolin evaluates a young man mainly by his talent on understanding the Buddism scripture, but fighting skill is also taken into account. 
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

There are several test cases. 
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

A fight can be described as two ids of the monks who make that fight. For each test case, output all fights by the ascending order of happening time. Each fight in a line. For each fight, print the new monk's id first ,then the old monk's id.
 

Sample Input

3
2 1
3 3
4 2
0
 

Sample Output

2 1
3 2
4 2
 
题意:有n个新加入的和尚和一个开始的大和尚,大和尚编号为1,攻击力1000,000,000  其它n个和尚编号和攻击力各不一样,输入流中按时间顺序给出了加入的和尚的编号和攻击力,每个新加入的和尚会找一个和他攻击力最接近的已加入的和尚比试,若有两个和尚和这个新和尚的差值相同,攻击力小的和他比试,输出新和尚的编号和与他比试的和尚的编号。
 
思路:使用平衡二叉树的算法,方便查找x值的前驱与后继。
 
代码如下:
#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的更多相关文章

  1. 算法与数据结构(十一) 平衡二叉树(AVL树)

    今天的博客是在上一篇博客的基础上进行的延伸.上一篇博客我们主要聊了二叉排序树,详情请戳<二叉排序树的查找.插入与删除>.本篇博客我们就在二叉排序树的基础上来聊聊平衡二叉树,也叫AVL树,A ...

  2. [LeetCode] Balanced Binary Tree 平衡二叉树

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  3. Java数据结构——平衡二叉树的平衡因子(转自牛客网)

    若向平衡二叉树中插入一个新结点后破坏了平衡二叉树的平衡性.首先要找出插入新结点后失去平衡的最小子树根结点的指针.然后再调整这个子树中有关结点之间的链接关系,使之成为新的平衡子树.当失去平衡的最小子树被 ...

  4. 【数据结构】平衡二叉树—AVL树

    (百度百科)在计算机科学中,AVL树是最先发明的自平衡二叉查找树.在AVL树中任何节点的两个子树的高度最大差别为一,所以它也被称为高度平衡树.查找.插入和删除在平均和最坏情况下都是O(log n).增 ...

  5. 平衡二叉树AVL删除

    平衡二叉树的插入过程:http://www.cnblogs.com/hujunzheng/p/4665451.html 对于二叉平衡树的删除采用的是二叉排序树删除的思路: 假设被删结点是*p,其双亲是 ...

  6. 平衡二叉树AVL插入

    平衡二叉树(Balancedbinary tree)是由阿德尔森-维尔斯和兰迪斯(Adelson-Velskiiand Landis)于1962年首先提出的,所以又称为AVL树. 定义:平衡二叉树或为 ...

  7. 数据结构快速回顾——平衡二叉树 AVL (转)

    平衡二叉树(Balanced Binary Tree)是二叉查找树的一个进化体,也是第一个引入平衡概念的二叉树.1962年,G.M. Adelson-Velsky 和 E.M. Landis发明了这棵 ...

  8. LeetCode——Balanced Binary Tree(判断是否平衡二叉树)

    问题: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bin ...

  9. 数据结构之平衡二叉树(AVL树)

    平衡二叉树(AVL树)定义如下:平衡二叉树或者是一棵空树,或者是具有以下性质的二叉排序树: (1)它的左子树和右子树的高度之差绝对值不超过1: (2)它的左子树和右子树都是平衡二叉树. AVL树避免了 ...

随机推荐

  1. Cubieboard2裸机开发之(五)看门狗操作

    前言 说到看门狗,应该不会陌生,看门狗说白了就是一个定时器,但是它有一个非常重要的功能就是复位系统.在A20里,看门狗的操作非常简单,只有两个寄存器,不需要操作时钟相关的东西,系统起来后可以直接使用, ...

  2. 基情四射的两个css样式

    自定义blog样式时,代码段的line-height继承样式post的line-height,间隔太大了,决定再减小点,css都玩了几年了,感觉中这是很容易的事情.然后,就悲剧了好久,原先自定义样式表 ...

  3. android一键分享功能不使用任何第三方sdk

    在android中有自带的一键分享功能,不过它会把所有带分享的应用都找出来,如果我们只需要一些常见的分享应用,该如何做呢? 下面看我的效果图(横屏和竖屏自动适配): 接下来看我的调用(支持图片和文字分 ...

  4. 使用 Portable Class Library(可移植类库)开发 Universal Windows App

    今天在这里跟大家聊聊关于 Windows Universal 应用夸平台的问题,首先Universal Windows App的定义相信大家已经有所了解了(如果你是一个刚刚接触 Universal A ...

  5. 免费国内外"代码托管服务器"收集

      国内 开源中国  http://git.oschina.net/  支持git 淘宝code  http://code.taobao.org/  支持svn 京东code  https://cod ...

  6. ops中set_sysclk set_clkdiv set_pll详解

    在看Alsa soc驱动的是时候,在snd_soc_dai_driver.ops中有3个字段 .set_sysclk .set_pll .set_clkdiv 开始的时候,总是晕头转向,感觉这3个回调 ...

  7. udp穿透简单讲解和实现(Java)

    在上一小节中了解到了通过浏览器自带的Webrtc功能来实现P2P视频聊天.在HTML5还没有普及和制定Webrtc标准的前提下,如果要在手机里进行视频实时对话等包括其他功能的话,还是要自己实现,还比较 ...

  8. 初识js中的闭包

    今天看了关于js闭包方面的文章,还是有些云里雾里,对于一个菜鸟来说,学习闭包确实有一定的难度,不说别的,能够在网上找到一篇优秀的是那样的不易. 当然之所以闭包难理解,个人觉得是基础知识掌握的不牢,因为 ...

  9. AngularJS 监控对象属性:$watch和$digest

    监控对象属性:$watch和$digest $watch和$digest是相辅相成的.两者一起,构成了Angular作用域的核心:数据变化的响应.如果你学习过WPF,并基于WPF使用过一些MVVM框架 ...

  10. DDD:关于模型的合法性,Entity.IsValid()合理吗?

    背景 见过很多框架(包括我自己的)都会在实体的定义中包含一个IsValid()方法,用来判断实体的合法性,是否应该这样设计呢?本文就这个问题介绍一点想法,希望大家多批评. 实体能否处于“非法”状态? ...