HDU 4585 Shaolin (STL)
Shaolin
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 315 Accepted Submission(s): 162
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.
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.
2 1
3 3
4 2
0
3 2
4 2
函数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)的更多相关文章
- HDU 4585 Shaolin(STL map)
Shaolin Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit cid= ...
- HDU 4585 Shaolin (STL map)
Shaolin Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Sub ...
- HDU 4585 Shaolin(Treap找前驱和后继)
Shaolin Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Total Su ...
- HDU 4585 Shaolin(水题,STL)
Shaolin Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Sub ...
- HDU 5934 Bomb(炸弹)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...
- HDU 5734 Acperience(返虚入浑)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...
- HDU 5724 Chess(国际象棋)
HDU 5724 Chess(国际象棋) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...
- HDU 5826 physics(物理)
physics(物理) Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) D ...
- HDU 5835 Danganronpa(弹丸论破)
Danganronpa(弹丸论破) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
随机推荐
- Everything常见问题及搜索技巧,附Demo
1 Everything 1.1 "Everything"是什么? "Everything"是一个运行于Windows系统,基于文件.文件夹名称的快速搜索引擎. ...
- 转: 使用MinGW + Eclipse CDT进行C++开发的配置方法
使用MinGW + Eclipse CDT进行C++开发的配置方法 1 2 3 4 5 6 分步阅读 Eclipse作为开源的IDE,可以用来高效开发Java.C++.C等程序.本文介绍如何在Wind ...
- String escape/unescape into XML
Is there any C# function which could be used to escape and un-escape a string, which could be used t ...
- Python requests如何将第一个请求得到的 cookie 通过 POST 提交给第二个请求
#coding=utf-8 import requests import json url_login, url_test = "http://192.168.0.162/login&quo ...
- Ajax:HyperText/URI, HTML, Javascript, frame, frameset, DHTML/DOM, iframe, XMLHttp, XMLHttpRequest
本文内容 Ajax 诞生 促使 Ajax 产生的 Web 技术演化 真正 Ajax Ajax 与 Web 2.0 Ajax 背后的技术 2008 年毕业,2011 年看了<Ajax 高级程序设计 ...
- LintCode:Fibonacci
C++ class Solution{ public: /** * @param n: an integer * @return an integer f(n) */ int fibonacci(in ...
- JavaScript 之 ScriptManager.RegisterStartupScript的应用
如果页面中不用Ajax,cs中运行某段js代码方式可以是: Page.ClientScript.RegisterStartupScript(Page.GetType(), "", ...
- Android基础新手教程——1.10 反编译APK获代替码&资源
Android基础新手教程--1.10 反编译APK获代替码&资源 标签(空格分隔): Android基础新手教程 本节引言: "反编译Apk".看上去好像好像非常高端的样 ...
- Rabbit mq订阅方式获取消息并可设置持久化
Rabbit 通过方式获取消息:订阅方式事实上是向queue注冊consumer,通过rpc向queue server发送注冊consumer的消息.rabbitMQ Server在收到消息后,依据消 ...
- Python两个变量的值进行交换的方法
Python两个变量的值进行交换的方法 使用第三方变量: '''这是第一种赋值方法,采用第三方变量''' c = a a = b b = c 使用两个变量: '''使用两个变量''' a = a+b ...