题意:

少林寺有n+1个和尚,他们都有一个独有的编号和战斗力值,当一个年轻人通过所有考试并被宣布为少林的新僧人时,将会有一场战斗,作为欢迎的一部分。新和尚必须与一位战斗等级最接近他的战斗等级的老和尚战斗。如果有两个老僧人满足这个条件,新僧侣将采取战斗等级低于他的僧侣与他对打。现在保证输入是按照编号顺序升序输入的,要求按顺序输出每一组战斗的双方编号,先输出新和尚的后输出老和尚的。(第一个和尚编号是1,战斗力是1000000000)

题目:

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

分析:

这个题充分体现出了map的优势,因为map存储的时候是按照关键字升序存储的,所以如果按照战斗力值作为关键字建立map的话,找到此战力值的前驱和后继就是可能要与新和尚对打的老和尚了
map的专项知识点看我的另一篇博客:https://blog.csdn.net/zeng_jun_yv/article/details/104704897

AC代码:

#include<bits/stdc++.h>
using namespace std;
map<int,int>a;
int n,x,y;
int main()
{while(~scanf("%d",&n)&&n){a.clear();a[1000000000]=1;for(int i=1; i<=n; i++){cin>>x>>y;a[y]=x;map<int,int>::iterator it=a.find(y);if(it==a.begin())printf("%d %d\n",x,(++it)->second);else if(it==a.end())printf("%d %d\n",x,(--it)->second);else if((abs((++it)->first)-(--it)->first)<abs((--it)->first-(++it)->first))printf("%d %d\n",x,(++it)->second);elseprintf("%d %d\n",x,(--it)->second);}}
}

Shaolin HDU - 4585(map模板题)相关推荐

  1. Shaolin(map+iterator) HDU - 4585

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

  2. HDU 1814 Peaceful Commission / HIT 1917 Peaceful Commission /CJOJ 1288 和平委员会(2-sat模板题)...

    HDU 1814 Peaceful Commission / HIT 1917 Peaceful Commission /CJOJ 1288 和平委员会(2-sat模板题) Description T ...

  3. HDU 2389 Rain on your Parade(二分匹配+Hopcroft-Carp算法模板题)

    You're giving a party in the garden of your villa by the sea. The party is a huge success, and every ...

  4. 单词数 HDU - 2072(字典树模板题amp;stl)

    lily的好朋友xiaoou333最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词的总数.下面你的任务是帮助xiaoou333解决这个问题. Input 有多组数据,每组一行,每组就 ...

  5. 并查集模板题(stl 中map的运用)

    DongDong认亲戚 DongDong每年过春节都要回到老家探亲,然而DongDong记性并不好,没法想起谁是谁的亲戚(定义:若A和B是亲戚,B和C是亲戚,那么A和C也是亲戚),她只好求助于会编程的 ...

  6. hdu 2191 悼念512汶川大地震遇难同胞 【多重背包】(模板题)

    题目链接:https://vjudge.net/problem/HDU-2191 悼念512汶川大地震遇难同胞--珍惜现在,感恩生活                                   ...

  7. hdu 1166 敌兵布阵 树状数组 模板题

    这题是树状数组入门的一模板题,非常基础,被小白成为"赤裸裸"的入门题,哈哈,一个plus,一个sum全部搞定 #include<stdio.h> #include< ...

  8. 一起开心2020暑假训练第二周 图论(模板题)

    比赛链接: 文章目录 A HDU 1285 一 B HDU 1863 起 C POJ 2387 开 D POJ 1502 心 E HDU 5922 图 F HDU 2112 论 A HDU 1285 ...

  9. 图论-有向图的连通性模板题(hdu1296)(hdu1827)

    1.强连通分量: 强连通分量可以理解为边数最少的情况下是一个环. 这里写了一个模板题用的是tarjan算法,当然还有其他算法. tarjan算法的关键其实还是对于num数组和low数组的使用 然后可以 ...

最新文章

  1. 【.net深呼吸】动态类型(高级篇)
  2. SQL 2005 Express 的“企业管理器” 下载
  3. 未在本地计算机上注册oraoledb.oracle.1提供程序,未在本地计算机上注册“OraOLEDB.Oracle.1”提供程序--问题的处理...
  4. js 为什么0.1+0.2不等于0.3
  5. PyTorch框架学习七——自定义transforms方法
  6. android开源2016_2016年开源年鉴:现在提供印刷版
  7. webrtc 静音检测(二)
  8. [源码和文档分享]基于C语言实现的流星雨模拟课程设计
  9. bat中冒号的作用(注释 ,跳转, 截取、替换字符串)
  10. groovy教程入门
  11. python模拟人工滑动_python selenium模拟滑动操作
  12. mysql sock golang_golang socket连接复用 - smux
  13. 【专访】黄健宏:为什么真正的聪明人喜欢用笨方法?
  14. SAS Planet+ArcGIS
  15. word表格分开快捷键_word文档如何快速拆分表格,干货!怎样快速拆分表格以及快速合并表格技巧介绍...
  16. 4、使用bean的scop属性来配置bean的作用域
  17. 相信积累的力量——《把时间当作朋友》读后感
  18. 视频教程-Linux视频教程-Linux
  19. 王兴:格局上输了,不管你多努力都不可能赢!
  20. 如何利用审查元素看密码[小技巧]

热门文章

  1. linux之写C代码出现warning: format not a string literal and no format arguments [-Wformat-security]
  2. sql CHECK ,UNIQUE 约束(mysql)
  3. 实现线段切割法_漫画:如何实现抢红包算法?
  4. querybuilder 排序_elasticsearch的匹配与排序问题
  5. 战队口号霸气押韵8字_高考励志班级口号霸气押韵大全
  6. 看似毫无规律的各种现象
  7. 他毕业于北师大,编写了我国首套数学教材,陈景润华罗庚都崇拜他
  8. 指定的文件不是虚拟磁盘 没有快照_vmware workstaiton 15 虚拟机克隆(4)
  9. java gui 移动_JavaGUI如图:当鼠标按left按钮,右边的字会移动到左边按 爱问知识人...
  10. php mysql explain_MySQL Explain详解