Shaolin
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 64 Accepted Submission(s): 38
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.
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.
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
STL的set乱搞就可以了
1 /* ********************************************** 2 Author : kuangbin 3 Created Time: 2013/8/10 11:55:20 4 File Name : F:2013ACM练习比赛练习2013杭州邀请赛重现1011.cpp 5 *********************************************** */ 6 7 #include <stdio.h> 8 #include <string.h> 9 #include <iostream> 10 #include <algorithm> 11 #include <vector> 12 #include <queue> 13 #include <set> 14 #include <map> 15 #include <string> 16 #include <math.h> 17 #include <stdlib.h> 18 using namespace std; 19 20 int main() 21 { 22 //freopen("in.txt","r",stdin); 23 //freopen("out.txt","w",stdout); 24 set<int>st; 25 map<int,int>mp; 26 int n; 27 while(scanf("%d",&n) == 1 && n) 28 { 29 st.clear(); 30 mp.clear(); 31 st.insert(1000000000); 32 mp[1000000000] = 1; 33 int u,v; 34 while(n--) 35 { 36 scanf("%d%d",&u,&v); 37 printf("%d ",u); 38 set<int>::iterator it = st.lower_bound(v); 39 if(it == st.end()) 40 { 41 it--; 42 printf("%d ",mp[*it]); 43 } 44 else 45 { 46 int t1 = (*it); 47 if(it != st.begin()) 48 { 49 it--; 50 if(v - (*it) <= t1 - v) 51 { 52 printf("%d ",mp[*it]); 53 } 54 else printf("%d ",mp[t1]); 55 } 56 else printf("%d ",mp[*it]); 57 } 58 mp[v] = u; 59 st.insert(v); 60 } 61 } 62 return 0; 63 }
今天才知道原来直接用map也可以实现。
原来map也是排序了的,Orz....
1 /* ********************************************** 2 Author : kuangbin 3 Created Time: 2013/8/10 11:55:20 4 File Name : F:2013ACM练习比赛练习2013杭州邀请赛重现1011.cpp 5 *********************************************** */ 6 7 #include <stdio.h> 8 #include <string.h> 9 #include <iostream> 10 #include <algorithm> 11 #include <vector> 12 #include <queue> 13 #include <set> 14 #include <map> 15 #include <string> 16 #include <math.h> 17 #include <stdlib.h> 18 using namespace std; 19 20 int main() 21 { 22 //freopen("in.txt","r",stdin); 23 //freopen("out.txt","w",stdout); 24 map<int,int>mp; 25 int n; 26 while(scanf("%d",&n) == 1 && n) 27 { 28 mp.clear(); 29 mp[1000000000] = 1; 30 int u,v; 31 while(n--) 32 { 33 scanf("%d%d",&u,&v); 34 printf("%d ",u); 35 map<int,int>::iterator it = mp.lower_bound(v); 36 if(it == mp.end()) 37 { 38 it--; 39 printf("%d ",it->second); 40 } 41 else 42 { 43 int t1 = it->first; 44 int tmp = it->second; 45 if(it != mp.begin()) 46 { 47 it--; 48 if(v - it->first <= t1 - v) 49 { 50 printf("%d ",it->second); 51 } 52 else printf("%d ",tmp); 53 } 54 else printf("%d ",it->second); 55 } 56 mp[v] = u; 57 } 58 } 59 return 0; 60 }