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.
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
题目大意:有一群和尚,他们各自有一个编号和技能值,每一个新和尚加入时都要和和他技能值最接近的和尚打斗一下,如果有两个技能值接近程度一样,那么优先选择技能值小于他的。输出两个和尚的编号,新和尚在前,老和尚在后。最早有一个编号为1,技能值为10亿的和尚。
分析:标准的求前驱(X的前驱是小于X且最大的数)和后继(X的后继是大于X且最小的数),求出前驱和后继后再按照题意比较。
代码如下;
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
const int N=100005;
struct monk
{
int id;
int grade;
}in[N];
int root,s,ans;
struct node
{
int l;
int r;
int val;
int monkid;
int ran;
}tree[N];
void lturn(int &k)
{
int t=tree[k].r;
tree[k].r=tree[t].l;
tree[t].l=k;
k=t;
}
void rturn(int &k)
{
int t=tree[k].l;
tree[k].l=tree[t].r;
tree[t].r=k;
k=t;
}
void ins(int &k,int x,int id)
{
if(k==0)
{
s++;
k=s;
tree[k].val=x;
tree[k].monkid=id;
tree[k].ran=rand();
return;
}
else if(tree[k].val<x)
{
ins(tree[k].r,x,id);
if(tree[k].ran>tree[tree[k].r].ran)
lturn(k);
}
else
{
ins(tree[k].l,x,id);
if(tree[k].ran>tree[tree[k].l].ran)
rturn(k);
}
}
void querypro(int k,int x)
{
if(k==0)
return ;
if(tree[k].val<x)
{
ans=k;
querypro(tree[k].r,x);
}
else
querypro(tree[k].l,x);
}
void querysub(int k,int x)
{
if(k==0)
return;
if(tree[k].val>x)
{
ans=k;
querysub(tree[k].l,x);
}
else
querysub(tree[k].r,x);
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
if(n==0)
break;
memset(tree,0,sizeof(tree));
root=0;
s=0;
for(int i=1;i<=n;i++)
{
scanf("%d %d",&in[i].id,&in[i].grade);
}
ins(root,1000000000,1);
for(int i=1;i<=n;i++)
{
ins(root,in[i].grade,in[i].id);
ans=0;
querypro(root,in[i].grade);
int tmp1=tree[ans].val;
int tmp2=tree[ans].monkid;
//printf("pro=%d %d\n",tmp1,tmp2);
ans=0;
querysub(root,in[i].grade);
int tmp3=tree[ans].val;
int tmp4=tree[ans].monkid;
//printf("sub=%d %d\n",tmp3,tmp4);
if(tmp2==0)
printf("%d %d\n",in[i].id,tmp4);
else if(tmp4==0)
printf("%d %d\n",in[i].id,tmp2);
else if(tmp3-in[i].grade<in[i].grade-tmp1)
printf("%d %d\n",in[i].id,tmp4);
else if(in[i].grade-tmp1<=tmp3-in[i].grade)
printf("%d %d\n",in[i].id,tmp2);
}
}
return 0;
}