hdu 4585 Shaolin_set用法
题意:有n个人想成为少林,但是成为少林必须跟少林的大师大一场,当然要选择战斗力很近的,有两大师战斗力跟那人相近程度一样就选战斗力小的那个,按输入顺序,先输入的人先成为少林大师,后面输入的人,选一个前面输入的人打一场,当然少林已经存在一个超级大师(初始号码为1,不用输入已存在)。
思路:我开头用排序搞来搞去都没成,后来看看人题解,瞬间发觉set很好用,像优先队列一样自己会排好序了。
函数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) ,pos = 0.即number数组的下标为0的位置。
pos = lower_bound( number, number + 8, 9) , pos = 1,即number数组的下标为1的位置(即10所在的位置)。
pos = lower_bound( number, number + 8, 111), 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(){
int n,k,g;
set<int> st;
map<int,int> mp;
while(~scanf("%d",&n) && n){
st.clear();
mp.clear();
st.insert(1000000000);
mp[1000000000]=1;
while(n--){
scanf("%d%d",&k,&g);
printf("%d ",k);
set<int>::iterator it=st.lower_bound(g);
int tmp=(*it);
if(it!=st.begin()){
it--;//比他战斗力少的第一个
if((*it)-g>=g-tmp)
printf("%d\n",mp[(*it)]);
else
printf("%d\n",mp[tmp]);
}else
printf("%d\n",mp[(*it)]); mp[g]=k;
st.insert(g);
}
}
return 0;
}
hdu 4585 Shaolin_set用法的更多相关文章
- 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 map)
Shaolin Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit cid= ...
- [HDU 4585] Shaolin (map应用)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4585 题目大意:不停的插入数字,问你跟他相距近的ID号.如果有两个距离相近的话选择小的那个. 用map ...
- HDU 4585
http://acm.hdu.edu.cn/showproblem.php?pid=4585 从原来的人中找出战斗数值最接近的,输出他们两人的序号 要在logn的复杂度完成查找,我用的是set,当然用 ...
- hdu 4585 map **
题意: Shaolin temple is very famous for its Kongfu monks.A lot of young men go to Shaolin temple every ...
- hdu 4585 Shaolin treap
Shaolin Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Problem ...
- A -- HDU 4585 Shaolin
Shaolin Time Limit: 1000 MS Memory Limit: 32768 KB 64-bit integer IO format: %I64d , %I64u Java clas ...
- hdu 4585 Shaolin
原题链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=46093 #include<algorithm> #in ...
- hdu 4585 set应用
#include<iostream> #include<algorithm> #include<cstring> #include<cstdio> #i ...
随机推荐
- c语言结构体在内存中存储,字节对齐
注意: 出于效率的考虑,C语言引入了字节对齐机制,一般来说,不同的编译器字节对齐机制有所不同,但还是有以下3条通用准则: (1)结构体变量的大小能够被其最宽基本类型成员的大小所整除: (2)结构体每个 ...
- poj 3187 Backward Digit Sums(穷竭搜索dfs)
Description FJ and his cows enjoy playing a mental game. They write down the numbers to N ( <= N ...
- iOS 语音识别使用讯飞报错
You must rebuild it with bitcode enabled(Xcode setting ENABLE_BITCODE), obtain an updated library fr ...
- [Redux] Passing the Store Down with <Provider> from React Redux
Previously, we wrote the Provider component by ourself: class Provider extends Component { getChildC ...
- [转]MVP模式开发
转自:http://www.jianshu.com/p/f7ff18ac1c31 基于面向协议MVP模式下的软件设计-(iOS篇) 字数9196 阅读505 评论3 喜欢11 基于面向协议MVP模式下 ...
- 英文长单词断行 word-break VS word-wrap
你真的了解word-wrap和word-break的区别吗? 这两个东西是什么,我相信至今还有很多人搞不清,只会死记硬背的写一个word-wrap:break-word;word-break:brea ...
- 使用xdebug+eclipse远程调试centOS7上的PHP代码
这两天一直在研究PHP代码的调试,原来开发一直用的var_dump,麻烦! 最近发现能使用xdebug+eclipse远程单步调试PHP,但是百度后实现此技术的文章都写得不够具体. 我照着这里零散的文 ...
- js实现楼层效果
今天自己写个楼层效果,有一点烦躁,小地方犯错误.各位大神来修改不足啊!!! <!DOCTYPE html><html lang="en"><head& ...
- ubuntu终端命令
整个电脑都划成ubuntu用. 装软件时的一个明显感觉就是很多事情,用终端的命令行去做很容易,用图形界面往往很复杂,而且很多时候还会出现权限的问题,对于ubuntu的用户权限,现在的唯一感觉就是权限在 ...
- 在js中使用json
在js中使用json var obj = { "1" : "value1", "2" : "value2" ...