Shaolin
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 1178 Accepted Submission(s): 501
Problem 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
32 13 34 20
Sample Output
2 13 24 2
Source
/*iterator lower_bound( const key_type &key ): 返回一个迭代器,指向键值>= key的第一个元素。
iterator upper_bound( const key_type &key ):返回一个迭代器,指向键值> key的第一个元素。*/
#include<stdio.h>
#include<set>
using namespace std;
struct node
{
int id,x;
friend bool operator<(node a,node b)
{
return a.x<b.x;//注意这里
}
}ma;
set<node>q;
int main()
{
int n,i,v1,v2;
while(scanf("%d",&n),n)
{
ma.id=1; ma.x=1000000000;
q.clear();
q.insert(ma);
for(i=0;i<n;i++)
{
scanf("%d%d",&ma.id,&ma.x);
set<node>::iterator now=q.lower_bound(ma),t1,t2;
t1=t2=now;
if(t1!=q.begin())
t1--;
v1=ma.x-t1->x; v2=t2->x-ma.x;
if(v1<=v2)
printf("%d %d\n",ma.id,t1->id);
else
printf("%d %d\n",ma.id,t2->id);
q.insert(ma);
set<node>::iterator it;
/*for(it=q.begin();it!=q.end();it++)
printf("%d %d\n",it->id,it->x);*/
}
}
return 0;
}
#include<stdio.h>
#include<algorithm>
#define N 100005
using namespace std;
struct node
{
int id,x,d,num;
}a[N];
bool cmp1(node a,node b)
{
return a.x<b.x;
}
bool cmp2(node a,node b)
{
return a.num<b.num;
}
int main()
{
int i,j,n,t;
while(scanf("%d",&n),n)
{
n++;
a[0].id=1; a[0].x=1000000000; a[0].num=1;
for(i=1;i<n;i++)
{
scanf("%d%d",&a[i].id,&a[i].x);
a[i].num=i+1;
}
sort(a,a+n,cmp1);//先按照x的值从小到大排序
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)//找出第一个比当前人的x值大,并且num值比当前的人小
{
if(a[j].num<a[i].num)
{
t=a[j].x-a[i].x;
a[i].d=a[j].id;
break;
}
}
for(j=i-1;j>=0;j--)//找出第一个比当前人的x值小,并且num值比当前的人小
{
if(a[j].num<a[i].num)
{
if(t>=a[i].x-a[j].x)
a[i].d=a[j].id;
break;
}
}
}
sort(a,a+n,cmp2);//按照出现顺序排序
for(i=1;i<n;i++)
printf("%d %d\n",a[i].id,a[i].d);
}
return 0;
}