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. java 读取文件路径空格和中文的处理

    应用部署时,发生文件读取错误,发现是部署路径中含有空格的文件夹名,然后把应用服务器位置迁移了. 从网上找到如下方案:1, TestURL().class.getResource("" ...

  2. C++ STL轻松导学

    作为C++标准不可缺少的一部分,STL应该是渗透在C++程序的角角落落里的.STL不是实验室里的宠儿,也不是程序员桌上的摆设,她的激动人心并非昙花一现.本教程旨在传播和普及STL的基础知识,若能借此机 ...

  3. phalcon开发工具(phalcon-devtools)

    一.简介 Phalcon提供的这个开发工具主要是用来辅助开发,比如生成一些程序的基本框架,生成控制器模型等.使用这个工具我们只需要一个简单的命令即可生成应用的基本框架. 二.下载 github: ht ...

  4. Android之NDK开发

    转自:http://www.cnblogs.com/devinzhang/archive/2012/02/29/2373729.html 一.NDK产生的背景 Android平台从诞生起,就已经支持C ...

  5. 刨根问底U3D---从Profile中窥探Unity的内存管理

    这篇文章包含哪些内容 这篇文章从Unity的Profile组件入手,来探讨一下Unity在开发环境和正式环境中的内存使用发面的一些区别, 并且给出了最好控制内存的方法(我想你已经知道了...Prefa ...

  6. 如何在Xcode6中添加空模板

    在Xcode中模板位置: Macintosh HD ▸ 应用程序 ▸ Xcode(低于版本6的).app ▸ Contents ▸ Developer ▸ Platforms ▸ iPhoneOS.p ...

  7. FileZilla FTP Server 高级防火墙例外

    在防火墙中: 在“例外”中,添加端口21,TCP 添加端口50000,TCP (或添加一组端口,一个一个的也行,如果你在软件中选择的是50000-51000,而在这里只打开了50000的单个端口,登录 ...

  8. ArcGIS Runtime for Android 使用异步GP服务绘制等值线

    关于基于Android上ArcGIS Server GP服务的调用,已经有前辈给出了很好的例子: http://blog.csdn.net/esrichinacd/article/details/92 ...

  9. js获取url参数值的方法

    index.htm?参数1=数值1&参数2=数值2&参数3=数据3&参数4=数值4&...... 静态html文件js读取url参数 根据获取html的参数值控制htm ...

  10. netty ByteToMessageDecoder 分析

    ByteToMessageDecoder 1.socket 移除时触发,最后次读数据处理 @Override public final void handlerRemoved(ChannelHandl ...