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.

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

2013ACM-ICPC杭州赛区全国邀请赛
 
题目大意:
 
有n个人要成为少林寺弟子,需要比武,比武对象是已经成为少林寺弟子的人,就是在他之前成为完成比武的人,需要与武力值相近的人比武,如果有多个,那么选择比自己武力值低的。输出自己的id还有与自己比武的人的id.
 
思路:
 
用set和map还有lower_bound。set用于去重排序,map用于映射武力值和编号id,lower_bound用于查找与自己武力值相近的和尚。详细见代码。
 
代码:
 
 #include <iostream>
#include <set>//利用set可去重按升序排序
#include <map>//利用map将id与武力值映射
#include <cmath>
using namespace std;
int main(){
int n,k,g;
set<int>s;
map<int,int>m;
while(cin>>n){
if(n==) break;
s.clear();
m.clear();
s.insert();//将master入集合
m[]=;//记录master的编号
while(n--){
cin>>k>>g;//输入新和尚的编号k与武力值g
cout<<k<<" ";
set<int>::iterator it1,it2;
it1=s.lower_bound(g);//it1为第一个武力值大于等于g的和尚的地址
if(it1==s.begin()) cout<<m[*it1]<<endl;
else {
it2=it1--;
if(abs(*it1-g)>abs(*it2-g)) cout<<m[*it2]<<endl;
else cout<<m[*it1]<<endl;
}
s.insert(g);//当前新和尚比武完成,将他的武力值入集合,供以后使用
m[g]=k;//记录该武力值g所对应的编号k
}
}
return ;
}

A -- HDU 4585 Shaolin的更多相关文章

  1. HDU 4585 Shaolin(Treap找前驱和后继)

    Shaolin Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Su ...

  2. HDU 4585 Shaolin(STL map)

    Shaolin Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit cid= ...

  3. [HDU 4585] Shaolin (map应用)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4585 题目大意:不停的插入数字,问你跟他相距近的ID号.如果有两个距离相近的话选择小的那个. 用map ...

  4. hdu 4585 Shaolin treap

    Shaolin Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Problem ...

  5. hdu 4585 Shaolin(STL map)

    Problem Description Shaolin temple is very famous for its Kongfu monks.A lot of young men go to Shao ...

  6. HDU 4585 Shaolin (STL)

    Shaolin Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Sub ...

  7. HDU 4585 Shaolin(水题,STL)

    Shaolin Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Sub ...

  8. HDU 4585 Shaolin (STL map)

    Shaolin Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Sub ...

  9. hdu 4585 Shaolin

    原题链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=46093 #include<algorithm> #in ...

随机推荐

  1. java-cef系列视频第三集:添加flash支持

    上一集我们介绍了如何搭建java-cef调试环境. 本视频介绍如何给java-cef客户端添加flashplayer支持 第四集视频我们将介绍java-cef中的自定义协议. 本作品采用知识共享署名- ...

  2. TabHost的用法(转)

    本文结合源代码和实例来说明TabHost的用法. 使用TabHost 可以在一个屏幕间进行不同版面的切换,例如android自带的拨号应用,截图:  查看tabhost的源代码,主要实例变量有: pr ...

  3. java之final关键字

    final关键字(可以读不可以写.只读) 1.final的变量的值不能够被改变 ①.final的成员变量 ②.final的局部变量(形参) //意思是“实参”一旦传进我的方法里面,就不允许改变 2.f ...

  4. [Google Translation API v2 for Java]

    Reference:https://cloud.google.com/translate/docs/reference/libraries#java-resources QuickstartSampl ...

  5. [转载]QString 乱谈(3)-Qt5与中文

    原文地址http://blog.csdn.net/dbzhang800/article/details/7542672?reload 两个月前,简单写过QTextCodec中的setCodecForT ...

  6. main函数读取jar包外部的配置文件properties

    首先,Java的main方法有个初始化入参args,如下所示: public static void main(String[] args) {} 然后,在linux下执行jar包引入外部配置文件的命 ...

  7. tcp为什要三次握手

    准备知识: 单工:信息只能单向传递.发送-->接收,单向,不能返回响应. 双工:指的是信息可双向发送. 全双工:信息可同时双向传递. 半双工:不能同时,单行道,一边传输完了,另一边才能发起传输. ...

  8. [Python] Create a Django project in Pycharm

    From: http://blog.csdn.net/u013088062/article/details/50158239 From: http://blog.csdn.net/u013088062 ...

  9. yousa_team团队项目——兼职平台网站 工作进度

    4月31 初步设计网站界面,功能以及数据库关系图 网站包括登陆注册界面,商家和学生都有个人主页,查看兼职信息界面和反馈界面,管理员有查看反馈界面,查看兼职市场,管理后台界面 登录注册界面实现用户的登陆 ...

  10. 【入门必备】最佳的 Node.js 学习教程和资料书籍

    Web 开发人员对 Node.js 的关注日益增多,更多的公司和开发者开始尝试使用 Node.js 来实现一些对实时性要求高,I/O密集型的业务.这篇文章中,我们整理了一批优秀的资源,你可以得到所有你 ...