题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4585
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)

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.

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

Problem solving report:

Description: 有N个依次进入少林,每次输出新进和尚和战斗等级与其最接近的旧和尚的ID。ID和战斗等级都是唯一的。
Problem solving: 每次输入一个和尚的信息,然后在map里建立映射, 用lower_bound(gra)寻找第一个大于等于当前加入的和尚所在的迭代器;然后,考虑当前的迭代器的位置,要是第一个,就输出当前迭代器;否则和它前面迭代器分别与刚加入的比较first之间的差值哪个更小,小的输出;要是两差值一样的话,输出前一个迭代器。

Accepted Code:

#include <bits/stdc++.h>
using namespace std;
int main() {int n, ids, gra;while (scanf("%d", &n), n) {map <int, int> spt;spt[1000000000] = 1;for (int i = 0; i < n; i++) {scanf("%d%d", &ids, &gra);map <int, int>::iterator itl, itr, it = spt.lower_bound(gra);itl = itr = it;if (itl != spt.begin())itl--;if (gra - itl -> first > itr -> first - gra)itl = itr;printf("%d %d\n", ids, itl -> second);spt[gra] = ids;}}return 0;
}

HDU - Shaolin(STL)相关推荐

  1. hdu 4302 STL multiset

    用multiset过 #include<cstdio> #include<cstring> #include<iostream> #include<set&g ...

  2. hdu 1027 STL next_permutation

    求下一个排列模板 #include<cstdio> #include<cstring> #include<iostream> #include<algorit ...

  3. linux chown 将root改变所有者为admin,Linux用户管理 权限管理 内存管理 网络管理命令 (第四天)...

    默认添加的用户会自动加入和用户名一样的组中 su 切换用户 查看当前登陆的用户: whoami id` 查看当前用户属于哪个组:groups groupadd 组名 添加组 groupdel 组名 删 ...

  4. hdu 4268 Alice and Bob(STL版)

    http://acm.hdu.edu.cn/showproblem.php?pid=4268 这是今天网络赛的水题,下午短路了,没想到怎么做.队友hq是用treap做的,不过赛后我才想懂怎么做,回到宿 ...

  5. hdu 5265 pog loves szh II STL

    pog loves szh II Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php? ...

  6. hdu 4409 Family Name List LCA +stl

    http://acm.hdu.edu.cn/showproblem.php?pid=4409 赛后才过只能说悲剧了,知道思路,stl不熟悉,所以导致写的很慢....占据了很多时间,手速+代码准确度.. ...

  7. 素数环(dfsamp;amp;STL做法)HDU - 1016

    HDU - 1016  cxsys训练第一周&第二周 A ring is compose of n circles as shown in diagram. Put natural numbe ...

  8. Shaolin(map+iterator) HDU - 4585

    Shaolin(map+iterator) HDU - 4585 少林寺以武僧而闻名.每年都有很多年轻人去少林寺当和尚.少林的主人评估一个年轻人主要通过他的人才了解佛教经文,但武功也考虑在内. 当一个 ...

  9. STL训练 HDU - 1716 Ray又对数字的列产生了兴趣:

    HDU - 1716 Ray又对数字的列产生了兴趣: 现有四张卡片,用这四张卡片能排列出很多不同的4位数,要求按从小到大的顺序输出这些4位数. Input 每组数据占一行,代表四张卡片上的数字(0&l ...

  10. Shaolin HDU - 4585(map模板题)

    题意: 少林寺有n+1个和尚,他们都有一个独有的编号和战斗力值,当一个年轻人通过所有考试并被宣布为少林的新僧人时,将会有一场战斗,作为欢迎的一部分.新和尚必须与一位战斗等级最接近他的战斗等级的老和尚战 ...

最新文章

  1. 鸿蒙智慧屏安卓内核,一切都是假象?荣耀智慧屏翻车,鸿蒙只是安卓的皮肤?...
  2. Win的cmd中文乱码
  3. css截断长文本显示
  4. Leet Code OJ 482. License Key Formatting [Difficulty: Medium]
  5. Visual C++ 2008入门经典 第四章数组 字符串
  6. DBA遇到问题时的30 个反应,你是哪一种?
  7. 如何访问局域网的Access数据库?
  8. Cuiwei Li / Detection of ECG characteristic points using wavelet transforms
  9. Linux电源管理-Linux Regulator Framework代码分析
  10. 华为交换机基础命令合集
  11. 那些年,我们一起卸载过的软件…
  12. 徐小贱c语言试卷,纹了这条锦鲤,我遇见了现在的老公 | 纹身大赏No.12
  13. RabbitMq Direct exchange路由模型
  14. sprintf函数详细解释
  15. Linux中cp(拷贝)命令
  16. 达梦8 DCA培训总结
  17. 视频直播系统源码,简单计时器
  18. .NET Core使用Cookie做身份验证
  19. 模拟增强PSP实用软件iRShell 3.81PS补丁放出
  20. 高版本android怎么玩低版本游戏,手机玩安卓系统安装变态版手游常见问题解决方法_android游戏安装教程详解...

热门文章

  1. 计算机网络有什么部分组成,计算机网络有哪些组成部分和详细对比
  2. Node.js开发入门—使用http访问外部世界
  3. latex normal是几号字_LaTeX 中英文字体字号设置
  4. 29-基于单片机的防盗报警系统仿真
  5. Java中成员变量的超详解
  6. JMS之——ActiveMQ 高可用与负载均衡集群安装、配置(ZooKeeper + LevelDB + Static discovery)
  7. CORS手机测试软件,司南导航RTK手簿软件测量大师连接千寻cors账号进行测量的方法教程...
  8. 和朱晔一起复习Java并发(一):线程池
  9. showVideo C#版 摄像头驱动程序
  10. 彻底解决SysFader:IEXPLORE.EXE应用程序错误