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 ;
}

最新文章

  1. ecshop 不同页面调用不同分类文章的解决办法
  2. php curl 采集
  3. marquee标签、插入百度地图
  4. 游戏贴图中常用术语《DC》的理解
  5. 用jquery追加的元素不能触发treeview事件
  6. python 调用系统命令
  7. [转载]python中multiprocessing.pool函数介绍
  8. Android 解析XML
  9. 解决Ubuntu14.04下Clementine音乐播放器不能播放wma文件的问题
  10. poj 1523 SPF(tarjan求割点)
  11. (转)Java compiler level does not match解决方法
  12. selenium +python+windows 环境搭建
  13. pandas 处理文本数据
  14. 下载 Internet Explorer 11(脱机安装程序)
  15. 002_ASP.NET 换主题
  16. stm32+rx8025
  17. Spring Cloud 入门教程(二): 配置管理
  18. [CI]CodeIgniter应用配置明细
  19. 第二章&#160;向量(f)归并排序
  20. 【转载】uwp 获取系统字体库

热门文章

  1. Softmax 函数的特点和作用是什么?
  2. A Complete ActiveX Web Control Tutorial
  3. POI中setDefaultColumnWidth方法不起作用的原因
  4. VS2017专业版使用最新版Qt5.9.2教程
  5. Android 之 PackageManager获取的应用程序信息与AndroidManifest文件中设置的信息不一致问题
  6. [网摘][医学影像] DICOM 和 NIFTI 基础知识与区别
  7. 微信小程序 - 分包加载(预下载)
  8. vsphere 5.1 改进和SSO理解
  9. (素材源代码) 猫猫学iOS 之UIDynamic重力、弹性碰撞吸附等现象牛逼Demo
  10. cocos2d-js Shader系列3:多重纹理 multiple textures multiple samplers