HDU 4585 Shaolin

Shaolin

Time Limit: 1000ms
Memory Limit: 32768KB
This problem will be judged on HDU. Original ID: 4585
64-bit integer IO format: %I64d      Java class name: Main
 
 
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

Source

 
解题:平衡树的使用。
 map版
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <climits>
 7 #include <vector>
 8 #include <queue>
 9 #include <cstdlib>
10 #include <string>
11 #include <set>
12 #include <stack>
13 #include <map>
14 #define LL long long
15 #define pii pair<int,int>
16 #define INF 0x3f3f3f3f
17 using namespace std;
18 map<int,int>mp;
19 int main() {
20     int n,x,y;
21     while(scanf("%d",&n),n) {
22         mp.clear();
23         mp[1e9] = 1;
24         for(int i = 0; i < n; i++) {
25             scanf("%d %d",&x,&y);
26             map<int,int>::iterator it = mp.lower_bound(y);
27             if(it->first == y) {
28                 printf("%d %d
",x,it->second);
29             } else {
30                 if(it == mp.begin())
31                     printf("%d %d
",x,it->second);
32                 else {
33                     map<int,int>::iterator it2 = it--;
34                     printf("%d %d
",x,y-it->first <= it2->first-y?it->second:it2->second);
35                 }
36                 mp[y] = x;
37             }
38         }
39     }
40     return 0;
41 }
View Code

Treap版

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <climits>
 7 #include <vector>
 8 #include <queue>
 9 #include <cstdlib>
10 #include <string>
11 #include <set>
12 #include <stack>
13 #include <ctime>
14 #define LL long long
15 #define pii pair<int,int>
16 #define INF 0x3f3f3f3f
17 using namespace std;
18 struct node {
19     int lt,rt,val,fix,id;
20     void init(){lt = rt = val = id = 0;}
21 };
22 node Treap[200000];
23 int tot;
24 void left_rotate(int &root) {
25     int b = Treap[root].rt;
26     Treap[root].rt = Treap[b].lt;
27     Treap[b].lt = root;
28     root = b;
29 }
30 void right_rotate(int &root) {
31     int b = Treap[root].lt;
32     Treap[root].lt = Treap[b].rt;
33     Treap[b].rt = root;
34     root = b;
35 }
36 void insertNode(int &root,int val,int id) {
37     if(!root) {
38         Treap[++tot].init();
39         root = tot;
40         Treap[root].val = val;
41         Treap[root].id = id;
42         Treap[root].fix = rand();
43         return;
44     }
45     if(val == Treap[root].val) return;
46     if(val < Treap[root].val) {
47         insertNode(Treap[root].lt,val,id);
48         if(Treap[Treap[root].lt].fix < Treap[root].fix)
49             right_rotate(root);
50     } else {
51         insertNode(Treap[root].rt,val,id);
52         if(Treap[Treap[root].rt].fix < Treap[root].fix)
53             left_rotate(root);
54     }
55 }
56 int pred(int &root,int val,int optimal){
57     if(!root) return optimal;
58     if(Treap[root].val <= val) return pred(Treap[root].rt,val,root);
59     else return pred(Treap[root].lt,val,optimal);
60 }
61 int succ(int &root,int val,int optimal){
62     if(!root) return optimal;
63     if(val <= Treap[root].val) return succ(Treap[root].lt,val,root);
64     else return succ(Treap[root].rt,val,optimal);
65 }
66 int main() {
67     srand(time(NULL));
68     int n,x,y,root;
69     while(scanf("%d",&n),n){
70         root = tot = 0;
71         insertNode(root,1e9,1);
72         for(int i = 0; i < n; i++){
73             scanf("%d %d",&x,&y);
74             int it = succ(root,y,-1);
75             if(Treap[it].val == y) printf("%d %d
",x,Treap[it].id);
76             else{
77                 int it2 = pred(root,y,-1);
78                 if(it2 == -1) printf("%d %d
",x,Treap[it].id);
79                 else printf("%d %d
",x,y-Treap[it2].val <= Treap[it].val - y?Treap[it2].id:Treap[it].id);
80             }
81             insertNode(root,y,x);
82         }
83     }
84     return 0;
85 }
View Code

SBT版

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <cmath>
  5 #include <algorithm>
  6 #include <climits>
  7 #include <vector>
  8 #include <queue>
  9 #include <cstdlib>
 10 #include <string>
 11 #include <set>
 12 #include <stack>
 13 #define LL long long
 14 #define pii pair<int,int>
 15 #define INF 0x3f3f3f3f
 16 using namespace std;
 17 const int maxn = 100100;
 18 struct node{
 19     int lt,rt,val,id,sz;
 20     void init(){id = lt = rt = sz = 0;}
 21 };
 22 node SBT[maxn];
 23 int n,tot;
 24 void right_rotate(int &root){
 25     int k = SBT[root].lt;
 26     SBT[root].lt = SBT[k].rt;
 27     SBT[k].rt = root;
 28     SBT[k].sz = SBT[root].sz;
 29     SBT[root].sz = SBT[SBT[root].lt].sz+SBT[SBT[root].rt].sz+1;
 30     root = k;
 31 }
 32 void left_rotate(int &root){
 33     int k = SBT[root].rt;
 34     SBT[root].rt = SBT[k].lt;
 35     SBT[k].lt = root;
 36     SBT[k].sz = SBT[root].sz;
 37     SBT[root].sz = SBT[SBT[root].lt].sz+SBT[SBT[root].rt].sz+1;
 38     root = k;
 39 }
 40 void maintain(int &root){
 41     if(SBT[SBT[SBT[root].lt].lt].sz > SBT[SBT[root].rt].sz){
 42         right_rotate(root);
 43         maintain(SBT[root].rt);
 44         maintain(root);
 45         return;
 46     }
 47     if(SBT[SBT[SBT[root].lt].rt].sz > SBT[SBT[root].rt].sz){
 48         left_rotate(SBT[root].lt);
 49         right_rotate(root);
 50         maintain(SBT[root].lt);
 51         maintain(SBT[root].rt);
 52         maintain(root);
 53         return;
 54     }
 55     if(SBT[SBT[SBT[root].rt].rt].sz > SBT[SBT[root].lt].sz){
 56         left_rotate(root);
 57         maintain(SBT[root].lt);
 58         maintain(root);
 59         return;
 60     }
 61     if(SBT[SBT[SBT[root].rt].lt].sz > SBT[SBT[root].lt].sz){
 62         right_rotate(SBT[root].rt);
 63         left_rotate(root);
 64         maintain(SBT[root].rt);
 65         maintain(SBT[root].lt);
 66         maintain(root);
 67     }
 68 }
 69 void insertNode(int &root,int val,int id){
 70     if(!root){
 71         SBT[++tot].init();
 72         root = tot;
 73         SBT[root].val = val;
 74         SBT[root].id = id;
 75         SBT[root].sz = 1;
 76     }else{
 77         SBT[root].sz++;
 78         if(val <= SBT[root].val) insertNode(SBT[root].lt,val,id);
 79         else insertNode(SBT[root].rt,val,id);
 80     }
 81     maintain(root);
 82 }
 83 int pred(int &root,int val,int optimal){
 84     if(!root) return optimal;
 85     if(SBT[root].val <= val) return pred(SBT[root].rt,val,root);
 86     else return pred(SBT[root].lt,val,optimal);
 87 }
 88 int succ(int &root,int val,int optimal){
 89     if(!root) return optimal;
 90     if(val <= SBT[root].val) return succ(SBT[root].lt,val,root);
 91     else return succ(SBT[root].rt,val,optimal);
 92 }
 93 int main() {
 94     int root,x,y;
 95     while(scanf("%d",&n),n){
 96         root = tot = 0;
 97         insertNode(root,1e9,1);
 98         for(int i = 0; i < n; i++){
 99             scanf("%d %d",&x,&y);
100             int it = succ(root,y,-1);
101             if(SBT[it].val == y) printf("%d %d
",x,SBT[it].id);
102             else{
103                 int it2 = pred(root,y,-1);
104                 if(it2 == -1) printf("%d %d
",x,SBT[it].id);
105                 else printf("%d %d
",x,y-SBT[it2].val <= SBT[it].val-y?SBT[it2].id:SBT[it].id);
106             }
107             insertNode(root,y,x);
108         }
109     }
110     return 0;
111 }
View Code

相关推荐