大理石在哪儿(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不会超时. 但是由于以前做比赛时也遇到这种题目,没注意看数据范围,然后暴力 ...
随机推荐
- 修改数据表的字符集为utf8mb4
修改数据表的字符集为utf8mb4
- 洛谷T51924 忘情
二分上界有多大开多大 二分上界有多大开多大 二分上界有多大开多大 重要的事情说三遍 又被bright神仙带着做题了 先无脑上wqs二分 我们可以把这个柿子画一下,区间的花费就变成((sigema(l~ ...
- 【Silverlight】Bing Maps学习系列(五):绘制多边形(Polygon)图形(转)
[Silverlight]Bing Maps学习系列(五):绘制多边形(Polygon)图形 Bing Maps Silverlight Control支持用户自定义绘制多边形(Polygon)图形, ...
- 杂项-公司:Sun
ylbtech-杂项-公司:Sun Sun Microsystems是IT及互联网技术服务公司(已被甲骨文收购)Sun Microsystems 创建于1982年.主要产品是工作站及服务器.1986年 ...
- Angular学习日记(一) 开发环境的搭建
从看视频到自己尝试,总会走很多的弯路才能成功. 首先:装最新版的nodejs. 第一种:使用npm安装 首先设置taobao镜像,npm config set registry https://reg ...
- java编程中的断言工具类(org.springframework.util.Assert)
转自:https://blog.csdn.net/gokeiryou263/article/details/19612471 断言工具类:Assert类, java.lang.Object ---&g ...
- nginx - ubutun下安装nginx(详述编译方法)
一.使用apt命令安装 sudo apt-get install nginx 二.编译方法安装(个人实践方法,具体见官方文档) 1)说明:我使用的虚拟机是64位 ubuntu server14.04, ...
- IDEA UI版本取消Output窗口打印消息的条数的限制
打开IDEA的安装目录-->进入bin文件夹-->编辑idea.properties文件::修改idea.cycle.buffer.size=1024为idea.cycle.buffer. ...
- J20170602-ts
アソシエーション association n. 联想; 协会,社团; 联合,联系; アンケート 英文是questionnaire
- bzoj 1034: [ZJOI2008]泡泡堂BNB【贪心】
是贪心 先把两个数组排序,然后贪心的选让a数组占优的(如果没有就算输),这是最大值,最小值是2n-贪心选b数组占优 #include<iostream> #include<cstdi ...