大理石在哪儿(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不会超时. 但是由于以前做比赛时也遇到这种题目,没注意看数据范围,然后暴力 ...
随机推荐
- 内容原发网站seo不重视2个标签,导致seo效果不如转发网站
采集数据,挖掘观点,小心求证,得出结论 时间经过 今日凌晨,爬虫热点采集,其中第一财经是目标站之一,采集到了 http://www.yicai.com/news/5391233.html 谷歌去年悄然 ...
- H264 层次构成[2]
H264层次构成 H264标准是由JVT(Joint Video Team,视频联合工作组)组织提出的新一代数字视频编码标准.JVT于2001年12月在泰国Pattaya成立.它由ITU-T的VCEG ...
- android 制作9.png图片
什么叫.9.PNG呢,这是安卓开发里面的一种特殊的图片 这种格式的图片在android 环境下具有自适应调节大小的能力. (1)允许开发人员定义可扩展区域,当需要延伸图片以填充比图片本身更大区 ...
- poj 2186(tarjan+缩点)
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 37083 Accepted: 15104 De ...
- hdoj--5526--欧拉回路(欧拉回路)
欧拉回路 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...
- luogu 1901 发射站
题目大意: 一个数列,它左边第一个比它高的人和右边第一个比它高的人要加上它的权值 思路: 单调栈维护一个单调递减的栈 正反各维护一遍 #include<iostream> #include ...
- bzoj1085 [SCOI2005]骑士精神——IDA*
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1085 搜索,IDA*,估价就是最少需要跳的步数: 代码意外地挺好写的,memcmp 用起来好 ...
- bzoj1854 [Scoi2010]游戏——匈牙利算法
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1854 这题...据说可以用并查集做,但没有去看... 用二分图匹配的话,就把装备和它的两个属 ...
- NestedPreb
屌丝手动版 One of the things we’re sorely missing from Unity is nested prefabs. So we rolled this little ...
- Spring 中 ApplicationContext 和 BeanFactory 的区别,以及 Spring bean 作用域
//从ApplicationContext 中取 bean ApplicationContext ac = new ClassPathXmlApplicationContext ( "com ...