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 he talent on understanding the Buddism scripture, but fighting Skil L is also taken to account.
When a young mans passes all the tests and is declared a new monk of Shaolin, there'll be a fight, as a part of the WELC Ome party. Every monk has a unique ID and a unique fighting grade, which is all integers. The new monk must fight with an old monk whose fighting grade are closest to his fighting grade. If there is a monks satisfying that condition, the new monk would take the one whose fighting grade was less than his .
The Master is the first monk-Shaolin, his ID is 1,and he fighting grade is 1,000,000,000.he just lost the fighting rec Ords. But he still remembers who joined Shaolin earlier, who joined later. Recover the fighting records for him.
Input
HDU 4585There is several test cases.
In each test case:
The first line was 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 an integer k and G, meaning a monk ' s ID and his fighting grade. (0<= K, g<=5,000,000)
The monks is listed by ascending order of jointing time. In the other words, monks who joined Shaolin earlier come first.
The input ends with n = 0.
Output
A fight can be described as both 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 and then the old monk's ID.
Sample Input
32 13 34 20
Sample Output
2 13 24 2 Test instructions: There are n new monks and a beginning of the Big Monk, the big monk numbered 1, attack 1000,000,000 other n monk number and attack different, the input stream in chronological order to add the monk's number and attack, Each newly added monk will find a monk who has the closest to his attack, and if there are two monks and the new monk, the difference is the same, the attack small and he contest, the output of the new monk's number and the number of the monk with whom he was a contest. Idea: Using the algorithm of balanced binary tree, it is convenient to find the precursor and successor of X-value. The code is as follows:
#include <iostream>#include<algorithm>#include<cstdio>#include<cstring>using namespacestd;structdata{intL,r,v,vo; intRnd;} tr[110000];intSize,root,ans1,ans2;///the default initial value for defining global integer variables is 0;voidRturn (int&k) { intt=TR[K].L; TR[K].L=TR[T].R; TR[T].R=K; K=t;}voidLturn (int&k) { intt=TR[K].R; TR[K].R=TR[T].L; TR[T].L=K; K=t;}voidInsertint&k,intXintXO) { if(k==0) {size++;///record the number of structures already in use;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); }}voidQuery_pro (intKintX///To find the precursor of X (the precursor is defined as less than X and the largest number);{ if(k==0)return; if(tr[k].v<x) {ans1=K; Query_pro (TR[K].R,X); } ElseQuery_pro (tr[k].l,x);}voidQuery_sub (intKintX///the successor of X (subsequent definition is greater than X, and the smallest number);{ if(k==0)return; if(tr[k].v>x) {ans2=K; Query_sub (TR[K].L,X); } Elsequery_sub (tr[k].r,x);}intMain () {intn,xo,x; while(SCANF ("%d", &n)!=eof&&N) {root=0; Size=0; for(intI=0;i<110000; i++) {TR[I].L=0; TR[I].R=0; TR[I].V=0; Tr[i].vo=0; Tr[i].rnd=0; } insert (Root,1000000000,1); while(n--) {scanf ("%d%d",&xo,&x); Insert (ROOT,X,XO); Ans1=0; Ans2=0; Query_pro (ROOT,X); Query_sub (ROOT,X); if(ans1==0) 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); Elseprintf"%d%d\n", XO,TR[ANS2].VO); } } } return 0;}
Priority Queue---Shaolin