UVA - 10474 Where is the Marble?
来源:互联网 发布:ubuntu中文语言包 apt 编辑:程序博客网 时间:2024/04/30 06:22
主要就是熟悉一下lower_bound;
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn=10011;
int a[maxn];
int main()
{
int n, q, ncase=0;
while(cin>>n>>q,q!=0&&n!=0)
{
cout<<"CASE# "<<++ncase<<":"<<endl;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
sort(a,a+n);
for(int i=0;i<q;i++)
{
int x;
cin>>x;
int p=lower_bound(a,a+n,x)-a;
if(a[p]==x)
{
cout<<x<<" found at "<<p+1<<endl;
}
else
{
cout<<x<<" not found"<<endl;
}
}
}
return 0;
}
函数作用
编辑升序排列的容器:
iteratorlower_bound( const key_type &key ): 返回一个迭代器,指向键值>= key的第一个元素。
iterator upper_bound( const key_type &key ):返回一个迭代器,指向键值>key的第一个元素。
★降序排列的容器:
iteratorlower_bound( const key_type &key ): 返回一个迭代器,指向键值<= key的第一个元素。
iterator upper_bound( const key_type &key ):返回一个迭代器,指向键值<key的第一个元素。
举例
编辑例如:map中已经插入了1,2,3,4的话,如果lower_bound(2)的话,返回的2,而upper_bound(2)的话,返回的就是3
equal_range函数返回一个pair,pair里面第一个变量是lower_bound返回的迭代器,pair里面第二个迭代器是upper_bound返回的迭代器,如果这两个迭代器相等的话,则说明map中不出现这个关键字,程序说明
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include<map>
#include<string>
#include<iostream>
usingnamespacestd;
intmain()
{
map<
int
,string>mapStudent;
mapStudent[1]=
"student_one"
;
mapStudent[3]=
"student_three"
;
mapStudent[5]=
"student_five"
;
map<
int
,string>::iterator iter;
iter=mapStudent.lower_bound(2);
{
//返回的是下界3的
迭代器
cout<<iter->second<<
endl;
}
iter=mapStudent.lower_bound(3);
{
//返回的是下界5的迭代器
cout<<iter->second<<endl;
}
iter=mapStudent.upper_bound(2);
{
//返回的是上界3的迭代器
cout<<iter->second<<
endl;
}
iter=mapStudent.upper_bound(3);
{
//返回的是上界5的
迭代器
cout<<iter->second<<endl;
}
return0;
}
0 0
- UVa 10474 Where is the Marble?
- Uva 10474 Where is the Marble?
- uva 10474 Where is the Marble?
- UVA 10474 Where is the Marble?
- UVA 10474 - Where is the Marble
- uva-10474 - Where is the Marble?
- uva 10474 - Where is the Marble?
- UVA 10474 - Where is the Marble?
- UVa 10474 - Where is the Marble?
- UVa 10474 - Where is the Marble
- UVa 10474 - Where is the Marble?
- UVA 10474 Where is the Marble?
- UVA 10474 - Where is the Marble
- UVa - 10474 - Where is the Marble?
- uva 10474 Where is the Marble?
- uva 10474 Where is the Marble?
- uva - 10474 - Where is the Marble?
- uva 10474 - Where is the Marble
- 机器学习实战Machine Learning In Action 中的KNN代码详细解释
- Python实现协程的生产者与消费者
- 2016"百度之星" - 资格赛(Astar Round1)
- tomcat超时设置
- 查看linux服务器硬盘IO读写负载
- UVA - 10474 Where is the Marble?
- Linux搭建python环境
- NYOJ 42 一笔画问题(欧拉定理&&并查集)
- 特殊的棋子(bfs)(北理16校赛)
- Android开发者上手宝典(三)
- Android笔记-活动
- Java 实现 SSH 协议的客户端登录认证方式
- Camera实现动画Papaer效果
- 点击Viewflipper图片跳转到对应的页面代码