首页 > 代码库 > HDU4585
HDU4585
Shaolin
Time Limit: 1000 MS Memory Limit: 32768 KB
64-bit integer IO format: %I64d , %I64u Java class name: Main
[Submit] [Status] [Discuss]
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
32 13 34 20
Sample Output
2 13 24 2
Source
2013ACM-ICPC杭州赛区全国邀请赛
题目大意:
有n个人要成为少林寺弟子,需要比武,比武对象是已经成为少林寺弟子的人,就是在他之前成为完成比武的人,需要与武力值相近的人比武,如果有多个,那么选择比自己武力值低的。输出自己的id还有与自己比武的人的id.
思路:
用set和map还有lower_bound。set用于去重排序,map用于映射武力值和编号id,lower_bound用于查找与自己武力值相近的和尚。详细见代码。
代码:
1 #include <iostream> 2 #include <set>//利用set可去重按升序排序 3 #include <map>//利用map将id与武力值映射 4 #include <cmath> 5 using namespace std; 6 int main(){ 7 int n,k,g; 8 set<int>s; 9 map<int,int>m;10 while(cin>>n){11 if(n==0) break;12 s.clear();13 m.clear();14 s.insert(1000000000);//将master入集合15 m[1000000000]=1;//记录master的编号16 while(n--){17 cin>>k>>g;//输入新和尚的编号k与武力值g18 cout<<k<<" ";19 set<int>::iterator it1,it2;20 it1=s.lower_bound(g);//it1为第一个武力值大于等于g的和尚的地址21 if(it1==s.begin()) cout<<m[*it1]<<endl;22 else {23 it2=it1--;24 if(abs(*it1-g)>abs(*it2-g)) cout<<m[*it2]<<endl;25 else cout<<m[*it1]<<endl;26 }27 s.insert(g);//当前新和尚比武完成,将他的武力值入集合,供以后使用28 m[g]=k;//记录该武力值g所对应的编号k29 }30 }31 return 0;32 }
HDU4585
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。