大理石在哪儿(Where is the Marble?,Uva 10474)
现有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
【分析】
题目意思已经很清楚了:先排序,再查找。使用algorithm头文件中的sort和lower_bound 很容易完成这两项操作,代码如下:
#include<algorithm>
using namespace std;
const int maxn = ;
int main() {
int n, q, x, a[maxn], kase = ;
while(scanf("%d%d", &n, &q) == && n) {
printf("CASE# %d:\n", ++kase);
for(int i = ; 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; //在已排序数组a中寻找x
if(a[p] == x) printf("%d found at %d\n", x, p+);
else printf("%d not found\n", x);
}
}
return ;
}
lower_bound 函数:
lower_bound()返回值是一个迭代器,返回指向比key大的第一个值的位置。例如:
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
int a[]={,,,,,,,};
printf("%d",lower_bound(a,a+,)-a);
return ;
}
lower_bound函数返回的是一个地址,-a之后变成下标。
不用lower_bound函数:
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std; int main() {
int n,m,count=;
while(){ cin>>n>>m;
if(n==) break;
int a[n];
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
}
printf("CASE# %d:\n",++count);
sort(a+,a+n+);
while(m--){
int x;
scanf("%d",&x);
int flag=;
for(int i=;i<=n;i++){
if(x==a[i]){
printf("%d found at %d\n",x,i);
flag=;
break;
}
}
if(!flag) printf("%d not found\n",x);
}
} return ;
}
大理石在哪儿(Where is the Marble?,Uva 10474)的更多相关文章
- 大理石在哪儿 (Where is the Marble?,UVa 10474)
题目描述:算法竞赛入门经典例题5-1 #include <iostream> #include <algorithm> using namespace std; ; int m ...
- Where is the Marble UVA - 10474
Raju and Meena love to play with Marbles. They have got a lot of marbles with numbers written on th ...
- UVA.10474 Where is the Marble ( 排序 二分查找 )
UVA.10474 Where is the Marble ( 排序 二分查找 ) 题意分析 大水题一道.排序好找到第一个目标数字的位置,返回其下标即可.暴力可过,强行写了一发BS,发现错误百出.应了 ...
- 大理石在哪?(Where is the Marble?,UVa 10474)
参考:ACM紫书 第五章 P108 [排序与检索] 下面的代码中有些 提示性输出,想Ac 需删除提示性输出语句,读者自行修改. #include <cstdio> #include < ...
- 【UVA - 10474 】Where is the Marble?(排序)
Where is the Marble? Descriptions: Raju and Meena love to play with Marbles. They have got a lot of ...
- 大理石在哪里UVa 10474
我自己写的代码 #include<iostream>#include<algorithm>using namespace std;int main(){ int N,a[ ...
- UVA 10474 大理石在哪 lower_bound
题意:找输入的数在排完序之后的位置. 主要是lower_bound 函数的使用.它的作用是查找大于或者等于x的第一个位置. #include<cstdio> #include<alg ...
- UVa 10474 Where is the Marble
题意:给出一列数,先排序,再查找学习了sort函数,lower_bound函数sort:可以给任意对象排序(包括自己定义的)(前提是定义好了‘<’运算符)lower_bound:查找大于或者等于 ...
- uva 10474 Where is the Marble? 计数排序
题目给出一系列数字,然后问哪个数字是从小到大排在第几的,重复出现算第一个. 数据范围为10000,不大,完全可以暴力,sort不会超时. 但是由于以前做比赛时也遇到这种题目,没注意看数据范围,然后暴力 ...
随机推荐
- a high-level neural networks AP
Keras Documentation https://keras.io/ You have just found Keras. Keras is a high-level neural networ ...
- 7. ExtJS.form中msgTarget
转自:https://www.cnblogs.com/guafuli/articles/msgtarget_values.html Ext表单提示方式:msgTarget:有4中方式:qtip,tit ...
- [Swift通天遁地]一、超级工具-(19)制作六种别具风格的动作表单
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- Photoshop CC2019破解版
Photoshop CC2019 精简版: 链接:https://pan.baidu.com/s/1PeFrhtLHxLRXCW_vMkAZDg 提取码:q6nl Photoshop CC2019: ...
- redis 缓存应用
第1章 部署与安装 wget http://download.redis.io/releases/redis-3.2.10.tar.gz tar xf redis-3.2.10.tar.gz cd r ...
- ibatis入门教程一
这几天研究ibatis玩,参考一篇贴子进行安装配置:蓝雪森林 选择这个帖子来跟随配置是因为这个帖子看着比较干净,但是我仍旧在配置得过程中出现了好几个问题,所以我决定在这个帖子的基础上将更多细节加上,做 ...
- maxItemsInObjectGraph解释
maxItemsInObjectGraph:一个整数,指定要序列化或反序列化的最大项数,可以限制对象图中要序列化的项数.默认的就是65535,当客户端与WebService之间传递的是对象要序列化的个 ...
- python 模块-easygui.buttonbox
2018-03-0315:43:11 ): Yes_or_No = easygui.buttonbox("是否良品?", choices=['Yes', 'No', '退出']) ...
- Java屏幕截图及剪裁
Java标准API中有个Robot类,该类可以实现屏幕截图,模拟鼠标键盘操作这些功能.这里只展示其屏幕截图. 截图的关键方法createScreenCapture(Rectangle rect) ,该 ...
- PMBOK项目管理九大知识领域和五大流程 --美国IT项目管理硕士笔记(二)
PMBOK 项目管理 九大知识领域和五大流程 PMI Project Management Institute.PMI 是世界上最大的非盈利机构,是项目管理领域的领导者.PMI制定项目管理行业标准 ...