大理石在哪儿 where is the Marble? lower_bound()
来源:互联网 发布:淘宝手机详情页模板 编辑:程序博客网 时间:2024/04/30 09:41
例题:
大理石在哪儿
现有N个大理石,每个大理石上写了一个非负整数、首先把各数从小到大排序然后回答Q个问题。每个问题问是否有一个大理石写着某个整数x,如果是,还要
回答哪个大理石上写着x。排序后的大理石从左到右编号为1~N。(在样例中,为了
节约篇幅,所有大理石的数合并到一行,所有问题也合并到一行。)
样例输入:
4 1
2 3 5 1
5
5 2
1 3 3 3 1
2 3
样例输出:
CASE# 1:
5 found at 4
CASE# 2:
2 not found
3 found at 3
demo:
#include <iostream>#include <algorithm>using namespace std;#define maxn 1000int main(){ int n,q,x,kase = 0; int a[maxn]; while(scanf("%d%d",&n,&q) == 2 && n) { printf("CASE# %d:\n",++kase); for(int i = 0; i < n; i++) { scanf("%d",&a[i]);}sort(a, a + n);while(q--){ scanf("%d",&x); int p = lower_bound(a, a + n, x) - a; if(a[p] == x) { printf("%d found at %d\n", x, p + 1); } else { printf("%d not found\n",x); }} } return 0;}
总结:
1.sort()函数真好使啊。。。
2.lower_bound()函数
资料:
STL中关于二分查找的函数有三个lower_bound 、upper_bound 、binary_search 。这三个函数都运用于有序区间(当然这也是运用二分查找的前提),下面记录一下这两个函数。
ForwardIter lower_bound(ForwardIter first, ForwardIter last,const _Tp& val)算法返回一个非递减序列[first, last)中的第一个大于等于值val的位置。
ForwardIter upper_bound(ForwardIter first, ForwardIter last, const _Tp& val)算法返回一个非递减序列[first, last)中的第一个大于值val的位置。
lower_bound和upper_bound如下图所示:
关于二分法排序,参考下面的博客,讲的太全了。。。
http://blog.csdn.net/EbowTang/article/details/50770315 0 0
- 大理石在哪儿 where is the Marble? lower_bound()
- uva10474大理石在哪儿where is the marble?
- 大理石在哪儿 where is the Marble?
- UVa 10474 Where is the Marble?(大理石在哪儿)
- 10474 - 大理石在哪儿Where is the Marble?(排序+检索)
- uva 10474 Where is the Marble?(大理石在哪儿)
- 大理石在哪儿(Where is the marble?,UVa 10474)
- 《算法竞赛入门经典2ndEdition 》例题5-1 大理石在哪儿(Where is the marble?, Uva10474)
- 大理石在哪(Where is the Marble?, uva 10474)
- UVA10474 Where is the Marble?(二分查找,STL-lower_bound)
- Where is the Marble
- Where is the Marble?
- Where is The Marble
- Where is the Marble?
- Where is the Marble?
- Where is the Marble?
- Where is the Marble?
- Where is the Marble?
- HashMap 哈希算法
- 关于换工作后的心得
- 【工作笔记】ElasticSearch从零开始学(三)—— 入门(聚合)
- 大陆身份证号码格式校验代码
- Ubuntu 下使用 ADB 调试 Android 应用时的设备识别问题
- 大理石在哪儿 where is the Marble? lower_bound()
- 单线程实现定时执行任务功能
- battery-historian工具环境部署及使用
- 数组赋值和传递(从网上看到的几种情况)
- 表单中的单选框、多选框中的name属性用途
- CAP理论以及hadoop组件
- C++运算符重载
- 如何改变eclipse中的字体大小
- 用selenium实现用谷歌浏览器打开指定网址