Shaolin

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 315    Accepted Submission(s): 162

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
 
Source
 

函数lower_bound()在first和last中的前闭后开区间进行二分查找,返回大于或等于val的第一个元素位置。如果所有元素都小于val,则返回last的位置

举例如下:

一个数组number序列为:4,10,11,30,69,70,96,100.设要插入数字3,9,111.pos为要插入的位置的下标

pos = lower_bound( number, number + 8, 3) - number,pos = 0.即number数组的下标为0的位置。

pos = lower_bound( number, number + 8, 9) - number, pos = 1,即number数组的下标为1的位置(即10所在的位置)。

pos = lower_bound( number, number + 8, 111) - number, pos = 8,即number数组的下标为8的位置(但下标上限为7,所以返回最后一个元素的下一个元素)。

所以,要记住:函数lower_bound()在first和last中的前闭后开区间进行二分查找,返回大于或等于val的第一个元素位置。如果所有元素都小于val,则返回last的位置,且last的位置是越界的!!~

返回查找元素的第一个可安插位置,也就是“元素值>=查找值”的第一个元素的位置

#include<iostream>
#include<cstdio>
#include<cstring>
#include<set>
#include<map> using namespace std; int main(){ //freopen("input.txt","r",stdin); int n,k,g;
set<int> st;
map<int,int> mp;
while(~scanf("%d",&n) && n){
st.clear();
mp.clear();
st.insert();
mp[]=;
while(n--){
scanf("%d%d",&k,&g);
printf("%d ",k);
set<int>::iterator it=st.lower_bound(g);
if(it==st.end()){
it--;
printf("%d\n",mp[(*it)]);
}else{
int tmp=(*it);
if(it!=st.begin()){
it--;
if(g-(*it)<=tmp-g)
printf("%d\n",mp[(*it)]);
else
printf("%d\n",mp[tmp]);
}else
printf("%d\n",mp[(*it)]);
}
mp[g]=k;
st.insert(g);
}
}
return ;
}

HDU 4585 Shaolin (STL)的更多相关文章

  1. HDU 4585 Shaolin(STL map)

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

  2. HDU 4585 Shaolin (STL map)

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

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

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

  4. HDU 4585 Shaolin(水题,STL)

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

  5. HDU 5934 Bomb(炸弹)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  6. HDU 5734 Acperience(返虚入浑)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  7. HDU 5724 Chess(国际象棋)

    HDU 5724 Chess(国际象棋) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

  8. HDU 5826 physics(物理)

     physics(物理) Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)   D ...

  9. HDU 5835 Danganronpa(弹丸论破)

     Danganronpa(弹丸论破) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

随机推荐

  1. vc2008中mfc菜单、控件等汉字显示为问号或乱码的解决方法

    在vc2008中建立基于mfc的project.在向导的Application type页面中如果在resource language选项中选择"英语(美国)"(图一),那么在pr ...

  2. ASP入门(三)-VBScript变量、运算符

    ASP内置了两种语法引擎,分别是VBScript和JScript. VBScript是VB的一个子集.JScript和JavaScript有些类似. 如果你熟悉VB,建议用VBScript,否则推荐使 ...

  3. 一个十年IT从业者的职场感言:为什么不要自称是“程序员”

    转载:https://blog.csdn.net/S_king_/article/details/78529089 如果我可以给每个工程教育增加一门课,它不会涉及编译器.门电路或是时间复杂度,而是一门 ...

  4. destoon源码解读

    一.module module值:表示模块的id ID1.核心: ID2.会员: ID3.扩展: 当ID>3时,为购买.公司等模块. dt:为各种变量,相当于整站的配置,如:关键词.描述.积分等 ...

  5. ionic3+angular5页面间传递参数

    一.从一个页面跳转到另一个页面的方法 1.引入服务 import { NavController } from 'ionic-angular'; 2.初始化 constructor(public na ...

  6. Linux下使用Nohup后台运行程序

    一.不输出日志的命令 #!/bin/bash echo "starting nohup..." nohup dotnet ./mynetcoreapp.dll > /dev/ ...

  7. angularJS 事件广播与接收[转]

    路由的事件 事件这个词在前端出现的频率真是高,根本拦不住,哪都是.$route服务在路由过程中的每个阶段都会触发不同的事件,可以为这些不同的路由事件设置监听器并做出响应. 一共有4个事件用来监听路由的 ...

  8. Binutils工具集中的一些比较常用的工具

    addr2line addr2line是用来将程序地址转换成其所对应的程序源文件.代码行以及所对应的函数. ar ar是用来管理档案文件的,在嵌入式系统开发中,ar主要是用来对静态库进行管理. ar常 ...

  9. VB.NET版+三层实现登陆

    三层已经学了一些时间了,開始认为自己能够用C#敲代码了,就用C#写了一个实现登陆的,真正再用在机房中.还是认为非常吃力的,所以.决定用vb.net敲了.以下是我用vb.net实现的登陆.能够给大家做一 ...

  10. 再次编译 arm toolchains

    为什么说再呢,因为已经好多次了.每次失败,都再从失败的地方开始.今天这篇呢,主要是记录今天的进展. 1. 编译要分三步走 之前学习的时候就有印象,要三步走.但是因为没有实践过,所以,忘差不多了.所谓三 ...